- Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathArrayChallenge.java
56 lines (40 loc) · 1.31 KB
/
ArrayChallenge.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
importjava.util.*;
importjava.io.*;
classMain {
publicstaticbooleanArrayChallenge(int[] arr) {
ArrayList<Integer> list = newArrayList<Integer>();
for(inti: arr){
list.add(i);
}
intmaxNumber = list.get(0);
for(inti = 1;i<list.size();i++){
if(list.get(i)>maxNumber)
maxNumber= list.get(i);
}
list.remove(Integer.valueOf(maxNumber));
intn = list.size();
for (inti = 0; i < (1<<n); i++)
{
intsum = 0;
// Print current subset
for (intj = 0; j < n; j++)
// (1<<j) is a number with jth bit 1
// so when we 'and' them with the
// subset number we get which numbers
// are present in the subset and which
// are not
if ((i & (1 << j)) > 0){
sum+=list.get(j);
}
if(sum == maxNumber){
returntrue;
}
}
returnfalse;
}
publicstaticvoidmain (String[] args) {
// keep this function call here
Scanners = newScanner(System.in);
System.out.print(ArrayChallenge(s.nextLine()));
}
}