GATE Computer ScienceHardGATE CS (PYQ pattern)
How many times is x++ executed? for (i = 2; i <= 100; i++) for (j = i*i; j <= 100; j += i) x++;
- A. 283
- B. 146
- C. 246
- D. 142
Correct answer: B. 146
The inner loop runs only for i ≤ 10 (i² ≤ 100), and then ⌊(100 − i²)/i⌋ + 1 times: i = 2…10 give 49, 31, 22, 16, 11, 8, 5, 3, 1 = 146. Starting j at 2i instead of i² gives 283; letting i start at 1 adds 100 (246); using j < 100 gives 142.
More Algorithms questions
- How many times is x++ executed? for (i = 1; i <= 1000; i = 2) for (j = i; j >= 1; j /= 3) x++; (integer division)
- 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…