+ 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;

13th Oct 2019, 4:38 PM
Aadarsh Gupta
Aadarsh Gupta - avatar
3 Réponses
+ 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;
13th Oct 2019, 5:22 PM
Diego
Diego - avatar
+ 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;
13th Oct 2019, 5:25 PM
Aadarsh Gupta
Aadarsh Gupta - avatar
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
13th Oct 2019, 5:29 PM
Aadarsh Gupta
Aadarsh Gupta - avatar