-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsequences.java
More file actions
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
package RecursionAndBacktracking;
import java.util.ArrayList;
import java.util.Scanner;
public class Subsequences {
public static void printSubsequences(int index, int[] array, int size, ArrayList<Integer> subsequences){
if(index == size){
for(int ele : 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
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of test cases:");
int t = sc.nextInt();
while (t-- > 0){
System.out.println("Enter the size of the array:");
int size = sc.nextInt();
int[] array = new int[size];
System.out.println("Enter the elements of the array:");
for(int i=0 ; i<size ; i++){
array[i] = sc.nextInt();
}
ArrayList<Integer> subsequences = new ArrayList<>();
printSubsequences(0, array, size, subsequences);
}
}
}