Algorithms Quiz | SP Contest 1 | Question 1
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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.