Skip to content

Commit 7e9f1b6

Browse files
Fix listSubSequenceDuplicate method to remove duplication and update comments
1 parent 6fab6de commit 7e9f1b6

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

ArrayBasedProblems/SubSequenceInArray.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private static void listSubSequence() { // time complexity - o(n*2^n) duplicatio
3838
System.out.println(outer);
3939
}
4040

41-
private static void listSubSequenceDuplicate() { // time complexity - o(n*2^n) with duplication allow
41+
private static void listSubSequenceDuplicate() { // time complexity - o(n*2^n) remove duplication
4242
int nums[] = { 1, 2, 2 };
4343
Arrays.sort(nums);
4444
ArrayList<ArrayList<Integer>> outer = new ArrayList<>();
@@ -50,14 +50,25 @@ private static void listSubSequenceDuplicate() { // time complexity - o(n*2^n) w
5050
start = end + 1;
5151
end = outer.size() - 1;
5252
int n = outer.size();
53-
for (int j = 0; j < n; j++) {
53+
for (int j = start; j < n; j++) {
5454
ArrayList<Integer> inter = new ArrayList<>(outer.get(j));
5555
inter.add(nums[i]);
5656
outer.add(inter);
5757
}
5858
}
5959
System.out.println(outer);
6060
}
61+
62+
// [[], [1], [2], [1, 2], [2], [1, 2], [2, 2], [1, 2, 2] ] with duplication [2] [1,2]
63+
// Remove duplicate from above list
64+
65+
// if we have duplicate element initially we need to sort the array first.
66+
// [[]]
67+
// [[],[1]]
68+
// [[],[1],[2],[1, 2]]
69+
// Add new element [2] with new added element in list [2], [1, 2]
70+
// [[],[1],[2],[1,2],[2,2],[1,2,2]]
71+
6172

6273
private static void targetSumSubSequence() {
6374
int a[] = { 5, 3, 1, 6, 2 }, sumOfSquence = 6, n = a.length, total = (1 << n) - 1, count = 0;

0 commit comments

Comments
 (0)