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 Show 15 previous comments 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 3 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 pC commented Dec 20, 2015 reply Follow flag j = n/2+n/4+n/8+...+1 this is log series doesn't it ? Then how are you getting theta(n) ? Will it not be thetha ( log n ) ? 1 1 replyShare One commented Jul 8, 2016 reply Follow flag 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) 24 24 replyShare Puja Mishra commented Dec 26, 2016 i edited by Puja Mishra Dec 26, 2016 reply Follow flag what will be the value of n ?? only n=2^a ones?? i think it will be after applying gp n.(1-1/2^n) hence O(n) .... n/2^n will neglected for bigger value of n ... 2 2 replyShare pC commented Dec 27, 2016 reply Follow flag @puja_mishra, $\begin{align*} f(n) & = \frac{n}{2}+\frac{n}{4}+\frac{n}{8}+.....+\frac{n}{2^{k}} \\ & = n \sum_{i=1}^{k}\frac{1}{2^{i}} \\ & = n (1-\frac{1}{2^{k}}) \\ & = n- \frac{n}{2^{k}} & [ \text{where} \ n=2^{k}] \\ & = n-1 \end{align*}$ Which is $O(n)$ 23 23 replyShare shaurya vardhan commented Oct 31, 2017 reply Follow flag this explanation is wrong , wrongly selected as the best answer . 1 1 replyShare Puja Mishra commented Oct 31, 2017 reply Follow flag Then wat will be the ans?? 0 0 replyShare shaurya vardhan commented Oct 31, 2017 reply Follow flag Answer is correct , the explanation is not correct .. Val (j) grows linearly .. like this For i=16 i=8 j=8 i=4 j=12 i=2 j=14 i=1 j=15 And finally , i= 0. It's explained in the answer below .. that is more apt . 0 0 replyShare 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.