-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTwoConditions.java
54 lines (41 loc) · 1.81 KB
/
TwoConditions.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
49
50
51
52
53
54
public class TwoConditions {
// Write a function that will return 1 if an integer array satisfies the following conditions and returns 0 otherwise.
// 1. it has even numbers of elements
// 2. Sum of all the numbers in the first half of the array is equal to the sum of all the numbers in the second half of the array.
//
// If you are programming in Java, the function Signature is
// int answerThree(int[] a)
// Examples
// -------------------|--------|-----------------------------------------------------------------------
// | a | return | Explanation |
// |-------------------|--------|-----------------------------------------------------------------------|
// | {5,4,3,2,3,4,6,1} | 1 | *There are 8 (even) number of elements in the array. Thus condition 1 |
// | | | satisfied. |
// | | | *The sum of all the numbers in the first half = 5+4+3+2 = 14 |
// -------------------|--------|-----------------------------------------------------------------------
static int answerThree(int [] a){
if (a.length % 2 != 0) {
return 0;
}
int firstSum = 0;
int lastSum = 0;
int midIndex = a.length/2;
for (int i=0;i<a.length;i++) {
if (i < midIndex){
firstSum += a[i];
}else {
lastSum += a[i];
}
}
return firstSum == lastSum ? 1 : 0;
}
static void twoConditionsTest() {
System.out.println(answerThree(new int[]{5,4,3,2,3,4,6,1}));
System.out.println(answerThree(new int[]{5,4,3,2,3,4,6}));
System.out.println(answerThree(new int[]{5,4,3,2,3,4,6,2}));
System.out.println(answerThree(new int[]{6,4,3,2,3,4,6,2}));
}
public static void main(String[] args) {
twoConditionsTest();
}
}