forked from hmkcode/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShifting.java
More file actions
66 lines (54 loc) · 1.45 KB
/
Shifting.java
File metadata and controls
66 lines (54 loc) · 1.45 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
package com.hmkcode;
public class Shifting
{
public static void main( String[] args )
{
String[] e = {"A","B","C","D","E"};
int k = 3;
combination(e,k);
}
public static void combination(Object[] e, int k){
int[] ignore = new int[e.length-k]; // --> [0][0]
int[] combination = new int[k]; // --> [][][]
// set initial ignored elements
//(last k elements will be ignored)
for(int w = 0; w < ignore.length; w++)
ignore[w] = e.length - k +(w+1);
int i = 0, r = 0, g = 0;
boolean terminate = false;
while(!terminate){
// selecting N-k non-ignored elements
while(i < e.length && r < k){
if(i != ignore[g]){
combination[r] = i;
r++; i++;
}
else{
if(g != ignore.length-1)
g++;
i++;
}
}
print(combination, e);
i = 0; r = 0; g = 0;
terminate = true;
// shifting ignored indices
for(int w = 0 ; w < ignore.length; w++){
if(ignore[w] > w){
ignore[w]--;
if(w > 0)
ignore[w-1] = ignore[w]-1;
terminate = false;
break;
}
}
}
}
private static void print(int[] combination, Object[] elements){
String output = "";
for(int z = 0 ; z < combination.length;z++){
output += elements[combination[z]];
}
System.out.println(output);
}
}