+ 1
What is the TIME COMPLEXITY of the code given below -
for (int i=1; i< n/4 ;i++) for (int j=1;j<n/4;j++) sum = sum +1;
3 ответов
+ 2
It's O(n^2), but it can be taken down to O(1) using this formula:
if (n>7)
sum = (n/4-1)*(n-4-1);
else
sum = 0;
+ 1
Then what about this
for (int i=1; i< 2*n ;i++)
for (int j=1;j<n*n /2;j*=2)
sum = sum +1;
0
The outer for loop will execute n times and inner also so it will become O (n^2) but what happens when we divide or add by some constant such as n*n/10