Can someone please tell how to generate n-bit strings(all possible combinations) i.e. counting bits form 0 to 2^n-1 using Divide and Conquer Approach.
I was able to do this with the following algorithm, but the space complexity as well as time complexity are of O(2^n). Can someone give me a better algorithm (using Divide and conquer) which requires less space than this.
ArrayList generate(int n)
{
if(n==1)
{
Create an arrayList and store strings "0" and "1";
}
else
{
generate(n-1)
now recompose the solution to get 2^n strings and return this arraylist
"PROBLEM here is that the length of the array list is also getting
exponential"
}
}
I believe you are misunderstood. Generating a bit string is not a problem on its own, so you can not propose a solution for it. Perhaps you left out a part of the problem. For instance you might define a representation of a solution using a bit string and then try to find the optimal bit string for a given problem.
One more thing, in general the time complexity of a problem represented as an n-bit string is always O(2^n) unless the problem is defined. Then you can use problem's criteria for reducing the either complexity. But before the problem is defined, generating and traversing an n-bit string always requires you to tend to each and every single possible 2^n combination.
EDIT:
Here's a pseudocode for divide & conquer, if you must:
solution breakdown(problem p)
{
if (smallenough(p))
{
return solve(p);
}
problem[] subproblems = divide(p);
solution[] subsolutions;
for (i=0; i<count(subproblems); i++)
{
subsolutions[i] = breakdown(subproblems[i]);
}
return reconstruct(subsolutions);
}