Elections in ChefLand- Editorial || CL_ELECTIONS

PROBLEM LINK: Elections in ChefLand | CodeChef

Problem Code: Elections in ChefLand | CodeChef

Practice: CodeChef | Competitive Programming | Participate & Learn | CodeChef

Contest : Code to Compete Coding Competition | CodeChef

Author: Codechef Adgitm Chapter : https://www.codechef.com/users/test_account_9
Tester: Codechef Adgitm Chapter : https://www.codechef.com/users/test_account_9
Editorialist: Codechef Adgitm Chapter : https://www.codechef.com/users/test_account_9

DIFFICULTY:

Easy-Med

PROBLEM:

Chef lives in ChefLand. In ChefLand, there is an election going on for the President of ChefLand and Chef is in in-charge of declaring the result of the elections. All the citizens of ChefLand casted their vote for their president. Chef has the final list in which the candidate name is written in such a way that every citizen writes their preferred candidates in that list. Now, you help the chef to find out the winner of this election through this list.

NOTE− If two candidates have the same number of votes then print the name of the lexicographically smaller candidate name.

EXPLANATION:
Traverse and note down score adjacently.

SOLUTION:
C++:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
int n, q, k, m;
vector primes(N);

void solve() {
cin >> n;
map<string, int> mp;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (char& ch: s) {
ch = toupper(ch);
}
mp[s]++;
}

int mx = 0;
string s = "";
for (auto it: mp) {
	if (mx == 0 || mx < it.second) {
		mx = it.second;
		s = it.first;
	}
}
cout << s << " " << mx << "\n";

}

int main() {
cin >> t;
while (t–) {
solve();
}
return 0;
}

1 Like