-
Notifications
You must be signed in to change notification settings - Fork 0
/
AliceBobChocolate.java
48 lines (43 loc) · 1.04 KB
/
AliceBobChocolate.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
43
44
45
46
47
48
/* https://codeforces.com/problemset/problem/6/C
* tag: #greedy #two-pointer
* count sumLeft, sumRight,
* the smaller sum += nextElement based on its side.
*/
import java.util.Scanner;
public class AliceBobChocolate {
static String calAmountOfBars() {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
int temp = scanner.nextInt();
arr[i] = temp;
}
scanner.close();
// special case
if (n == 1) {
return "1 0";
}
int sumAlice = arr[0];
int sumBob = arr[n - 1];
int countAlice = 1;
int countBob = 1;
int left = 1;
int right = n - 2;
while (left <= right) {
if (sumAlice <= sumBob) {
sumAlice += arr[left];
countAlice += 1;
left++;
} else {
sumBob += arr[right];
countBob += 1;
right--;
}
}
return countAlice + " " + countBob;
}
public static void main(String[] args) {
System.out.println(calAmountOfBars());
}
}