-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCombinations.java
More file actions
31 lines (25 loc) · 797 Bytes
/
Combinations.java
File metadata and controls
31 lines (25 loc) · 797 Bytes
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
import java.util.Arrays;
import java.util.Scanner;
public class Combinations {
private static void combine(int[] nums, int count, int size, int start, String comb) {
if (count == size) {
System.out.println(comb);
} else {
for (int i = start; i < nums.length; i++) {
combine(nums, count + 1, size, i + 1, comb + nums[i] + " ");
}
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = input.nextInt();
}
Arrays.sort(nums);
int k = input.nextInt();
input.close();
combine(nums, 0, k, 0, "");
}
}