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.
Here is my solution in Java. I initially solved it by replacing the elements in list with its correct position and counting the bribes while doing that. That yielded result but few of the test cases failed due to performance. I tried a little in local IDE and took help online to arrive at this solution}
publicstaticvoidminimumBribes(List<Integer>inp){inttotalbribe=0;intcurrentPerson=0;intoriginalPosition=0;intsize=inp.size();//loop through all the elementsfor(inti=0;i<size;i++){currentPerson=inp.get(i);originalPosition=currentPerson-1;//If deviation of more than 2 position then returnif(originalPosition-i>2){System.out.println("Too chaotic");return;}//count the number of bribes current person has received.//currentPerson - 2 because if someone moved more than 2 position then it is chaotic for(intj=Math.max(0,currentPerson-2);j<i;j++){if(inp.get(j)>currentPerson){totalbribe++;}}}System.out.println(totalbribe);}
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 →
Here is my solution in Java. I initially solved it by replacing the elements in list with its correct position and counting the bribes while doing that. That yielded result but few of the test cases failed due to performance. I tried a little in local IDE and took help online to arrive at this solution}