Statement - 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.
Example 1:
Input: nums = [1,3,4,2,2]
Output: 2
Problem Link - Click
Code - Run Time O(n) and Space Complexity O(1).
class Solution {
public:
int findDuplicate(vector<int>& nums) {
for(int i = 0; i < nums.size(); i++)
{
if(nums[abs(nums[i])] < 0)
return abs(nums[i]);
nums[abs(nums[i])] *= -1;
}
return 0;
}
};
0 Comments