Max likes football very much. In order to check the popularity of the game, he decided to talk to random people and ask them about their favourite game and note it down in a list.
Given a list of names of people and their favorite sport, help Max in finding the sport liked by most of the people, and also how many people like football.
He could have met the same people more than once, he will only count the response of his first meet with any person.
Note: Name of person as well as sport is a single string in lowercase. The length of the name of people, as well as sport, is less than 11.
Example:
Input: n = 5, data = {{"abir", "cricket"}, {"aayush", "cricket"}, {"newton", "kabaddi"}, {"abhinash", "badminton"}, {"sanjay", "tennis"}, {"abhinash", "badminton"}, {"govind", "football"}}
Output:
cricket 1
Approach:
C++
#include <bits/stdc++.h>using namespace std;bool cmp(pair<string, int> a, pair<string, int> b){if (a.second == b.second){return a.first > b.first;}return a.second < b.second;}void statistics(int n, vector<pair<string, string>> &data){map<string, int> mp;unordered_set<string> st;for (int i = 0; i < n; i++){string name = data[i].first, sport = data[i].second;if (st.find(name) == st.end())mp[sport]++;st.insert(name);}vector<pair<string, int>> v;for (auto it = mp.begin(); it != mp.end(); it++)v.push_back({it->first, it->second});sort(v.begin(), v.end(), cmp);int p = v.size();cout << v[p - 1].first << "\n";cout << mp["football"] << "\n";}int main(){int n = 7;vector<pair<string, string>> data = {{"abir", "cricket"},{"aayush", "cricket"},{"newton", "kabaddi"},{"abhinash", "badminton"},{"sanjay", "tennis"},{"abhinash", "badminton"},{"govind", "football"}};statistics(n, data);return 0;}
No comments:
Post a Comment