-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubSequenceInArray.java
90 lines (81 loc) · 2.89 KB
/
SubSequenceInArray.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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package ArrayBasedProblems;
import java.util.ArrayList;
import java.util.Arrays;
public class SubSequenceInArray {
private static void bitSubSequence() { // time complexity - o(2^n)
int a[] = { 1, 2, 3 }, n = a.length, total = (1 << n) - 1;
System.out.println("Total number of subSequence :" + total);
for (int i = 1; i <= total; i++) {
int target = i, j = 0;
String b = "";
while (target != 0) {
if ((target & 1) == 1) {
b += String.valueOf(a[j]) + " ";
}
j++;
target >>= 1;
}
System.out.println(b);
}
}
private static void listSubSequence() { // time complexity - o(n*2^n) duplication not allow
int nums[] = { 1, 2, 3 };
ArrayList<ArrayList<Integer>> outer = new ArrayList<>();
outer.add(new ArrayList<>());
for (int i : nums) {
int n = outer.size();
for (int j = 0; j < n; j++) {
ArrayList<Integer> inter = new ArrayList<>(outer.get(j));
inter.add(i);
outer.add(inter);
}
}
System.out.println(outer);
}
private static void listSubSequenceDuplicate() { // time complexity - o(n*2^n) with duplication allow
int nums[] = { 1, 2, 2 };
Arrays.sort(nums);
ArrayList<ArrayList<Integer>> outer = new ArrayList<>();
outer.add(new ArrayList<>());
int start = 0, end = 0;
for (int i = 0; i < nums.length; i++) {
start = 0;
if (i > 0 && nums[i] == nums[i - 1])
start = end + 1;
end = outer.size() - 1;
int n = outer.size();
for (int j = 0; j < n; j++) {
ArrayList<Integer> inter = new ArrayList<>(outer.get(j));
inter.add(nums[i]);
outer.add(inter);
}
}
System.out.println(outer);
}
private static void targetSumSubSequence() {
int a[] = { 5, 3, 1, 6, 2 }, sumOfSquence = 6, n = a.length, total = (1 << n) - 1, count = 0;
System.out.println("Total number of subSequence :" + total);
for (int i = 1; i <= total; i++) {
int target = i, sum = 0, j = 0;
String b = "";
while (target != 0) {
if ((target & 1) == 1) {
sum += a[j];
b += String.valueOf(a[j]) + " ";
}
j++;
target >>= 1;
}
if (sum == sumOfSquence) {
System.out.println(b);
count++;
}
}
System.out.println("Number of Subsequence equal to sumOfSquence Given :" + count);
}
public static void main(String[] args) {
bitSubSequence();
listSubSequence();
listSubSequenceDuplicate();
}
}