Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div 2
A singly linked list has a tail of 4 nodes leading into a cycle of 6 nodes. Floyd's method moves a slow pointer 1 step and a fast pointer 2 steps in each round, starting both at the head. After how many rounds do they first meet?
- A. 4
- B. 10
- C. 12
- D. 6
Correct answer: D. 6
After i rounds the slow pointer is at position i and the fast one at 2i. They first coincide when i is at least the tail length 4 and divisible by the cycle length 6, so i = 6.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Reverse each complete group of k = 3 nodes in the list 1 → 2 → … → 10, leaving a final incomplete group as it is. What value is at…
- What is the area of the largest rectangle in the histogram with bar heights [2, 4, 2, 1, 6, 5, 5]?
- In the circular array [1, 5, 3, 4, 2], what is the next greater element of the value 2 at index 4, when the search wraps past the end?
- A window of length 3 slides over [1, 3, −1, −3, 5, 3, 6, 7]. What is the sum of the maximum of each window?
- Treat [5, −3, 5] as a circle. What is the maximum sum of a non-empty contiguous subarray, where the subarray may wrap around the end?
- Ten people stand in a circle numbered 1 to 10. Counting from person 1, every 3rd person is eliminated and removed, and counting continues…