-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewYearChaos.java
More file actions
40 lines (31 loc) · 1.41 KB
/
newYearChaos.java
File metadata and controls
40 lines (31 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//HACKERRANK New Year Chaos
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
public class newYearChaos {
public static void main(String[] args) {
List<Integer> exampleList = new ArrayList<>(Arrays.asList(2, 1, 5, 3, 4));
minimumBribes(exampleList);
exampleList = Arrays.asList(2, 5, 1, 3, 4);
minimumBribes(exampleList);
}
public static void minimumBribes(List<Integer> q){
int bribes = 0;
for(int i = q.size() - 1; i >= 0; i--){
int difference = q.get(i) - (i + 1);
if(difference > 2){
System.out.println("Too chaotic");
return;
}
//Subtracting by 2 is a clever shortcut because we don't need to check more than 2 spots ahead.
//Example: 4 at end of array. Initial position would have been in 4th place (1-indexed). If person bribes them, then 4 is in 5th place and that number is in 4th. If that number bribes again they would be in third, or index 2 (and q.get(i) - 2 would be 2).
//Also the Math.max(0) is a safety measure because if the number is 1, then q.get(i) - 2 would be -1 and we can't index that so we just do 0.
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);
}
}