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?When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a group of items and you know at least one item appears more than once. The brute force approach is the most straightforward way to solve this by systematically comparing every single item against every other item until you find a match.
Here's how the algorithm would work step-by-step:
def find_duplicate_number(collection_items):
# Systematically check every item against all subsequent items to discover a repeating value
for primary_index in range(len(collection_items)):
for second_index in range(primary_index + 1, len(collection_items)):
# A match confirms the current value appears more than once in our input dataset
if collection_items[primary_index] == collection_items[second_index]:
return collection_items[primary_index]Imagine everyone is walking in a path that eventually leads to a looped track where people just run in circles. The secret to finding the repeated person is to use two runners, one moving slowly and the other moving twice as fast, until they eventually meet each other.
Here's how the algorithm would work step-by-step:
def find_duplicate(sequence_of_numbers):
# Floyd's cycle detection guarantees convergence when values map to indices.
slow_runner = sequence_of_numbers[0]
fast_runner = sequence_of_numbers[0]
while True:
slow_runner = sequence_of_numbers[slow_runner]
fast_runner = sequence_of_numbers[sequence_of_numbers[fast_runner]]
if slow_runner == fast_runner:
break
# Resetting one pointer to the start finds the cycle entry via path geometry.
pointer_from_start = sequence_of_numbers[0]
pointer_from_meeting = slow_runner
while pointer_from_start != pointer_from_meeting:
pointer_from_start = sequence_of_numbers[pointer_from_start]
pointer_from_meeting = sequence_of_numbers[pointer_from_meeting]
# The entry node of the cycle mathematically corresponds to the duplicate value.
return pointer_from_start| Case | How to Handle |
|---|---|
| Minimum valid input size of length two | The cycle detection algorithm correctly handles the smallest possible array by pointing index 1 to itself or another valid position within the range. |
| Maximum possible constraint values up to hundred thousand elements | The pointer-based Floyd's Cycle Finding algorithm runs in linear time and constant space, avoiding memory limits and timeouts. |
| Duplicate number appears multiple times in the array | The linked list cycle approach inherently finds the entry point of the cycle regardless of the frequency of the duplicated value. |
| The duplicate number points to itself forming a self-loop | The fast and slow pointers will immediately meet at this node, correctly identifying the duplicate without entering an infinite loop. |
| Values span the entire range from one to n without gaps except the duplicate | The array acts as a valid functional graph where every index from zero to n points to a valid index, guaranteeing a well-defined cycle. |
| The duplicate value is the maximum possible value n | Since all array values are bounded within the range one to n inclusive, the pointers will never exceed valid index bounds. |
| Zero values or negative numbers present in the input | The problem constraints explicitly state all integers are within the range one to n inclusive, making out-of-range checks unnecessary. |
| Array modification restriction strictly enforced | Using Floyd's Tortoise and Hare algorithm allows us to solve the problem in read-only mode, satisfying the constant space and immutable array constraints. |