Q -: Find the Duplicate Number

Q -: 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 the problem without modifying the array nums and uses 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

Solution


class Solution {

public:

    int findDuplicate(vector<int>& nums) {

      sort(nums.begin(),nums.end());

        for(int i=1;i<nums.size();i++){

            if(nums[i]==nums[i-1]){

                return nums[i];

            }

        }

        return -1;

    }

};

Comments

Popular posts from this blog

Coding Ninjas

Code : Get Path - BFS

Code : All connected components