forked from chicuongit913/MumTestJavaExam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisMadhavArray.java
56 lines (45 loc) · 1.25 KB
/
isMadhavArray.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
55
56
package mumTestPackage;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import sun.tools.jar.resources.jar;
class MumTest7 {
@Test
void test() {
assertEquals(1, isMadhavArray(new int[] {6, 2, 4, 2, 2, 2, 1, 5, 0, 0}));
assertEquals(1, isMadhavArray(new int[] {2, 1, 1}));
assertEquals(1, isMadhavArray(new int[] {2, 1, 1, 4, -1, -1}));
assertEquals(0, isMadhavArray(new int[] {18, 9, 10, 6, 6, 6}));
assertEquals(0, isMadhavArray(new int[] {-6, -3, -3, 8, -5, -4}));
assertEquals(1, isMadhavArray(new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, -2, - 1}));
assertEquals(1, isMadhavArray(new int[] {3, 1, 2, 3, 0, 0}));
}
int isMadhavArray(int[ ] a) {
if(a.length < 3 )
return 0;
int i = 1, counter = 2;
while (i < a.length) {
int sum = 0;
for (int j = 0; j < counter; j++, i++) {
sum +=a[i];
}
if(sum != a[0])
return 0;
System.out.println("--------");
System.out.println(i);
System.out.println(counter);
System.out.println("--------");
if(i == a.length)
{
System.out.println("----++++---");
if(counter*(counter+1)/2 == a.length)
return 1;
}
else {
counter++;
if((i+counter) > a.length)
return 0;
}
}
return 1;
}
}