Skip to content

Latest commit

 

History

History
51 lines (38 loc) · 1.06 KB

36.md

File metadata and controls

51 lines (38 loc) · 1.06 KB

METHOD 1

vector<int> findErrorNums(vector<int>& nums) {
        
        sort(nums.begin(),nums.end());
        int n=nums.size();
        vector<int>ans;
        int t=0,flag=1;
        for(int i=0;i<n-1;i++)
            if(nums[i]==nums[i+1])
                 ans.push_back(nums[i]);
        
        for(int i=1;i<n+1;i++)
            if(find(nums.begin(),nums.end(),i)==nums.end())
                ans.push_back(i);
        
        return ans;
    }

METHOD 2

vector<int> findErrorNums(vector<int>& nums) {
        
        sort(nums.begin(),nums.end());
        int n=nums.size();
        vector<int>ans;
        int t=0,flag=1;
        int sum=0;
        for(int i=0;i<n-1;i++)
        {
            if(nums[i]==nums[i+1])
                 ans.push_back(nums[i]);
            sum+=nums[i];
        }
        sum+=nums[n-1];
        sum-=ans[0];
        
        int osum=((n+1)*n)/2;
        ans.push_back(osum-sum);
        
        return ans;
    }