Suppose my input array is [15,20,12]
The required answer is a 2D array
The Required is output is as followed
[12
20
20 12
15
15 12
15 20
15 20 12
]
Asked
Active
Viewed 2,307 times
0

Sarthak
- 188
- 1
- 2
- 14
-
Is input array assumed to contain no duplicates? If it contains duplicates, must they appear in the output? For example, what is output for `[15,15]` input - is it `[[],[15],[15,15]]` or `[[],[15],[15],[15,15]]` ? – Tomáš Záluský Feb 23 '20 at 14:01
-
Sorry,we dont want empty array in answer. for input=[15, 15] the output is expected to be [[15],[15],[15,15]] @TomášZáluský – Sarthak Feb 23 '20 at 14:37
-
what you mean by _we dont want empty array in answer_ ? - this contradicts with original question where you state it as expected part of output – Tomáš Záluský Feb 23 '20 at 14:40
-
Yes, i am sorry for that error. Actually the question was described like this. On executing the sample test case, it was not showing empty array. Sorry again. I have edited the question. – Sarthak Feb 23 '20 at 14:47
-
@Sarthak - If one of the answers resolved your issue, you can help the community by marking it as accepted. An accepted answer helps future visitors use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. – Arvind Kumar Avinash May 24 '20 at 11:07
4 Answers
1
Here you go.
public static void main(String[] args) {
int[] nums= {15, 20, 12};
int[][] subsets = subsets(nums);
for (int i = 1; i < subsets.length; i++) {
System.out.println(Arrays.toString(subsets[i]));
}
}
public static int[][] subsets(int input[]) {
List<List<Integer>> list = new ArrayList<>();
subsetsHelper(list, new ArrayList<>(), input, 0);
return convertListToArray(list);
}
private static void subsetsHelper(List<List<Integer>> list , List<Integer> resultList, int [] nums, int start){
list.add(new ArrayList<>(resultList));
for(int i = start; i < nums.length; i++){
// add element
resultList.add(nums[i]);
// Explore
subsetsHelper(list, resultList, nums, i + 1);
// remove
resultList.remove(resultList.size() - 1);
}
}
private static int[][] convertListToArray(List<List<Integer>> list) {
int[][] array = new int[list.size()][];
for (int i = 0; i < array.length; i++) {
array[i] = new int[list.get(i).size()];
}
for(int i=0; i<list.size(); i++){
for (int j = 0; j < list.get(i).size(); j++) {
array[i][j] = list.get(i).get(j);
}
}
return array;
}
1.As each recursion call will represent subset here, add resultList(see recursion code above) to the list of subsets in each call. 2.Iterate over elements of a set. 3.In each iteration Add elements to the list explore(recursion) and make start = i+1 to go through remaining elements of the array. Remove element from the list
Output:
[15]
[15, 20]
[15, 20, 12]
[15, 12]
[20]
[20, 12]
[12]

MOnkey
- 751
- 6
- 13
-
@Sarthak, Could you please check my answer and accept and upvote it, if it will work for you. – MOnkey Feb 23 '20 at 14:43
-
Hey, the function is pre defined as public static int[][] subsets(int input[]). – Sarthak Feb 23 '20 at 14:44
1
Not clear if it's homework or practical case. This is how would I solve it using Guava PowerSet:
public static void main(String[] args) {
Integer input[] = {15,20,12};
List<Integer> rev = Lists.reverse(Arrays.asList(input));
Set<Integer> indices = IntStream.range(0, input.length).boxed().collect(ImmutableSet.toImmutableSet());
Object output[] = Sets.powerSet(indices).stream()
.filter(indexset -> !indexset.isEmpty())
.map(indexset -> indexset.stream().map(i -> rev.get(i)).collect(Collectors.collectingAndThen(toList(), Lists::reverse)))
.map(List::toArray)
.toArray();
System.out.println(Arrays.deepToString(output));
}

Tomáš Záluský
- 10,735
- 2
- 36
- 64
1
Disclaimer:
- This is my original work. No part of the solution has been copied from anywhere.
- My solution works perfectly for 3 elements. However, this needs to be improved to work for arrays of other sizes. Despite this, I am publishing it so that OP or anyone else can extend this solution to work for an array of any size.
- This question is close to the power set except for the fact that a power set can not have duplicate elements. If this exception is removed from this question, there are many solutions available e.g. at 1, 2, 3 etc.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr = { 15, 20, 12 };
System.out.println(Arrays.deepToString(subsets(arr)));
}
public static int[][] subsets(int input[]) {
int[][] subarrs = new int[(int) Math.pow(2, input.length) - 1][input.length];
int[] indices = { 0 };
subsetsHelper(input, subarrs, 0, 0, 0, indices);
return subarrs;
}
private static void subsetsHelper(int input[], int[][] subarrs, int index, int i, int j, int[] indices) {
if (i == input.length) {
subarrs[index] = input;
return;
}
int[] subarr = new int[indices.length];
for (int x = 0; x < subarr.length; x++) {
subarr[x] = input[indices[x]];
}
subarrs[index] = subarr;
if (j == input.length - 1) {
subsetsHelper(input, subarrs, index + 1, i + 1, i + 1, new int[] { i + 1 });
} else {
subsetsHelper(input, subarrs, index + 1, i, j + 1, new int[] { i, j + 1 });
}
}
}
Output:
[[15], [15, 20], [15, 12], [20], [20, 12], [12], [15, 20, 12]]

Arvind Kumar Avinash
- 71,965
- 6
- 74
- 110
0
public static int[][]returnallsub(int arr[], int si){
if(si==arr.length)
{int[][]ret=new int [1][0];
return ret;
}
int [][]rss =returnallsub(arr,si+1);
int [][]tss=new int[rss.length*2][];
int i=0;
for(;i<rss.length;i++) {
tss[i]=new int [rss[i].length];
}
int k=0;
for(;k<rss.length;k++) {
tss[i]=new int [rss[k].length+1];
i++;
}
int j=0;
for(i=0;i<rss.length;i++) {
for(j=0;j<rss[i].length;j++){
tss[i][j]=rss[i][j];
}
}
for(k=i;k<tss.length;k++) {
tss[k][0]=arr[si];
}
int r=i;
for(i=0;i<rss.length;i++) {
for(j=0;j<rss[i].length;j++){
tss[r][j+1]=rss[i][j];
}
r++;
}
return tss;
}
public static int[][] subsets(int arr[]) {
int start=0;
return returnallsub( arr,0);
}
}

Basit Hasan
- 1
- 1