GATE Computer ScienceEasyGATE CS (PYQ pattern)
How many times is x++ executed? for (i = 1; i <= 1000; i *= 2) for (j = i; j >= 1; j /= 3) x++; (integer division)
- A. 55
- B. 28
- C. 34
- D. 44
Correct answer: C. 34
i takes the 10 values 1, 2, 4, …, 512. For each, the inner loop runs ⌊log₃ i⌋ + 1 times: 1, 1, 2, 2, 3, 4, 4, 5, 6, 6 for i = 1, 2, 4, …, 512, total 34 (e.g. i = 512: 512, 170, 56, 18, 6, 2). Halving instead of dividing by 3 gives 55; the condition j > 1 gives 28.
More Algorithms questions
- Let f(n) = (log₂ n)^(log₂ n), g(n) = n^(log₂ log₂ n) and h(n) = (log₂ log₂ n)!. Which of the following is correct? I. f(n) = Θ(g(n)). II…
- Let f₁ = n^(1/log₂n), f₂ = (log₂ n)³, f₃ = (log₂ n)^(log₂ log₂ n), f₄ = 2^√(log₂ n), f₅ = n^0.001. Which lists them in increasing order of…
- In the median-of-medians selection algorithm the input is split into groups of g elements (g odd), giving the standard recurrence T(n) ≤…
- n keys, each an n-bit unsigned integer, are sorted by LSD radix sort that treats each key as digits of r = ⌊log₂ n⌋ bits (radix 2^r) and…
- Two sorted lists of 5 and 7 distinct keys are to be merged into one sorted list by comparisons. What does the decision-tree…
- Cocktail-shaker sort is applied to [5, 1, 4, 2, 8, 0, 3]: sweeps alternate left-to-right and right-to-left over the unsorted part (each…