Who is the Winner Editorial (Cladding the code Round 2)

The question was to find the winner based on the total score of each participant and if participants are having equal scores then their timestamp is checked for their last submission. Those who has it first wins. The code is below.

#include<bits/stdc++.h>

using namespace std;
int main()
{
int n; cin>>n;
std::vector<pair <int, pair<string, int>>> apiData;
for(int i = 0; i < n;i++)
{
int timeStamp, scoreObtained;
string userName;
cin>>timeStamp>>userName>>scoreObtained;
apiData.push_back({timeStamp, {userName, scoreObtained}});
}
sort(apiData.begin(), apiData.end());
map<string, int> sortedUsersWithScores, sortedUsersWithMaximumScores;
for(int i = 0; i < n;i++)
sortedUsersWithScores[apiData[i].second.first]+=apiData[i].second.second;
int maxScore=INT_MIN;
for(int i = 0; i < n;i++)
maxScore = max(maxScore, sortedUsersWithScores[apiData[i].second.first]);
for(int i = 0; i < n;i++)
{
if(maxScore == sortedUsersWithScores[apiData[i].second.first])
{
sortedUsersWithMaximumScores[apiData[i].second.first] += apiData[i].second.second;
if(sortedUsersWithMaximumScores[apiData[i].second.first] >= maxScore)
{
cout<<apiData[i].second.first<<endl;
break;
}
}
}
return 0;
}

Please Give me a test case for which this code fails for the problem PASSIVE BEANS @pj220593

100
95 94 31 65 35 95 70 78 81 36 69 97 39 28 89 62 36 23 35 21 36 11 65 39 13 34 79 87 91 70 43 82 24 97 6 87 49 81 60 92 63 9 16 6 31 90 6 5 70 48 27 86 65 66 45 8 67 17 24 44 34 93 26 32 82 54 72 83 94 81 79 79 9 9 4 59 42 83 80 67 28 62 56 58 87 67 34 3 61 14 62 19 94 31 74 97 20 70 7 98

Please try this on your given code , answer to this is 52 while yours it is giving 99.

Can you explain how is it 52 ? the sorted array looks like.
3,4,5,6,6,6,7,8,9,9,9,11,13,14,16,17,19,20,21,23,24,24,26,27,28,28,31,31,31,32,34,34,34,35,35,36,36,36,39,39,42,43,44,45,48,49,54,56,58,59,60,61,62,62,62,63,65,65,65,66,67,67,67,69,70,70,70,70,72,74,78,79,79,79,80,81,81,81,82,82,83,83,86,87,87,87,89,90,91,92,93,94,94,94,95,95,97,97,97,98
Now if I delete everything but 98 then gcd will be 98 which is the maximum, I think. @pj220593

1 Like