Created
October 7, 2022 20:32
-
-
Save webber2408/e12ab892dff4c309be43beaeed591875 to your computer and use it in GitHub Desktop.
Approach-6) Find Duplicates - Floyd's Hare & Tortoise Algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int findDuplicate(vector<int>& nums) { | |
int tort = nums[0], hare = nums[0]; | |
do{ | |
tort = nums[tort]; | |
hare = nums[nums[hare]]; | |
}while(tort != hare); | |
tort = nums[0]; | |
while(tort != hare){ | |
tort = nums[tort]; | |
hare = nums[hare]; | |
} | |
return tort; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment