forked from avinashbest/java-coding-ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsetOfArray.java
More file actions
76 lines (69 loc) · 2.31 KB
/
Copy pathSubsetOfArray.java
File metadata and controls
76 lines (69 loc) · 2.31 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
71
72
73
74
75
76
package recursion3.Assignment;
import java.util.Scanner;
/*Given an integer array (of length n), find and return all the subsets of input array.
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)
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 */
public class SubsetOfArray {
public static int[][] returnSubsetOfArray(int[] arr, int startIndex) {
/*we are returning a jagged 2d-array*/
if (startIndex == arr.length) {
return new int[1][0];
}
int[][] smallOutput = returnSubsetOfArray(arr, startIndex + 1);
int[][] output = new int[2 * smallOutput.length][];
int k = 0;
for (int i = 0; i < smallOutput.length; i++) {
output[k] = new int[smallOutput.length];
/*copying smallerOutput array in output array*/
for (int j = 0; j < smallOutput[i].length; j++) {
output[k][j] = smallOutput[i][j];
}
k++;
}
for (int i = 0; i < smallOutput.length; i++) {
output[k] = new int[smallOutput.length + 1];
output[k][0] = arr[startIndex];
for (int j = 1; j < smallOutput[i].length; j++) {
output[k][j] = smallOutput[i][j - 1];
}
k++;
}
return output;
}
public static int[][] returnSubsetOfArray(int[] arr) {
return returnSubsetOfArray(arr, 0);
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int size = scan.nextInt();
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = scan.nextInt();
}
int[][] result = returnSubsetOfArray(arr);
printArray(result);
}
private static void printArray(int[][] result) {
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[0].length; j++) {
System.out.print(result[i][j] + "\t");
}
System.out.println();
}
}
}