For(int i=1;i<=n;i=i*2){
For(j=1;j<=i;j=j*2){
Sum=sum+i;
Sum=sum*R(n/2);
}
}
First of all recursive call happens inside the inner loop, so solving will be difficult. Still, lets try.
Outer loop runs $\log_2 n$ times and inner loop runs $\log_2 i$ times for each $i$ and $i$ is multiplied by $2$ after each iteration. So, the no. of times the inner loop runs will be
$\lg 1 + \lg 2 + \lg 4 + \dots + \lg n \\= \lg \left(1.2.4.\dots. n\right) \\=\lg\left(2^0 . 2^1 . 2^2 . \dots 2^{\lg n}\right)
\\=\lg \left[ 2^{ \left ( 0 + 1 + \dots + \lg n\right)} \right]
\\= \frac {(\lg n). (\lg n + 1)}{2} $
So, our recurrence relation will be
$T(n) = \frac{ (\lg n) .(\lg n + 1)}{2} T(n/2) + c$
Now, lets try substitution assuming $c = 1$.
T(1) = 1
T(2) = 2
T(4) = 6
T(8) = 36
T(16) = 360
T(32) = 5400
I suppose all choices should be eliminated by now - the growth rate is higher than that of $n^2$.
#include<stdio.h>
int count = 0;
int R(int n) {
if(n<=1) {
count++;
return 1;
}
else {
int sum=0,i,j;
for(i=1;i<=n;i=i*2)
for(j=1;j<=i;j=j*2){
sum=sum+i;
count++;
sum=sum*R(n/2);
}
return sum;
}
}
int main(int argc, char* argv[]) {
R(atoi(argv[1]));
printf("count = %d\n", count);
}