Advertisement
vaibhav1906

Find the Duplicate Number

Mar 24th, 2022
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int findDuplicate(vector<int>& nums) {
  4. int sum =0;
  5. int n = nums.size();
  6. int k;
  7. for(int i=0; i<n; i++){
  8. if(nums[i]<0) k = - nums[i];
  9. else k = nums[i];
  10.  
  11. if(nums[k-1]<0){
  12. return k;
  13. }
  14. else{
  15. nums[k-1] = -nums[k-1];
  16. }
  17. }
  18.  
  19. return 0;
  20. }
  21. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement