Skip to content

Instantly share code, notes, and snippets.

@randyhbh
Created December 18, 2018 22:39
Show Gist options
  • Save randyhbh/9a9f594fbcca378578208711b89f610a to your computer and use it in GitHub Desktop.
Save randyhbh/9a9f594fbcca378578208711b89f610a to your computer and use it in GitHub Desktop.
This method is the answer to the problem of "New Year's Chaos" in which he receives an int [] and must answer how many bribes there have been; If a person bribes more than 2 times it is total chaos.
static void minimumBribes(int[] q) {
int ans=0;
for(int i=q.length -1; i >= 0 ; i--) {
if (q[i] - (i+1) > 2) {
System.out.println("Too chaotic");
return;
}
for (int j = Integer.max(0, q[i] - 2); j < i; j++)
if (q[j] > q[i]) ans++;
}
System.out.println(ans);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment