45 45 votes Consider the following C-program fragment in which $i$, $j$ and $n$ are integer variables. for( i = n, j = 0; i > 0; i /= 2, j +=i ); Let $val(j)$ denote the value stored in the variable $j$ after termination of the for loop. Which one of the following is true? $val(j)=\Theta(\log n)$ $val(j)=\Theta (\sqrt{n})$ $val(j)=\Theta( n)$ $val(j)=\Theta (n\log n)$ Algorithms gatecse-2006 algorithms normal time-complexity + – Rucha Shelke 30.9k views answer comment Share Follow Print See all 18 Comments 18 18 Comments reply suvasish commented Jun 28, 2017 reply Follow flag All the answers are wrong though it is selected as best answer! correct it some one . 0 0 replyShare sushmita commented Sep 6, 2017 reply Follow flag C is correct answer. 0 0 replyShare A_i_$_h commented Dec 22, 2017 reply Follow flag i did not understand step 2 here step 1 - j=n*(1/2+1/4+1/8-----1/n) step 2 - j=n*(1-1/n) can someone help 0 0 replyShare Manu Thakur commented Dec 23, 2017 reply Follow flag simplify the for loop: j=0; for( i=n; i>0; i=i/2) { j = j+i; } $j = 0 + n + n/2 + n/2^2 + n/2^3 + ................. n/2^k$ $j = n*(1 + 1/2 + 1/2^2 + 1/2^3 + .......... 1/2^k) $ $n = 2^k$ $j = n*\frac{( 1 - (1/2)^k)}{1-1/2}$ $j = n*\frac{( 1 - 1/2^k)}{1/2}$ $j = ( n - n/2^k)$ keep $n=2^k$ $j = n -1 = n$ Answer is (C). 32 32 replyShare A_i_$_h commented Dec 24, 2017 reply Follow flag @manu j=n∗(1−(1/2)k)1−1/2 what formula have you used here ? 0 0 replyShare Manu Thakur commented Dec 24, 2017 reply Follow flag it's a G.P series of k terms where r=1/2 and r <1 3 3 replyShare A_i_$_h commented Dec 24, 2017 reply Follow flag @manu shouldnt it be j=(n−n/2k) * 2 keep n=2k j= 2(n−1)=n where did that 1/2 in denominator go 1 1 replyShare iarnav commented Jan 12, 2018 reply Follow flag @Manu Thakur pC how to analyze that n=2^k? 0 0 replyShare Nitesh Singh 2 commented Jan 11, 2019 reply Follow flag If we calculate in terms of time complexity, we can do summation till infinity. sum of G.P till infinite = A/1-r A=1/2, r= 1/2 =O (n) 0 0 replyShare Shamim Ahmed commented Jan 26, 2019 reply Follow flag Its not the case of summation till infinity. 0 0 replyShare talha hashim commented Aug 17, 2019 i edited by talha hashim Aug 17, 2019 reply Follow flag It is the case of summation till infinity when i is real but it is given that i is integer so finite summation 0 0 replyShare mrinmoyh commented Oct 11, 2019 reply Follow flag instead of j += i, if it was simply j++, then the value of j will be floor(logn) + 1, which is Theta(logn) 2 2 replyShare Overflow04 commented Oct 24, 2022 reply Follow flag @Manu Thakur in the given example value of j is increment after the value of i decrement. value of k is increment before the value of i decrement. 1 1 replyShare ChayAdhiraj commented Jan 28, 2023 reply Follow flag Why is n= 2**k ? Why not k = n. Then T(n) changes ! Since i = i/2. The total no of terms will be logn in the GP series. 0 0 replyShare Daal_bhaat_enjoyer commented Nov 9, 2024 reply Follow flag as we all know .. n * (1/2 + 1/4 + 1/8 + 1/16 + 1/32...... 1/2^k) , where 2^k = n... so why can't we jus put log2n in place of the series, since we know that the sum of harmonic series is log2n.. so answer would be θ(nlog2n) 0 0 replyShare Souvik00 commented Jan 8, 2025 reply Follow flag @Daal_bhaat_enjoyer log n = (1 + 1/2 + 1/3 + 1/4 + ...) 1 1 replyShare duckduck commented Dec 3, 2025 reply Follow flag if j++ is given instead of j+=i, then θ(log n) will be correct right? 1 1 replyShare Karthik_Voorukonda commented 4 days ago reply Follow flag Check this 0 0 replyShare Please log in or register to add a comment.
Best answer 81 81 votes Answer will be $\Theta(n)$ $j = n/2+n/4+n/8+\ldots +1$ $\quad = n \left[1/2^1 + 1/2^2 + 1/2^3 +\ldots + 1/2^{\lg n}\right] $ (Sum of first $n$ terms of GP is $\left[a . \frac{1-r^n}{1-r}\right],$ where $a$ is the first term, $r$ is the common ratio $< 1,$ and $n$ is the number of terms) $\quad = n \left[1/2 \frac{1 - (1/2)^{\lg n}}{1-1/2} \right]$ $\quad = n \left[\frac{n-1}{n}\right]$ $\quad = n-1 = \Theta(n)$ anonymous answered Jan 1, 2015 • edited Apr 30, 2018 by Arjun anonymous comment Share Follow See all 10 Comments 10 10 Comments reply Show 7 previous comments Neelay Upadhyaya commented Dec 3, 2017 reply Follow flag Just a small note, in the $for$ loop for( i = n, j = 0; i > 0; i /= 2, j +=i ); if we have for( i = n, j = 0; i > 0; j +=i, i /= 2 ); the answer might vary as then $j$ would be incremented first with $i's$ initial value So if $n= 2^k$ where $k = 4$,we get $j=15$ in the first case. and $j=31$ in the second. 3 3 replyShare Vicky rix commented Dec 16, 2017 reply Follow flag yes .... log n series is (1 + 1/2 + 1/3 + 1/4 + 1/5 + .....1/n) not (1 + 1/2 + 1/4 + ....) 3 3 replyShare Queenia Agrawal commented Jan 19, 2018 reply Follow flag I have come to similar answer but for G.P. sum which formula you guys are using? I used a{r^{n} - 1}/{r - 1} and finally got j = 2n-2 which is O(n) so answer comes same. But what formulae you guys used? I know its a trivial question but please help. 1 1 replyShare Please log in or register to add a comment.
22 22 votes is correct because i gets reduced log2(n) time say for eg i=16 than i=8 j=8 i=4 j=12 i=2 j=14 i=1 j=15 i=0 hence ankur_mahiwal answered Jan 19, 2015 ankur_mahiwal comment Share Follow 0 reply Please log in or register to add a comment.
11 11 votes The variable j is initially 0 and value of j is sum of values of i. i is initialized as n and is reduced to half in each iteration. j = n/2 + n/4 + n/8 + .. + 1 = Θ(n) Note the semicolon after the for loop, so there is nothing in the body. Paras Nath answered Nov 11, 2017 Paras Nath comment Share Follow See all 2 Comments 2 2 Comments reply Swami patil commented Mar 11, 2018 reply Follow flag Thanks for giving information about semicolon I can't view that 0 0 replyShare Harish Alavala commented Jan 29, 2019 reply Follow flag nice explanation 0 0 replyShare Please log in or register to add a comment.
5 5 votes j=n/2+n/4+n/8----+1 j=n*(1/2+1/4+1/8-----1/n) j=n*(1-1/n) j=n-1 so O(n) focus _GATE answered Dec 20, 2016 focus _GATE comment Share Follow See 1 comment 1 1 comment reply PRANAV M commented Jun 12, 2018 reply Follow flag what formula of gp is used? 0 0 replyShare Please log in or register to add a comment.
5 5 votes I hope it helps! Answer will be option C. Setika Mehra answered Oct 15, 2020 Setika Mehra comment Share Follow 0 reply Please log in or register to add a comment.
1 1 vote C is the answer. $\frac{n}{1}+\frac{n}{2}+\frac{n}{2^{2}}+....+\frac{n}{2^{logn}} = n(1+\frac{1}{2} +\frac{1}{2^{2}}+....+\frac{1}{2^{logn}})\Rightarrow \Theta (n)$ Ankitrana answered Sep 6, 2016 Ankitrana comment Share Follow 0 reply Please log in or register to add a comment.