-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN3 repeat number.cpp
66 lines (51 loc) · 992 Bytes
/
N3 repeat number.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
int Solution::repeatedNumber(const vector<int> &v) {
int n = v.size();
int el1 = INT_MAX;
int el2 = INT_MAX;
int count1 =0;
int count2 =0;
for(int i=0;i<n;i++)
{
if(el1==v[i])
{
count1++;
}
else if(el2 = v[i])
{
count2++;
}
else if(count1==0)
{
count1++;
el1 = v[i];
}
else if(count2==0)
{
count2++;
el2 = v[i];
}
else
{
count1--;
count2--;
}
}
count1=0;
count2=0;
for(int i=0;i<n;i++)
{
if(el1==v[i])
{
count1++;
}
else if(el2==v[i])
{
count2++;
}
}
if(count1>n/3)
return el1;
if(count2>n/3)
return el2;
return -1;
}