-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPS2Q7.java
More file actions
39 lines (37 loc) · 838 Bytes
/
Copy pathPS2Q7.java
File metadata and controls
39 lines (37 loc) · 838 Bytes
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
public class PS2Q7 {
public static void printArray(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] a = { 2,1,6,4 };
int[] eps = new int[a.length];
int[] ops = new int[a.length];
eps[0] = a[0];
for (int i = 1; i < a.length; i++) {
if (i % 2 == 0) {
eps[i] = eps[i - 1] + a[i];
}
if (i % 2 != 0) {
eps[i] = eps[i - 1];
}
}
ops[1] = a[1];
for (int i = 2; i < a.length; i++) {
if (i % 2 == 0) {
ops[i] = ops[i - 1];
}
if (i % 2 != 0) {
ops[i] = ops[i - 1] + a[i];
}
}
for (int i = 1; i < a.length; i++) {
if (eps[i - 1] + (ops[a.length-1]-ops[i]) == (eps[a.length-1]-eps[i]) + ops[i - 1]) {
System.out.println(i);
System.out.println(a[i]);
}
}
}
}