forked from chicuongit913/MumTestJavaExam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisMinMaxDisjoint.java
51 lines (40 loc) · 1.24 KB
/
isMinMaxDisjoint.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
package mumTestPackage;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
class MumTest31 {
@Test
void test() {
assertEquals(0, isMinMaxDisjoint(new int[] {18, -1, 3, 4, 0}));
assertEquals(1, isMinMaxDisjoint(new int[] {9, 0, 5, 19}));
assertEquals(0, isMinMaxDisjoint(new int[] {0, 5, 18, 0, 9}));
assertEquals(0, isMinMaxDisjoint(new int[] {7, 7, 7, 7}));
assertEquals(0, isMinMaxDisjoint(new int[] {}));
assertEquals(1, isMinMaxDisjoint(new int[] {0, -1 , -2 , -3 , -4, 5, 9, 2 , 18}));
}
int isMinMaxDisjoint(int[ ] a) {
if (a == null|| a.length < 3) {
return 0;
}
int minIndex = 0, maxIndex = 0;
int minOccour = 1, maxOccour = 1;
for (int i = 1; i < a.length; i++) {
if(a[minIndex] > a[i]) {
minIndex = i;
minOccour = 1;
} else if (a[maxIndex] < a[i]) {
maxIndex = i;
maxOccour = 1;
} else if (a[minIndex] == a[i]) {
minOccour++;
} else if (a[maxIndex] == a[i]) {
maxOccour++;
}
}
System.out.println(Arrays.toString(new int[] {minIndex, maxIndex, minOccour, maxOccour}));
if(a[minIndex] != a[maxIndex] && Math.abs(minIndex-maxIndex) != 1 && minOccour == 1 && maxOccour == 1) {
return 1;
}
return 0;
}
}