-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
anamika.anamika
committed
Dec 30, 2023
1 parent
76d007a
commit d0141df
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# POTD Questins from [GfG](https://www.geeksforgeeks.org/) | ||
|
||
* [Winner of an Election](./WinnerOfAnElection/Readme.md) | ||
* []() |
56 changes: 56 additions & 0 deletions
56
Platform-Wise/GeeksForGeeks/POTD/WinnerOfAnElection/Readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Winner of an Election | ||
|
||
> Level: Easy | ||
### Problem Description: | ||
Given an array of **n** names **arr** of candidates in an election, where each name is a string of **lowercase** characters. A candidate name in the array represents a vote casted to the candidate. Print the name of the candidate that received the **maximum count** of votes. If there is a **draw** between two candidates, then print **lexicographically** **smaller** name. | ||
|
||
Example 1: | ||
|
||
``` | ||
Input: | ||
n = 13 | ||
arr[] = {john,johnny,jackie,johnny,john | ||
jackie,jamie,jamie,john,johnny,jamie, | ||
johnny,john} | ||
Output: john 4 | ||
``` | ||
|
||
Explanation: john has 4 votes casted for | ||
him, but so does johny. john is | ||
lexicographically smaller, so we print | ||
john and the votes he received. | ||
|
||
----------- | ||
|
||
Example 2: | ||
|
||
``` | ||
Input: | ||
n = 3 | ||
arr[] = {andy,blake,clark} | ||
Output: andy 1 | ||
``` | ||
|
||
Explanation: All the candidates get 1 | ||
votes each. We print andy as it is | ||
lexicographically smaller. | ||
|
||
------------ | ||
|
||
### Your Task: | ||
You only need to complete the function **winner()** that takes an array of strings **arr**, and length of **arr n** as parameters and returns an **array of string of length 2**. First element of the array should be the **name** of the candidate and second element should be the **number of votes** that candidate got in **string format**. | ||
|
||
**Complexity:** | ||
* Expected Time Complexity: **O(n)** | ||
* Expected Auxiliary Space: **O(n)** | ||
|
||
**Constraints:** <br> | ||
* 1 <= n <= 105 <br> | ||
* 1 <= |arri| <= 105 | ||
|
||
-------------------- | ||
|
||
> [Problem Link on GfG](https://www.geeksforgeeks.org/problems/winner-of-an-election-where-votes-are-represented-as-candidate-names-1587115621/1) | ||
------------------- |
38 changes: 38 additions & 0 deletions
38
Platform-Wise/GeeksForGeeks/POTD/WinnerOfAnElection/sol.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class Solution: | ||
|
||
#Complete this function | ||
|
||
#Function to return the name of candidate that received maximum votes. | ||
def winner(self,arr,n): | ||
# Your code here | ||
# return the name of the winning candidate and the votes he recieved | ||
d={} | ||
for a in arr: | ||
d[a]=d.get(a,0)+1 | ||
#print(d) | ||
|
||
m=-1 | ||
for v in d.values(): | ||
if v>=m: | ||
m=v | ||
# max number of votes are m now | ||
#print(m) | ||
|
||
l=[] | ||
for k in d.keys(): | ||
if d[k]==m: | ||
l.append(k) | ||
#print(l) | ||
return [min(l),m] | ||
|
||
# { | ||
# Driver Code Starts | ||
if __name__=="__main__": | ||
T=int(input()) | ||
for _ in range(T): | ||
n=int(input()) | ||
arr=input.strip().split() | ||
|
||
result=Solution.winner(arr, n) | ||
print(result[0], result[1]) | ||
# } Driver Code Ends |