Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.
There is only one repeated number in nums, return this repeated number.
You must solve the problem without modifying the array nums and using only constant extra space.
Example 1:
Input: nums = [1,3,4,2,2] Output: 2
Example 2:
Input: nums = [3,1,3,4,2] Output: 3
Example 3:
Input: nums = [3,3,3,3,3] Output: 3
Constraints:
1 <= n <= 105nums.length == n + 11 <= nums[i] <= nnums appear only once except for precisely one integer which appears two or more times.
Follow up:
nums?Problem Overview: You receive an array of n + 1 integers where each value is in the range [1, n]. Since there are more numbers than the range allows, at least one value must appear more than once. The task is to return the duplicate number without modifying the array and while using minimal extra space.
Approach 1: Floyd's Tortoise and Hare (Cycle Detection) (O(n) time, O(1) space)
This approach treats the array like a linked list. Each index points to the next index using the value stored at that position. Because values are restricted to 1..n, following these "next" pointers must eventually form a cycle. The duplicate number is exactly the entry point of that cycle. Use two pointers: a slow pointer moving one step at a time and a fast pointer moving two steps. When they meet, a cycle exists. Reset one pointer to the start and move both one step at a time until they meet again; that position corresponds to the duplicate value. This technique uses the classic two pointers pattern and works without modifying the array. Time complexity is O(n) because each pointer traverses the structure at most a few passes. Space complexity is O(1) since only a couple of variables are used.
Approach 2: Binary Search on Result (O(n log n) time, O(1) space)
This method applies binary search on the value range instead of array indices. The key insight comes from the pigeonhole principle. For any midpoint mid, count how many numbers in the array are less than or equal to mid. If the count exceeds mid, the duplicate must lie in the range [1, mid]. Otherwise, it lies in [mid + 1, n]. Repeatedly narrow the search range until a single value remains. Each step scans the array once to compute the count, giving O(n) work per iteration and O(log n) iterations overall. The algorithm uses constant extra memory and does not modify the input.
Recommended for interviews: Floyd's cycle detection is the solution most interviewers expect. It demonstrates deeper algorithmic thinking because you reinterpret the array as a linked structure and apply cycle detection to locate the repeated value. The binary search approach is also valid and easier to reason about, but it is slower at O(n log n). Showing the binary search idea first and then optimizing to Floyd's algorithm signals strong problem-solving skills.
This approach uses cycle detection to find the duplicate number using two pointers. Imagine the numbers in the array form a linked list, where the value at each index points to the next index. When there is a duplicate, a cycle will be formed. The first step is to find the intersection point of the cycle using a slow and fast pointer. Once they meet, use two pointers to find the entrance of the cycle, which will be the duplicate number. This method doesn't modify the array and uses O(1) space.
This C solution implements the cycle detection algorithm. Two pointers, slow and fast, iterate through the array. First they meet in the cycle; then, by resetting one and moving both pointers step by step, they meet at the duplicate number.
Time Complexity: O(n), where n is the size of the array. Space Complexity: O(1), since we use a constant amount of extra space.
This approach leverages a binary search technique on the range of possible values of the duplicate number. For each midpoint in this range, count how many numbers are less than or equal to it. If the count is greater than the midpoint, the duplicate must be in the lower range; otherwise, it is in the upper range. Binary search is applied until the duplicate is found. This doesn't require additional space and doesn't alter the input array.
This C solution uses binary search on the range of possible numbers, counting numbers less than or equal to the midpoint, and then adjusting the binary search boundaries accordingly, eventually discovering the duplicate number.
Time Complexity: O(n log n). Space Complexity: O(1).
| Approach | Complexity |
|---|---|
| Approach 1: Floyd's Tortoise and Hare (Cycle Detection) | Time Complexity: O(n), where n is the size of the array. Space Complexity: O(1), since we use a constant amount of extra space. |
| Approach 2: Binary Search on Result | Time Complexity: O(n log n). Space Complexity: O(1). |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Floyd's Tortoise and Hare (Cycle Detection) | O(n) | O(1) | Optimal interview solution when array cannot be modified and constant space is required |
| Binary Search on Value Range | O(n log n) | O(1) | Useful when you recognize the pigeonhole principle and want a simpler reasoning approach |
Find the Duplicate Number - Floyd's Cycle Detection - Leetcode 287 - Python • NeetCode • 439,819 views views
Watch 9 more video solutions →Practice Find the Duplicate Number with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor