Advertisement
vaibhav1906

Find the Duplicate Number

Aug 6th, 2022
923
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.33 KB | None
  1. class Solution {
  2. public:
  3.     int findDuplicate(vector<int>& nums) {
  4.         int n = nums.size();
  5.        
  6.         for(int i = 0; i<n; i++){
  7.             int indx = abs(nums[i])-1;
  8.            
  9.             if(nums[indx]>0)nums[indx] *=-1;
  10.             else return indx+1;
  11.            
  12.         }
  13.        
  14.         return -1;
  15.     }
  16. };
Advertisement
RAW Paste Data Copied
Advertisement