Alex_tz307

Element majoritar

Sep 14th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int n, x[100005];
  6.  
  7. int main(){
  8.     cin  >> n;  
  9.     for(int i = 0 ; i < n ; ++ i)
  10.         cin >> x[i];
  11.     int candidat = x[0], contor = 1;
  12.     for(int  i = 1; i < n ; ++i)
  13.     {
  14.         if(x[i] == candidat)
  15.             contor++;
  16.         else
  17.             contor --;
  18.         if(contor == 0)
  19.             candidat = x[i], contor = 1;
  20.     }
  21.     contor = 0;
  22.     for(int i = 0 ; i < n ; ++ i)
  23.         if(x[i] == candidat)
  24.             contor ++;
  25.     if(contor > n/2)
  26.         cout << "DA " << candidat;
  27.     else
  28.         cout << "NU";
  29.     return 0;
  30. }
Add Comment
Please, Sign In to add comment