Skip to content

Commit 4661432

Browse files
committed
updated algorithm
updated algorithm
1 parent 19f096b commit 4661432

File tree

1 file changed

+25
-32
lines changed

1 file changed

+25
-32
lines changed

java-combinations/Combination.java

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
package com.hmkcode.baloot;
1+
package com.hmkcode;
22

33

44

55
public class Combination {
66

77

88
public static void main(String[] args){
9-
Object[] elements = new Object[] {'A','B','C','D','E', 'F', 'G', 'H'};
10-
11-
combination(elements,7);
9+
Object[] elements = new Object[] {'A','B','C','D','E'};
10+
combination(elements,3);
1211
}
1312

1413

@@ -22,55 +21,49 @@ public static void combination(Object[] elements, int K){
2221
System.out.println("Invalid input, K > N");
2322
return;
2423
}
24+
2525
// calculate the possible combinations
26-
// e.g. c(4,2)
2726
c(N,K);
2827

29-
// get the combination by index
30-
// e.g. 01 --> AB , 23 --> CD
28+
// init combination index array
3129
int combination[] = new int[K];
3230

33-
// position of current index
34-
// if (r = 1) r*
35-
// index ==> 0 | 1 | 2
36-
// element ==> A | B | C
37-
int r = 0;
38-
int index = 0;
31+
32+
int r = 0; // index for combination array
33+
int i = 0; // index for elements array
3934

4035
while(r >= 0){
41-
// possible indexes for 1st position "r=0" are "0,1,2" --> "A,B,C"
42-
// possible indexes for 2nd position "r=1" are "1,2,3" --> "B,C,D"
43-
44-
// for r = 0 ==> index < (4+ (0 - 2)) = 2
45-
if(index <= (N + (r - K))){
46-
combination[r] = index;
36+
37+
// forward step if i < (N + (r-K))
38+
if(i <= (N + (r - K))){
39+
combination[r] = i;
4740

48-
// if we are at the last position print and increase the index
41+
// if combination array is full print and increment i;
4942
if(r == K-1){
50-
51-
//do something with the combination e.g. add to list or print
5243
print(combination, elements);
53-
index++;
44+
i++;
5445
}
5546
else{
56-
// select index for next position
57-
index = combination[r]+1;
47+
// if combination is not full yet, select next element
48+
i = combination[r]+1;
5849
r++;
5950
}
51+
6052
}
53+
54+
// backward step
6155
else{
6256
r--;
63-
if(r > 0)
64-
index = combination[r]+1;
65-
else
66-
index = combination[0]+1;
57+
if(r >= 0)
58+
i = combination[r]+1;
59+
6760
}
6861
}
6962
}
7063

7164

7265

73-
public static int c(int n, int r){
66+
private static int c(int n, int r){
7467
int nf=fact(n);
7568
int rf=fact(r);
7669
int nrf=fact(n-r);
@@ -82,7 +75,7 @@ public static int c(int n, int r){
8275
return ncr;
8376
}
8477

85-
public static int fact(int n)
78+
private static int fact(int n)
8679
{
8780
if(n == 0)
8881
return 1;
@@ -91,7 +84,7 @@ public static int fact(int n)
9184
}
9285

9386

94-
public static void print(int[] combination, Object[] elements){
87+
private static void print(int[] combination, Object[] elements){
9588

9689
String output = "";
9790
for(int z = 0 ; z < combination.length;z++){

0 commit comments

Comments
 (0)