Practice Problem

Find the Duplicate Number

Difficulty: Medium

Given an array of n + 1 integers where each integer is in the range [1, n], find the one duplicate number without modifying the array and using only constant extra space.

Find the Duplicate Number

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 it without modifying the array nums and using only constant extra space.

Examples

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 <= 10^5
  • nums.length == n + 1
  • 1 <= nums[i] <= n
  • There is only one repeated number in nums, but it could be repeated more than once.

Expected Complexity

  • Time: O(n)
  • Space: O(1)
MEDIUM
Singly Linked List
Fast/Slow Pointers
Cycle Detection
Intermediate

0 views

Solution

Hints

Hint 1
Hint 2
Premium
Hint 3
Premium
Hint 4
Premium