0 votes 0 votes What is the time complexity of the following code? int j = 0; for(i=0;i<n;i++) { for(i=0;i<2n;i++) { while(j<n) { j++; } } } a) O(n$^{4}$) b) O(n$^{3}$) c) O(n$^{2}$) d) O(n) I am getting option O(n$^{2}$) and answer is given O(n) Explain briefly. Algorithms time-complexity + – Siddharth Bhardawaj asked Aug 5, 2018 • edited Aug 5, 2018 by srestha Siddharth Bhardawaj 1.9k views answer comment Share Follow See all 13 Comments See all 13 13 Comments reply Show 10 previous comments Soumya29 commented Aug 5, 2018 reply Follow Share @Na462 @srestha Yes, It's $O(n^2)$ :) 2 votes 2 votes srestha commented Aug 18, 2018 reply Follow Share @Soumya here TC of j is O(1) right? 0 votes 0 votes nephron commented Oct 31, 2018 reply Follow Share srestha Soumya29 Both of them are taking variable 'i' but it is not a global variable. It is local within its scope. So, the complexity should be O(n2) 0 votes 0 votes Please log in or register to add a comment.
0 votes 0 votes o(n²) is correct Psy Duck answered Nov 7, 2022 Psy Duck comment Share Follow See all 0 reply Please log in or register to add a comment.