-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindDuplicate.cpp
More file actions
44 lines (36 loc) · 1.12 KB
/
Copy pathfindDuplicate.cpp
File metadata and controls
44 lines (36 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
Given an array of integers nums containing n + 1 integers where each integer
is in the range [1, n] inclusive.
There is only one repeated number in nums, return this repeated number.
Input: nums = [1,3,4,2,2]
Output: 2
*/
class Solution {
public:
/*
This question can be done in O(n**2), sorting the array and 2ptr(O(nlog(n))),
else we can keep a freq array.
The freq array can be a hash map. But, since, we have been given that the elements
will be from [0,n] only, we can use this to keep a count by creating a array of it.
The most optimal solution is the floyd algorithm. Which is similar to
finding a loop in a linked list
*/
int findDuplicate(vector<int>& nums)
{
int slow = nums[0];
int fast = nums[0];
do
{
slow = nums[slow];
fast = nums[nums[fast]];
}while(fast!=slow);
// finding the entrance
slow = nums[0];
while(slow!=fast)
{
slow = nums[slow];
fast = nums[fast];
}
return slow;
}
};