Find the Duplicate Number
Find the repeated value without changing the array or using extra memory, by treating it as a linked list.
The problem
You get an array nums of n + 1 integers, and every value lies between 1 and n, so at least one value must repeat. Exactly one value repeats, but it may appear more than twice. Return that value without modifying the array and using only O(1) extra space.
Input nums = [2, 5, 1, 3, 5, 4]
Output 5
Six values from the range 1 to 5, and 5 is the only one that shows up twice.
Input nums = [4, 1, 4, 4, 2]
Output 4
4 appears three times; it is still the one repeated value.
Key constraint: You may not change the array and may use only O(1) extra space, which rules out sorting it, marking it or keeping a set.
How to spot the pattern
- There are n + 1 values in the range 1 to n, so every value is also a valid index.
- The rules forbid both extra memory and changes to the input.
The idea
Treat the array as a linked list in which index i links to index nums[i]. Every value is a valid index, so you can follow these links forever, which means they must run into a cycle. The repeated value is an index that two different positions link to, and that is exactly where the cycle begins, so Floyd's slow and fast pointers find it, just like in Linked List Cycle II.
Brute force: Compare every pair of values, which takes O(n^2) time; a hash set is faster but breaks the O(1) space rule.
Step by step
- 1Start
slowandfastat index 0; no value is 0, so nothing links back to index 0 and it acts as the head of the list. - 2Move
slow = nums[slow]andfast = nums[nums[fast]]until they are equal. - 3Start a new pointer at index 0 and keep
slowat the meeting point. - 4Move both one link at a time; the index where they meet is the duplicate value.
See the pattern move
The fast and slow pointers pattern on a small example. Step through it, or press play.
Code
class Solution:
def findDuplicate(self, nums: List[int]) -> int:
# Index i links to index nums[i]; the duplicate is where the cycle starts.
slow = fast = 0
while True:
slow = nums[slow] # one link
fast = nums[nums[fast]] # two links
if slow == fast:
break
finder = 0
while finder != slow:
finder = nums[finder]
slow = nums[slow]
return finderComplexity
Time
O(n)
Space
O(1)
As in Linked List Cycle II, each phase takes O(n) steps, and only a few indexes are stored.
Watch out for
- Returning the meeting point of the first phase: it is inside the cycle, but not necessarily the cycle's entrance.
- Restarting both pointers in the second phase; one stays at the meeting point and only the other goes back to index 0.
- Sorting the array or marking values as negative: both find the answer but modify the input, which the problem forbids.
Follow-up questions
Is there another O(1) space approach?
Binary search on the value: for a guess mid, count how many numbers are at most mid. If that count is more than mid, the duplicate is at most mid, otherwise it is larger. That takes O(n log n) time.
Why is index 0 a safe place to start?
Values run from 1 to n, so no link ever points back to index 0. It sits outside the cycle, like the head of a list that leads into a loop, which is exactly the setup the cycle-start trick needs.
In Hinglish
Array ko linked list samjho: index i se nums[i] par jao. Duplicate wahi jagah hai jahan do raaste milte hain, yaani cycle ka start, aur use Floyd se dhoondo.