Finding the Duplicate in an Array of $n + 1$ Integers ($O(1)$ Space, Array Unchanged)
Jab aap ke paas $n + 1$ integers ka array ho jahan har number $1$ se $n$ ke darmiyan ho (Pigeonhole Principle ki wajah se kam az kam aik number duplicate hoga), aur aap input array ko modify nahi kar sakte aur extra space $O(1)$ rakhni hai, to is ko linked list cycle mein model karke Floyd’s Tortoise and Hare algorithm se solve karte hain.
Is se hum bina kisi extra array ya hash set ke duplicate number $O(n)$ time aur $O(1)$ space mein nikal lete hain.
Why It Maps to a Linked List Cycle
Har index i ko aik node samjhein, aur nums[i] ko us node ka next pointer (i -> nums[i]):
- Kyunke har value $1$ se $n$ ke darmiyan hai, koi bhi pointer
0par nahi jata (index 0hamesha safe start hai, kyunki values $1…n$ hain). - Jab do indices ke paas same value
dhoti hai (yanidduplicate hai), to dono indicesdki taraf point karte hain—matlab nodedke paas do incoming edges aa jaati hain. - Do incoming edges ka matlab hai ke graph mein cycle banta hai, aur is cycle ka entry point exact duplicate value
dhota hai.
Two-Phase Floyd’s Cycle Detection
- Phase 1 — Intersection Find Karna:
slow = nums[0]aurfast = nums[0]se start karein.- Har step mein
slow = nums[slow](1 hop) aurfast = nums[nums[fast]](2 hops) chalayein jab takslow == fastna ho jaye.
- Phase 2 — Cycle Entry (Duplicate) Dhoondna:
slowko wapasnums[0]par le aayein, jabkefastwahi meeting point par rahega.- Ab dono ko 1-1 step (
slow = nums[slow],fast = nums[fast]) aage barhayein jab takslow == fastna ho jaye. - Jis point par dono milenge, wahi duplicate number hai.
(Math proof: Agar start se cycle entry tak ka distance $F$ hai, entry se meeting point tak ka distance $a$ hai, aur cycle ki length $C$ hai, to $2(F + a) - (F + a) = kC \implies F + a = kC \implies F = kC - a$. Is liye entry se start karne wala aur meeting point se chalne wala dono $F$ steps ke baad exact cycle entry par milte hain.)
Quick Trace: nums = [1, 3, 4, 2, 2] ($n = 4$, values in $1…4$)
Edges: 0 -> 1 -> 3 -> 2 -> 4 -> 2 (cycle 2 -> 4 -> 2, entry = 2).
- Phase 1:
- Step 1:
slow = 1,fast = 3 - Step 2:
slow = 3,fast = 2 - Step 3:
slow = 2,fast = 2$\implies$ Meet at2.
- Step 1:
- Phase 2:
- Reset
slow = nums[0] = 1,fast = 2. - Step 1:
slow = 3,fast = 4. - Step 2:
slow = 2,fast = 2$\implies$ Meet at2(the duplicate).
- Reset
Code Implementation
Python Solution
def find_duplicate(nums: list[int]) -> int:
# Phase 1: Find intersection inside the cycle
slow = nums[0]
fast = nums[0]
while True:
slow = nums[slow]
fast = nums[nums[fast]]
if slow == fast:
break
# Phase 2: Find the entrance to the cycle (the duplicate number)
slow = nums[0]
while slow != fast:
slow = nums[slow]
fast = nums[fast]
return slow
C++ Solution
#include <vector>
int findDuplicate(const std::vector<int>& nums) {
// Phase 1: Detect cycle and find meeting point
int slow = nums[0];
int fast = nums[0];
do {
slow = nums[slow];
fast = nums[nums[fast]];
} while (slow != fast);
// Phase 2: Move slow to start; both advance 1 step to find cycle entry
slow = nums[0];
while (slow != fast) {
slow = nums[slow];
fast = nums[fast];
}
return slow;
}
Complexity
- Time Complexity: $O(n)$ — dono phases mil kar linear number of steps mein cycle entry dhoond lete hain.
- Auxiliary Space: $O(1)$ — sirf do integer pointers (
slowaurfast) istemal hote hain aur input array bilkul change nahi hota.
