Created
December 17, 2019 13:40
-
-
Save shubham1710/3b5fbeb42aa4e7d7c533d856da02e635 to your computer and use it in GitHub Desktop.
Gyu
This file contains hidden or 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
#include <cmath> | |
#include <cstdio> | |
#include <vector> | |
#include <iostrea | |
#include <algorithm> | |
using namespace std; | |
int repeatedNumber( vector<int> &A) { | |
int n = A.size(); | |
if(n == 0) return -1; | |
if(n <= 2) return A[0]; | |
int c1 = 0, c2 = 0, f = 0, s = 0; | |
for(int i = 0; i < n; i++) { | |
if(A[i] == f) { | |
c1++; | |
} | |
else if(A[i] == s) { | |
c2++; | |
} | |
else if(c1 == 0) { | |
c1 = 1; | |
f = A[i]; | |
} | |
else if(c2 == 0) { | |
c2 = 1; | |
s = A[i]; | |
} | |
else { | |
c1--; | |
c2--; | |
} | |
} | |
c1 = 0; | |
c2 = 0; | |
for(int i = 0; i < n; i++) { | |
if(A[i] == f) c1++; | |
if(A[i] == s) c2++; | |
} | |
if(c1 > n/3) return f; | |
else if(c2 > n/3) return s; | |
return -1; | |
} | |
int main() { | |
int n; | |
std::cin>>n; | |
vector <int> A(n); | |
for(int t=0;t<n;t++) | |
std::cin>>A[t]; | |
std::cout<<repeatedNumber(A); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment