We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
voidminimumBribes(vector<int>q){inttotalBribes=0;for(inti=0;i<q.size();i++){if(q[i]-(i+1)>2){// If the difference between an elements' initial position and it's final position is greater than 2,// that means it moved to the left more than twice, which means it bribed more than twice. That means the line's Too Chaotic.cout<<"Too chaotic"<<endl;return;}// Start from one position ahead (in the line) of where q[i] *could* have started (i.e at an index of either index 0 or q[i] - 2, whichever is higher). // Count how many people with a higher position than q[i] are in front of q[i] in the queue (i.e behind q[i] in the array), // because each of those people must have bribed their way past q[i].for(intj=max(0,q[i]-2);j<i;j++){if(q[j]>q[i]){totalBribes++;}}}cout<<totalBribes<<endl;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
New Year Chaos
You are viewing a single comment's thread. Return to all comments →
C++ solution: