GATE Computer ScienceModerateGATE CS (PYQ pattern)
Euclid's algorithm gcd(a, b) repeatedly replaces (a, b) by (b, a mod b) until b = 0, counting one step per mod operation. Over all pairs 1 ≤ b < a ≤ 1000, what is the maximum number of steps?
- A. 15
- B. 13
- C. 14
- D. 10
Correct answer: C. 14
By Lamé's theorem the worst inputs are consecutive Fibonacci numbers; the largest pair within 1000 is (987, 610) = (F₁₆, F₁₅), which needs 14 steps (610, 377, …, 1, 0). A pair needing 15 steps would require a ≥ F₁₇ = 1597. An exhaustive check of all pairs confirms 14, attained only by (987, 610).
More Algorithms questions
- The Sieve of Eratosthenes on 2..100 crosses out the multiples of each prime p ≤ 10, starting from p² (p², p² + p, …). How many numbers are…
- The Tower of Hanoi with 6 disks is played on three pegs in a row (left, middle, right), and a disk may move only between adjacent pegs…
- Of the 92 solutions of the 8-queens problem (rotations and reflections counted separately), how many have no queen on either of the two…
- Four men m0–m3 and four women w0–w3 have preference lists (best first) m0: w1 w0 w2 w3; m1: w0 w2 w1 w3; m2: w1 w3 w2 w0; m3: w0 w1 w3 w2…
- For n = 5 distinct keys, let M be the worst-case comparisons of top-down merge sort (split ⌈n/2⌉ + ⌊n/2⌋, merge stops when one side is…
- What is the minimum number of comparisons that guarantees finding the median (the third smallest) of 5 distinct elements in the worst case?