Data Structures & AlgorithmsVery hardGATE CS

An array next = [10, 6, 10, 1, 1, 9, 5, 5, 10, 3, 7] defines a linked structure in which the node after index i is next[i]. Floyd's algorithm starts both pointers at index 0, moves slow by one step and fast by two steps per round, and stops when they meet. At which index do they meet?

  1. A. 9
  2. B. 3
  3. C. 1
  4. D. 5

Correct answer: B. 3

The path from 0 enters a cycle at index 5 (cycle length 5). Simulating slow and fast pointers gives their first meeting at index 3; the cycle entry 5 is found only in the second phase.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic