-
Notifications
You must be signed in to change notification settings - Fork 0
/
Combinations.java
34 lines (30 loc) · 965 Bytes
/
Combinations.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
public class Combinations
{
static void printCombinations(char[] sequence, int N)
{
char[] data = new char[N];
for (int r = 0; r < sequence.length; r++)
combinations(sequence, data, 0, N - 1, 0, r);
}
static void combinations(char[] sequence, char[] data, int start, int end,
int index, int r)
{
if (index == r)
{
for (int j = 0; j < r; j++)
System.out.print(data[j] + " ");
System.out.println();
}
for (int i = start; i <= end && ((end - i + 1) >= (r - index)); i++)
{
data[index] = sequence[i];
combinations(sequence, data, i + 1, end, index + 1, r);
}
}
public static void main(String args[])
{
char[] sequence = { 'a', 'b', 'c', 'd', 'e' };
System.out.print("The combinations are: ");
printCombinations(sequence, sequence.length);
}
}