GATE Computer ScienceHardGATE CS
How many times is x++ executed by the following code? for (i = 1; i <= 100; i++) for (j = 1; j <= 100; j += i) x++;
- A. 482
- B. 518
- C. 573
- D. 600
Correct answer: C. 573
For a fixed i the inner loop runs for j = 1, 1 + i, 1 + 2i, ..., so it executes ⌈100/i⌉ times, not ⌊100/i⌋. Summing ⌈100/i⌉ over i = 1 to 100 gives 573; the floor sum 482 is the trap.
More Algorithms questions
- The array [7, 3, 11, 1, 9, 12, 5, 2, 10, 4, 8, 6] is sorted by merge sort. How many inversions (pairs i < j with a[i] > a[j]) does it…
- A connected weighted undirected graph has vertices 0 to 5 and edges (u, v, weight): (0,1,2), (0,2,2), (1,2,2), (1,3,3), (2,3,3), (2,4,3)…
- A tree has vertices 0 to 9 with edges 0-1, 0-2, 1-3, 1-4, 2-5, 2-6, 4-7, 4-8, 6-9, and vertex weights w0 = 5, w1 = 4, w2 = 6, w3 = 3, w4 =…
- Under the standard uniform-hashing approximations, a table with load factor α = 0.9 needs about ½(1 + 1/(1 − α)²) probes for an…
- The function below is called as g(12, 5). def g(n, k): if k == 0 or k == n: return 1 return g(n-1, k-1) + g(n-1, k) How many calls to g…
- Ten words are inserted into an initially empty trie (one node per character, plus a root): cat, car, cart, care, dog, dot, do, carbon…