0% completed
Solution: Find the Duplicate Number
Problem Statement
We are given an unsorted array containing ‘n+1’ numbers taken from the range 1 to ‘n’. The array has only one duplicate but it can be repeated multiple times. Find that duplicate number without using any extra space. You are, however, allowed to modify the input array.
Example 1:
Input: [1, 4, 4, 3, 2]
Output: 4
Example 2:
Input: [2, 1, 3, 3, 5, 4]
Output: 3
Example 3:
Input: [2, 4, 1, 4, 4]
Output: 4
Constraints:
nums.length == n + 1- 1 <= n <= 10^5
1 <= nums[i] <= n- All the integers in `nums
.....
.....
.....
Renat Zamaletdinov
· 2 years ago
What is the idea behind it?
current = arr[arr[slow]]
d.psawyer
· 3 years ago
Problem reads:
We are given an unsorted array containing n+1 numbers taken from the range 1 to n. The array has only one duplicate but it can be repeated multiple times. Find that duplicate number without using any extra space. You are, however, allowed to modify the input array.
However, we are supplied 5,5,5,5,5 as a test case. Given that the array is supposed to contain n+1 numbers taken from the range 1 to n If the number 5 is anywhere in the range 1 to n, then the minimum array length must be 6
Ada
· 4 years ago
Can't you just return slow value for the similar problem? Since we found that there is a cycle when they meet, then surely the duplicate is the value at the slow position, and thus no use for the find_start method? I just returned the slow value without using find_start and it still passes all the cases mentioned above.
vetaranto
· 2 years ago
The question (Find the Duplicate Number) should clarify that constant O(1) extra space is allowed given the Python3 solution instantiates a variable (and thus uses extra space).
Pete Stenger
· 2 years ago
Solution doesn't work on [3, 1, 3], which is one of the test cases.
kfaham
· a year ago
test case: [3, 1, 3] is invalid
Length is 3, thus range of each element has to be up to 2 based on the constraints given in the problem other wise the fast & slow pointer approach doesn't work
Jack Tan
· 8 months ago
Russell Rogers
· 4 years ago
The hyperlink to the "Start of LinkedList Cycle" actually takes you to the topic on a different website at educative.io instead of this course on designgurus.org.
Julius Yudelson
· 3 years ago
The fourth test (first hidden) is [5, 5, 5, 5, 5] but the problem states the array contains numbers 1 through N with N+1 numbers in the array.
Mohammed Dh Abbas
· 2 years ago
class Solution: def findNumber(self, nums): def swap(i, j): nums[i], nums[j] = nums[j], nums[i] for i in range(len(nums)): while nums[i] != i + 1 and nums[i] != nums[nums[i] - 1]: swap(i, nums[i] - 1) return nums[-1]