Created
December 23, 2017 18:39
-
-
Save thmain/c880f2d9809cedae822fa0935bd9f328 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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