Problem
You get an array nums of n + 1 integers, where every value is between 1 and n. By the pigeonhole principle at least one value repeats, and in fact exactly one value does (it may appear more than twice). Return that value without modifying the array 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
Hint for the shape: treat each index i as a node whose next is nums[i].
Constraints
1 <= n <= 10^5nums.length == n + 11 <= nums[i] <= nExactly one value appears two or more times.