GATE Computer ScienceVery hardGATE CS (PYQ pattern)
How many times does x++ execute? for (i = 1; i <= 64; i *= 2) for (j = 1; j <= i; j++) for (k = j; k <= i; k *= 2) x++;
- A. 254
- B. 247
- C. 127
- D. 448
Correct answer: B. 247
For i = 2^a the inner two loops run Σ_{j=1}^{i} (⌊log₂(i/j)⌋ + 1) = 2i − 1 times (e.g. i = 2: 2 + 1 = 3). Summing over i = 1, 2, …, 64: (2 + 4 + … + 128) − 7 = 254 − 7 = 247. Dropping the −1 gives 254.
More Algorithms questions
- How many times does x++ execute? for (i = 1; i <= 200; i++) { j = 1; while (j <= i) { x++; j = 3; } }
- 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…