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.
public static void minimumBribes(List q) {
int bribes = 0;
for (int i = q.size() - 1; i >= 0; i--) {
// Check if the person at position i has moved more than 2 positions forward
if (q.get(i) - (i + 1) > 2) {
System.out.println("Too chaotic");
return;
}
// Count how many people have overtaken the person in position i
for (int j = Math.max(0, q.get(i) - 2); j < i; j++) {
if (q.get(j) > q.get(i)) {
bribes++;
}
}
}
System.out.println(bribes);
}
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 →
public static void minimumBribes(List q) { int bribes = 0;
}