How many times is the below loop executed?
for ( int i=0; i < n; i++)
{ for ( int j=0; j < (2*i); j+=(i/2))
{
cout<< "Hello Geeks" ;
}
} |
(A) O(n)
(B) Infinite times
(C) O(n2)
(D) O(nlogn)
Answer: (B)
Explanation: At the second iteration of the outer loop, i.e. when i = 1, the inner loop will become an infinite loop as the increment condition is j = j+(i/2) and for i = 1, i/2 = 0.
Quiz of this Question
Article Tags :
Recommended Articles