Advertisement
GokulDeep

Subset

Feb 26th, 2024
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1.  
  2.  
  3. public class Subset {
  4.     public static void main(String[] args) {
  5.  
  6.         Subset sub = new Subset();
  7.         int[] arr = new int[] { 1, 2, 3 };
  8.         sub.subsets(arr);
  9.     }
  10.  
  11.     List<List<Integer>> opList = new ArrayList<>();
  12.  
  13.     public List<List<Integer>> subsets(int[] nums) {
  14.         int temp[] = new int[nums.length];
  15.         printAll(nums, nums.length, 0, temp, 0);
  16.         return opList;
  17.     }
  18.  
  19.     public void printAll(int[] arr, int n, int i, int[] temp, int s) {
  20.  
  21.         if (i == n) {
  22.             List<Integer> minList = new ArrayList<>();
  23.             for (int j = 0; j < s; j++) {
  24.                 minList.add(temp[j]);
  25.             }
  26.             opList.add(minList);
  27.             return;
  28.         }
  29.         temp[s] = arr[i];
  30.  
  31.         printAll(arr, n, i + 1, temp, s + 1); // yes
  32.         printAll(arr, n, i + 1, temp, s);// no
  33.  
  34.     }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement