Skip to content

Instantly share code, notes, and snippets.

@thmain
Created December 23, 2017 18:39
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/c880f2d9809cedae822fa0935bd9f328 to your computer and use it in GitHub Desktop.
Save thmain/c880f2d9809cedae822fa0935bd9f328 to your computer and use it in GitHub Desktop.
public class PrintArraySubSequences {
public void printAllSubSequences(int [] arrInput){
int [] temp = new int[arrInput.length];
int index = 0;
solve(arrInput, index, temp);
}
private void solve(int [] arrInput, int index, int [] temp){
if(index==arrInput.length){
print(arrInput,temp);
return;
}
//set the current index bit and solve it recursively
temp[index] = 1;
solve(arrInput,index+1,temp);
//unset the current index bit and solve it recursively
temp[index] = 0;
solve(arrInput,index+1,temp);
}
private void print(int [] arrInput, int [] temp){
String result = "";
for (int i = 0; i <temp.length ; i++) {
if(temp[i]==1)
result += arrInput[i]+" ";
}
if(result=="")
result = "{Empty Set}";
System.out.println(result);
}
public static void main(String[] args) {
int [] arrInput = {1, 2, 3};
new PrintArraySubSequences().printAllSubSequences(arrInput);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment