GATE Computer ScienceVery hardGATE CS (PYQ pattern)
How many times does x++ execute? for (i = 1; i < 200; i *= 2) for (j = i; j < 200; j += i) x++;
- A. 400
- B. 1600
- C. 393
- D. 399
Correct answer: C. 393
For i = 2^k the inner loop runs ⌈(200 − i)/i⌉ times: 199, 99, 49, 24, 12, 6, 3, 1 for i = 1, 2, 4, …, 128. The sum is 393, far below the n log n = 1600 that a careless bound suggests.
More Algorithms questions
- The solution of the recurrence T(n) = 2T(n/2) + n/log n (with T(1) = 1, logs base 2) is:
- The solution of T(n) = 4T(n/2) + n² log n is:
- Matrices A1 to A6 have dimensions 5×40, 40×10, 10×30, 30×8, 8×25 and 25×6. The minimum number of scalar multiplications needed to compute…
- A text uses 8 symbols with frequencies 5, 9, 12, 13, 16, 45, 3 and 7. If a Huffman code is built, the total number of bits needed for the…
- A 0/1 knapsack has capacity 50 and items given as (value, weight): (60, 10), (100, 20), (120, 30), (75, 25). The maximum total value is:
- Which of the following statements are correct? (I) 2-SAT can be solved in polynomial time. (II) Subset-sum has a pseudo-polynomial…