-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
53 lines (41 loc) · 1.46 KB
/
Main.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
public class Main {
public static void main(String[] args) {
// System.out.println("Hello world!");
int[] n = {1,3,5,4};
Main obj = new Main();
int k= obj.combinationSum(n);
System.out.println(k);
}
// public int combinationSum(int[] num){
// int total = 0;
// for(int i=0; i<n.length-2;i++){
// total= total +n[i];
// System.out.println(n[i] + ", total" + total);
// for(int j = i+1; j<n.length-1;j++){
//
// total = total + n[i] + n[j] ;
// // total = total + n[j] ;
// System.out.println("from 2nd loop" +n[i]+" + "+ n[j]+ " total" + total);
//
// for (int k =j+1;k<n.length;k++){
// total = total + n[i] + n[j] +n[k];
// System.out.println("from 2nd loop" +n[i]+" + "+ n[j]+ "+" + n[k] + "+ " + " total" + total);
//
// }
// }
// }
public int combinationSum(int[] num){
int total = 0;
for(int i =0;i<=num.length;i++){
int k = 0;
// total= total+num[i];
for(int j=i;j<num.length;j++){
total = total+k+num[j];
k = k +num[j];
System.out.println("i is" + num[i] + " , j is" + +num[j] + "current total" + total);
}
System.out.println("total is " + total);
}
return total ;
}
}