Skip to content

Instantly share code, notes, and snippets.

@thmain
Created February 24, 2016 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/b574c5f0fc29c7698d65 to your computer and use it in GitHub Desktop.
Save thmain/b574c5f0fc29c7698d65 to your computer and use it in GitHub Desktop.
public class AllSubSetOfSizeK {
public void subset(int[] A, int k, int start, int currLen, boolean[] used) {
if (currLen == k) {
for (int i = 0; i < A.length; i++) {
if (used[i] == true) {
System.out.print(A[i] + " ");
}
}
System.out.println();
return;
}
if (start == A.length) {
return;
}
// For every index we have two options,
// 1.. Either we select it, means put true in used[] and make currLen+1
used[start] = true;
subset(A, k, start + 1, currLen + 1, used);
// 2.. OR we dont select it, means put false in used[] and dont increase
// currLen
used[start] = false;
subset(A, k, start + 1, currLen, used);
}
public static void main(String[] args) {
int A[] = { 1, 2, 3, 4, 5 };
boolean[] B = new boolean[A.length];
AllSubSetOfSizeK i = new AllSubSetOfSizeK();
i.subset(A, 3, 0, 0, B);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment