-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindDuplicateNumber.cpp
More file actions
48 lines (46 loc) · 1.45 KB
/
findDuplicateNumber.cpp
File metadata and controls
48 lines (46 loc) · 1.45 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
45
46
47
/*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. */
//NORMAL APROACH :
class Solution {
public:
int findDuplicate(vector<int>& nums) {
int i,j;
//traversing array
for(i=0;i<nums.size();i++)
{
//comparing each element with its forward's element and if found same then break;
for(j=i+1;j<nums.size();j++)
{
if(nums[i]==nums[j])
break;
}
//return the duplicate element
if(j<nums.size())
return nums[i];
}
return -1;
}
};
//OPTIMAL APROACH :
class Solution {
public:
int findDuplicate(vector<int>& nums) {
//taking two variables initially pointing 1st element
int slow=nums[0],fast=nums[0];
do{
//increasing slow by1 and fast by 2 untill slow and fast overlapp
slow=nums[slow];
fast=nums[nums[fast]];
}while(slow!=fast);
//after overlap leave slow as it is and point fast to 1st element again
fast=nums[0];
//now increase both slow and fast by 1 untill overlap
while(slow!=fast)
{
slow=nums[slow];
fast=nums[fast];
}
//element on which slow and fast overlap will be duplicate element
return slow;
}
};