-
Notifications
You must be signed in to change notification settings - Fork 0
/
DuplicateElement.cpp
82 lines (65 loc) · 1.72 KB
/
DuplicateElement.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <bits/stdc++.h>
using namespace std;
/*
LinkedList cycle method!-most optimal
2 5 9 6 9 3 8 9 7 1
we traverse-
2-9-1-5-3-6-8-9-7-9
A cycle is created
Take two pointers-slow & fast
slow takes one step at a time
fast takes two steps at a time
whenever they collide,
take fast pointer to start and make it move one by one alongwith the slow pointer
They will collide where the duplicate element is present
*/
//Time Complexity O(n)
//Space Complexity O(1)
int findDuplicate(vector <int> & nums) {
int slow=nums[0];
int fast=nums[0];
do
{
slow=nums[slow];
fast=nums[nums[fast]];
}
while(slow!=fast);
fast=nums[0];
while(slow!=fast)
{
slow=nums[slow];
fast=nums[fast];
}
return slow;
}
int main()
{
int n;
cin>>n;
vector <int> v(n);
for(int i=0;i<n;i++)
cin>>v[i];
cout<<findDuplicate(v)<<endl;
}
/* Naive approach-sort array and adjacent elements
Time Complexity would have been O(n log n) and Space Complexity O(1)
It will also distort the array */
/*
Another approach-
Time Complexity O(n)
Space Complexity O(n)
Form a frequency array calculating frequency of no. of times element is present. If more than once, that is the duplicate array.
int findDuplicate(vector<int> nums)
{
int n=nums.size();
int Frequency[n+1]={0};
for(int i=0;i<n;i++)
{
if(Frequency[nums[i]]==0)
Frequency[nums[i]]++;
else
return nums[i];
}
return -1;
}
*/