← 返回 nvidia 的题目列表Find Any Duplicate in Range [1, n-1] with O(1) Extra Space
类型:online_judge
Problem: Find Any Duplicate in an Array (Modifying Input Allowed, O(1) Extra Space)
You are given an integer array nums of length n. Each element is in the range [1, n-1].
It is guaranteed that at least one number is duplicated.
Return any one duplicated number.
Requirements / Constraints
Use only O(1) extra space (no extra arrays/hash sets/maps).
You may modify the input array nums.
The time complexity must be better than O(n^2).
Output
Return any duplicated value.
Typical Constraints (common interview setting)
2 <= n <= 2 * 10^5
nums[i] ∈ [1, n-1]
Examples
Input: [1,3,4,2,2] Output: 2
Input: [3,1,3,4,2] Output: 3
What to provide
Describe/implement an algorithm meeting the constraints, and state time and space complexity.
Example
Input
1 3 4 2 2
Output
2