Find the Duplicate Number - Floyd's Cycle Detection - Leetcode 287 - Python

Find the Duplicate Number - Floyd's Cycle Detection - Leetcode 287 - Python

TLDR;

This video explains how to find the duplicate number in an array of integers using Floyd's cycle-finding algorithm. The problem is framed as a linked list cycle detection problem, where array values are treated as pointers. The algorithm involves identifying the start of the cycle, which corresponds to the duplicate number. The video covers the intuition behind the algorithm, provides a mathematical proof, and presents a Python implementation.

  • The problem is framed as a linked list cycle detection problem.
  • Floyd's cycle-finding algorithm is used to find the duplicate number.
  • The algorithm involves identifying the start of the cycle, which corresponds to the duplicate number.

Read the problem [0:00]

The problem involves finding a duplicate number in an integer array nums of length n + 1, where each value in the array is within the range of 1 to n. The goal is to return the number that is repeated more than once. The constraints are that the solution must use constant extra space and cannot modify the input array. A straightforward approach using a hash set would take O(n) time and O(n) memory, but the problem requires a solution with O(1) space complexity.

Drawing Explanation [2:32]

The array can be viewed as a linked list where the values act as pointers to indices. Since the array length is n + 1 and the values range from 1 to n, a cycle will inevitably form within the linked list. The duplicate number is the entry point to this cycle. The index 0 is not part of the cycle because no value in the array points to it, as all values are between 1 and n. The duplicate number is the start of the cycle because multiple nodes point to it. Floyd's algorithm is then applied to find the beginning of the cycle.

Floyd's algorithm involves two phases. In the first phase, a slow pointer and a fast pointer are initialized at the start of the linked list (index 0). The slow pointer moves one step at a time, while the fast pointer moves two steps at a time. The pointers will eventually meet at an intersection point within the cycle. In the second phase, the slow pointer remains at the intersection point, and a second slow pointer is initialized at the start of the linked list. Both slow pointers move one step at a time until they meet again. This second intersection point is the start of the cycle, which corresponds to the duplicate number.

The video provides a mathematical proof to explain why the intersection point is always the start of the cycle. The proof involves defining variables for the distance from the start to the cycle (p), the distance from the intersection point to the start of the cycle (x), and the length of the cycle (c). By setting up equations for the distances traveled by the slow and fast pointers, it can be shown that p is equal to x, meaning the distance from the start to the cycle is the same as the distance from the intersection point to the start of the cycle.

Coding Explanation [14:31]

The code implements Floyd's algorithm using two pointers, fast and slow, initialized to 0. The while true loop advances slow by one position (nums[slow]) and fast by two positions (nums[nums[fast]]) until they meet. Once they intersect, a second slow pointer, slow2, is initialized to 0. Both slow and slow2 are advanced one step at a time until they meet. The meeting point is the duplicate number, which is then returned. The code ensures that the pointers remain within the bounds of the array and does not modify the input array, adhering to the problem's constraints.

Watch the Video

Date: 10/9/2025 Source: www.youtube.com
Share

Stay Informed with Quality Articles

Discover curated summaries and insights from across the web. Save time while staying informed.

© 2024 BriefRead