Search⌘ K
AI Features

Problem: Find the Duplicate Number

Explore how to identify the duplicate number in an integer array by applying a binary search on the value range. Learn to leverage the pigeonhole principle, use constant extra space, and achieve a solution with O(n log n) time complexity while preserving the original array.

Statement

You are given an integer array nums of length n + 1, where every element lies in the range [1,n][1, n] inclusive.

Exactly one number in nums is repeated (it may appear more than once), while all other numbers appear exactly once. Return the repeated number.

You must solve this problem without modifying the array nums and using only constant extra space.

Note:
How can we prove that at least one duplicate number must exist in nums? Can you solve the problem in linear time?

Constraints:

  • 11 \leq n 105\leq 10^5

  • nums.length ==== n +1+ 1 ...