What is the tool he is using for testing?
Read more… (9 words)
Codechef has same question in their practice problems.
https://www.codechef.com/problems/TLG
In map, the elements are sorted by their key values( here voter ids ).
Take care of corner cases like when maximum number of symbols occur in the last pair of brackets
#include<iostream>using namespace std;int main(){int n, pos=0, maxpos=0, depth = -1, maxdepth = -1, maxdepthpos = 0, maxsym = 0, sym = 0;cin>>n;int arr[n];for(int i=0; i<n; i++)cin>>arr[i];for(int i=0; i<n; i++){if(depth==-1){if(sym>maxsym){maxpos = pos;maxsym = sym;}sym = 1;
#include<iostream>#include<map>#include<vector>using namespace std;int main(){int n1, n2, n3, x, count = 0;vector<int> vtrs;map<int, int> voters;cin>>n1>>n2>>n3;for(int i=0; i<n1; i++){cin>>x;voters[x]++;}for(int i=0; i<n2; i++){cin>>x;voters[x]++;}for(int i=0; i<n3; i++){cin>>x;
#include<iostream>#include<vector>using namespace std;int main(){int N, H; cin>>N>>H;int stack[N];for(int i=0; i<N; i++) cin>>stack[i];vector<int> commands;int temp;do{cin>>temp;commands.push_back(temp);}while(temp); // stop taking inputs when 0 occursbool picked_box = false;int i = 0, position = 0;while(commands[i]){switch(commands[i]){case 1:
My solution uses only O(1) memory and O(N) time.
#include<iostream>#include<cmath>using namespace std;int main(){int N; cin>>N;int p1, p1_total = 0, p2, p2_total = 0, winner, max_lead = 0;for(int i=0; i<N; i++){cin>>p1>>p2;p1_total += p1; p2_total += p2; // sum of scores of player1 and player2 till the current roundint temp = p2_total-p1_total; // temp is -ve if player1 is in lead after the current roundif (max_lead<abs(temp)){max_lead = abs(temp);winner = temp>0 ? 2 : 1;}}cout<<winner<<" "<<max_lead;}