GATE Computer ScienceVery hardGATE CS (PYQ pattern)
How many times does x++ execute? for (i = 1; i <= 200; i++) { j = 1; while (j <= i) { x++; j *= 3; } }
- A. 862
- B. 906
- C. 1000
- D. 884
Correct answer: D. 884
For a given i the loop runs ⌊log₃ i⌋ + 1 times: 1 time for i = 1, 2; 2 times for i = 3…8; 3 times for 9…26; 4 times for 27…80; 5 times for 81…200 (120 values). The sum is 2 + 12 + 54 + 216 + 600 = 884.
More Algorithms questions
- How many balanced parenthesis strings with 6 pairs have nesting depth at most 2?
- In how many ways can a 2 × 10 rectangle be tiled with 2 × 1 dominoes (either orientation) and 2 × 2 squares?
- How many permutations of 7 distinct items have every cycle of length at least 3 (no fixed points and no 2-cycles)?
- Twelve symbols, all with the same frequency 1, are encoded with an optimal Huffman code. What is the total number of bits to encode one…
- In the hiring problem 10 candidates arrive in uniformly random order. The first 3 are only interviewed (never hired); from candidate 4 on…
- Karger's contraction algorithm is run on a connected multigraph with n = 8 vertices, but it stops when 4 super-vertices remain (instead of…