-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZero.java
More file actions
74 lines (66 loc) · 1.82 KB
/
Zero.java
File metadata and controls
74 lines (66 loc) · 1.82 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Zero.java
*
* Version:
* 1
*
*/
/**
* This program can determine if given a set of numbers
* has a subset that sums to zero.
*
* @author Ishika Prasad
*/
public class Zero {
static int[] testSet = {-1, -1, 2, 5, 6};
/**
* This method determines all possible combinations
* of the given set and print subset which sums to zero.
* Otherwise, print statement which shows subset doesn't exist.
*
*/
public static void checkSet(int[] testSet) {
int combinations, sum, flag=0, n, indexTS;
for(combinations = 1; combinations < 32; combinations++)
{
n = combinations;
indexTS = 0;
sum = 0;
while (n > 0) {
sum = sum + testSet[indexTS] * (n % 2);
indexTS++;
n /= 2;
}
if (sum == 0) {
flag = 1;
System.out.print("Found subset that sums to zero: ");
if(testSet[indexTS-1] == 0) {
System.out.print(testSet[indexTS-1]);
}
else {
n = combinations;
indexTS = 0;
while (n > 0) {
if (testSet[indexTS] * (n % 2) != 0)
System.out.print(testSet[indexTS] + " ");
indexTS++;
n /= 2;
}
}
System.out.println();
}
if (flag == 1)
break;
}
if (flag != 1)
System.out.println("Unable to find subset that sums to zero");
}
/**
* The main program.
*
*@param args command line arguments (ignored)
*/
public static void main(String args[]) {
checkSet(testSet);
}
}