-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReturnSubsetOfAnArray.java
More file actions
70 lines (58 loc) · 1.99 KB
/
ReturnSubsetOfAnArray.java
File metadata and controls
70 lines (58 loc) · 1.99 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
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
// Return subset of an array
// Send Feedback
// Given an integer array (of length n), find and return all the subsets of
// input array.
// NOTE- Subsets are of length varying from 0 to n, that contain elements of the
// array. But the order of elements should remain same as in the input array.
// Note :
// The order of subsets are not important.
// Input format :
// Line 1 : Size of array
// Line 2 : Array elements (separated by space)
// Expected Time Complexity: O(2^N), where N is the size of given array
// Sample Input:
// 3
// 15 20 12
// Sample Output:
// [] (this just represents an empty array, don't worry about the square
// brackets)
// 12
// 20
// 20 12
// 15
// 15 12
// 15 20
// 15 20 12
import java.util.ArrayList;
public class solution {
// Return a 2D array that contains all the subsets
public static int[][] subsets(int input[]) {
// Write your code here
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
ArrayList<Integer> current = new ArrayList<>();
helper(input, 0, current, result);
// Converting the result arraylist of arraylists to the 2d array ans.
int ans[][] = new int[result.size()][];
for (int i = 0; i < result.size(); i++) {
ArrayList<Integer> subset = result.get(i);
ans[i] = new int[subset.size()];
for (int j = 0; j < subset.size(); j++) {
ans[i][j] = subset.get(j);
}
}
return ans;
}
public static void helper(int input[], int index, ArrayList<Integer> current,
ArrayList<ArrayList<Integer>> result) {
if (index == input.length) {
result.add(new ArrayList<>(current));
return;
}
// Including the element and calling helper.
current.add(input[index]);
helper(input, index + 1, current, result);
// Excluding the element and calling helper
current.remove(current.size() - 1);
helper(input, index + 1, current, result);
}
}