forked from chicuongit913/MumTestJavaExam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilterArray.java
44 lines (36 loc) · 852 Bytes
/
filterArray.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
package mumTestPackage;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
class MumTest40 {
@Test
void test() {
System.out.println(Arrays.toString(filterArray(new int[] {9, -9} , 4)));
}
int[ ] filterArray(int[ ] a, int n) {
if (a.length == 0 || n == 0) {
return new int[] {};
}
int i = 0;
int count = 0;
int[] representation = new int[a.length + 1];
while ( n > 0 && i < a.length+1) {
if(n%2 != 0)
{
representation[i] = count;
i++;
}
count++;
n /=2;
}
System.out.println(Arrays.toString(representation));
System.out.println(representation[i-1]);
if(representation[i-1] >= a.length)
return null;
int[] tempArr = new int[i];
for (int j = 0; j < i; j++) {
tempArr[j] = a[representation[j]];
}
return tempArr;
}
}