You have been given an integer n that corresponds to the number of bits in an array. the task is to print all the possible strings using those n bits. the array can consist only boolean values (i.e either 0 or 1)
We will be solving this problem using recursion.
public class AllStringsNBits {
public static void main(String[] args) {
int n = 5;
int[] arr = new int[n];
GenerateAllStringOfNBits(n, arr);
}
private static void GenerateAllStringOfNBits(int n,int[] arr) {
if(n<1) {
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]);
}
System.out.println();
}
else {
arr[n-1]=0;
GenerateAllStringOfNBits(n-1, arr);
arr[n-1]=1;
GenerateAllStringOfNBits(n-1, arr);
}
}
}
The time complexity for the above solution is O(2^n)



