forked from chicuongit913/MumTestJavaExam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisNUnique.java
37 lines (30 loc) · 822 Bytes
/
isNUnique.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
package mumTestPackage;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class MumTest20 {
@Test
void test() {
assertEquals(1, isNUnique(new int[] {7, 3, 3, 2, 4}, 11));
assertEquals(0, isNUnique(new int[] {7, 3, 3, 2, 4}, 10));
assertEquals(0, isNUnique(new int[] {7, 3, 3, 2, 4}, 6));
assertEquals(0, isNUnique(new int[] {7, 3, 3, 2, 4}, 8));
assertEquals(0, isNUnique(new int[] {7, 3, 3, 2, 4}, 4));
assertEquals(1, isNUnique(new int[] {7, 3, 2, 4}, 5));
}
int isNUnique(int[ ] a, int n) {
if(a == null && a.length < 2)
return 0;
int nUnique = 0;
for (int i = 0; i < a.length-1; i++) {
for (int j = i+1; j < a.length; j++) {
if(a[i] + a[j] == n)
nUnique++;
}
}
if (nUnique == 1) {
return 1;
} else {
return 0;
}
}
}