int fun(int n)
{
int i,j,s=0;
for(i=1; i<=n; i++)
for(j=1; j>=1; j-- );
s= s+ 1/n;
return s;
}
What is the time complexity for this program? There is a semicolon after the second for loop.
int fun(int n)
{
int i,j,s=0;
for(i=1; i<=n; i++)
for(j=1; j>=1; j-- );
s= s+ 1/n;
return s;
}
What is the time complexity for this program? There is a semicolon after the second for loop.
hello @nidhigupta847
it will be O(n) only.
outer loop will run for n times . and inner loop will run for 1 times.
so N*1 -> N
semicolon after for loop indicates that the for loop has no body, but that doesnt mean that it will not execute
Hey thanks but the correct answer is O(n^2)
have u copied this correcty?
Yuppppppppppppppppppppppppp
once check -> is the inner loop starting from n or 1.
It’s starting from 1 only.
sorry dont know why it is O(n^2) , in my knowledge it should be O(n) only.
if they put j=n in place of j=1 then only complexity will be O(n^2)
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.