knakul853

Untitled

Jun 26th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. /**
  2. knakul853
  3. **/
  4.  
  5. class Solution {
  6. public:
  7.     int findDuplicate(vector<int>& nums) {
  8.        
  9.         int n = (int)nums.size();
  10.        
  11.         if( n> 1)
  12.         {
  13.             int slow = nums[0];
  14.             int fast = nums[nums[0]];
  15.            
  16.             while(slow != fast )
  17.             {
  18.                 slow = nums[slow];
  19.                 fast = nums[nums[fast]];
  20.             }
  21.             fast = 0;
  22.             while(slow != fast)
  23.             {
  24.                 slow = nums[slow];
  25.                 fast = nums[fast];
  26.             }
  27.            
  28.             return slow;
  29.         }
  30.         return -1;
  31.     }
  32. };
Add Comment
Please, Sign In to add comment