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.
JAVA
public static void minimumBribes(List list) {
int totalBribes = 0;
for (int i = 0; i < list.size(); i++) {
int bribes = list.get(i) - (i + 1);
if (bribes > 2) {
System.out.println("Too chaotic");
return;
}
// Count total bribes for the current person
for (int j = Math.max(0, list.get(i) - 2); j < i; j++) {
if (list.get(j) > list.get(i)) {
totalBribes++;
}
}
}
System.out.println(totalBribes);
}
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 →
JAVA public static void minimumBribes(List list) { int totalBribes = 0; for (int i = 0; i < list.size(); i++) { int bribes = list.get(i) - (i + 1); if (bribes > 2) { System.out.println("Too chaotic"); return; } // Count total bribes for the current person for (int j = Math.max(0, list.get(i) - 2); j < i; j++) { if (list.get(j) > list.get(i)) { totalBribes++; } } } System.out.println(totalBribes); }