- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsequences.java
More file actions
Latest commit
45 lines (37 loc) · 1.48 KB
/
Copy pathSubsequences.java
File metadata and controls
45 lines (37 loc) · 1.48 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
packageRecursionAndBacktracking;
importjava.util.ArrayList;
importjava.util.Scanner;
publicclassSubsequences {
publicstaticvoidprintSubsequences(intindex, int[] array, intsize, ArrayList<Integer> subsequences){
if(index == size){
for(intele : subsequences){
System.out.print(ele + " ");
}
if(subsequences.size() == 0){ // condition for null set
System.out.println("Null set");
}
System.out.println();
return;
}
subsequences.add(array[index]);
printSubsequences(index+1, array, size, subsequences); // pick
subsequences.remove(subsequences.size()-1);
printSubsequences(index+1, array, size, subsequences); // not pick
}
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
System.out.println("Enter the number of test cases:");
intt = sc.nextInt();
while (t-- > 0){
System.out.println("Enter the size of the array:");
intsize = sc.nextInt();
int[] array = newint[size];
System.out.println("Enter the elements of the array:");
for(inti=0 ; i<size ; i++){
array[i] = sc.nextInt();
}
ArrayList<Integer> subsequences = newArrayList<>();
printSubsequences(0, array, size, subsequences);
}
}
}