-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFairRations.java
42 lines (36 loc) · 1.14 KB
/
FairRations.java
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
41
42
/**
* Hacker Rank
* Fair Rations
* https://www.hackerrank.com/challenges/fair-rations/problem
*/
public class FairRations {
public static void main(String[] args) {
System.out.println(fairRations(new int[]{4, 7, 3, 2, 5, 9, 12})); // 4
System.out.println(fairRations(new int[]{7, 7, 7})); // NO
System.out.println(fairRations(new int[]{1, 2, 2, 2, 2, 3})); // 10
}
private static int fairRations(int[] a) {
if (a == null || a.length == 0) return 0;
int count = 0;
int odds = 0;
//for (int val : a) if (val % 2 == 1) odds++;
int firstOdd = -1;
for (int i = 0; i < a.length; i++) {
if (a[i] % 2 == 1) {
odds++;
if (firstOdd == -1) {
firstOdd = i;
} else {
// System.out.println("firstOdd = " + firstOdd + ", i = " + i);
count += (i - firstOdd) << 1;
firstOdd = -1;
}
}
}
if (odds % 2 == 1) {
System.out.println("NO");
return 0;
}
return count;
}
}