|
1 | 1 | package ArrayBasedProblems; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | + |
2 | 5 | public class SubSequenceInArray { |
3 | | - public static void main(String[] args) { |
4 | | - int a[]={5,3,1,6,2},sumOfSquence=6,n=a.length,total=(1<<n)-1,count=0; |
5 | | - System.out.println("Total number of subSequence :"+total); |
6 | | - |
7 | | - for(int i=1;i<=total;i++){ |
8 | | - int target=i,sum=0,j=0; |
9 | | - String b=""; |
10 | | - while(target!=0){ |
11 | | - if((target&1)==1){ |
12 | | - sum+=a[j]; |
13 | | - b+=String.valueOf(a[j])+" "; |
| 6 | + |
| 7 | + private static void bitSubSequence() { // time complexity - o(2^n) |
| 8 | + int a[] = { 1, 2, 3 }, n = a.length, total = (1 << n) - 1; |
| 9 | + System.out.println("Total number of subSequence :" + total); |
| 10 | + |
| 11 | + for (int i = 1; i <= total; i++) { |
| 12 | + int target = i, j = 0; |
| 13 | + String b = ""; |
| 14 | + while (target != 0) { |
| 15 | + if ((target & 1) == 1) { |
| 16 | + b += String.valueOf(a[j]) + " "; |
| 17 | + } |
| 18 | + j++; |
| 19 | + target >>= 1; |
| 20 | + } |
| 21 | + System.out.println(b); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + private static void listSubSequence() { // time complexity - o(n*2^n) duplication not allow |
| 26 | + int nums[] = { 1, 2, 3 }; |
| 27 | + ArrayList<ArrayList<Integer>> outer = new ArrayList<>(); |
| 28 | + outer.add(new ArrayList<>()); |
| 29 | + for (int i : nums) { |
| 30 | + int n = outer.size(); |
| 31 | + for (int j = 0; j < n; j++) { |
| 32 | + ArrayList<Integer> inter = new ArrayList<>(outer.get(j)); |
| 33 | + inter.add(i); |
| 34 | + outer.add(inter); |
| 35 | + } |
| 36 | + } |
| 37 | + System.out.println(outer); |
| 38 | + } |
| 39 | + |
| 40 | + private static void listSubSequenceDuplicate() { // time complexity - o(n*2^n) with duplication allow |
| 41 | + int nums[] = { 1, 2, 2 }; |
| 42 | + ArrayList<ArrayList<Integer>> outer = new ArrayList<>(); |
| 43 | + outer.add(new ArrayList<>()); |
| 44 | + int start = 0, end = 0; |
| 45 | + for (int i = 0; i < nums.length; i++) { |
| 46 | + start = 0; |
| 47 | + if (i > 0 && nums[i] == nums[i - 1]) |
| 48 | + start = end + 1; |
| 49 | + end = outer.size() - 1; |
| 50 | + int n = outer.size(); |
| 51 | + for (int j = 0; j < n; j++) { |
| 52 | + ArrayList<Integer> inter = new ArrayList<>(outer.get(j)); |
| 53 | + inter.add(nums[i]); |
| 54 | + outer.add(inter); |
| 55 | + } |
| 56 | + } |
| 57 | + System.out.println(outer); |
| 58 | + } |
| 59 | + |
| 60 | + private static void targetSumSubSequence() { |
| 61 | + int a[] = { 5, 3, 1, 6, 2 }, sumOfSquence = 6, n = a.length, total = (1 << n) - 1, count = 0; |
| 62 | + System.out.println("Total number of subSequence :" + total); |
| 63 | + |
| 64 | + for (int i = 1; i <= total; i++) { |
| 65 | + int target = i, sum = 0, j = 0; |
| 66 | + String b = ""; |
| 67 | + while (target != 0) { |
| 68 | + if ((target & 1) == 1) { |
| 69 | + sum += a[j]; |
| 70 | + b += String.valueOf(a[j]) + " "; |
14 | 71 | } |
15 | 72 | j++; |
16 | | - target>>=1; |
| 73 | + target >>= 1; |
17 | 74 | } |
18 | | - if(sum==sumOfSquence){ |
| 75 | + if (sum == sumOfSquence) { |
19 | 76 | System.out.println(b); |
20 | 77 | count++; |
21 | 78 | } |
22 | 79 | } |
23 | | - System.out.println("Number of Subsequence equal to sumOfSquence Given :"+count); |
| 80 | + System.out.println("Number of Subsequence equal to sumOfSquence Given :" + count); |
| 81 | + } |
| 82 | + |
| 83 | + public static void main(String[] args) { |
| 84 | + bitSubSequence(); |
| 85 | + listSubSequence(); |
| 86 | + listSubSequenceDuplicate(); |
24 | 87 | } |
25 | 88 | } |
0 commit comments