m=1;
for(i=1;i<=n;i++){
m=m*2;
for(j=1;j<=m;j++){
do something that is O(1)
}
}
What will be time complexity of the above code ?? Please tell me how to solve these types of problem.
Inner loop will iterate 1 time, then 2 times, then ..., then 2^n times. So we have 1 + 2 + 4 + ... + 2^n = 2^(n + 1) - 1 = O(2^n) iterations of inner loop.
One iteration of inner loop has constant complexity, so summed_inner_loop_complexity = O(2^n).
Whole complexity is O(2^n).
n, or get the summed inner loop complexity (better), but both means you're over counting. The answer should be O(2^n).