fork download
  1. //test
  2. //12
  3. //89 42 156 86 35 34 45 34 35 5 35 9 --> 35
  4.  
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. int main() {
  9. int n, v[10001], ap[501], aux = 0;
  10. cin >> n;
  11. // Initializam cu 0 fiecare pozitie pentru a putea numara aparitiile
  12. for (int i = 1; i <= n; ++i) {
  13. ap[i] = 0;
  14. }
  15. for (int i = 1; i <= n; ++i) {
  16. cin >> v[i];
  17. }
  18. // Actualizam sirul de aparitii
  19. for (int i = 1; i <= n; ++i) {
  20. ++ap[v[i]]; // Crestem numarul de aparitii ale elementului v[i]
  21. }
  22. for (int i = 1; i < n; ++i) {
  23. for (int j = i + 1; j <= n; ++j) { // Cautam elemente mai mici decat v[i]
  24. if (ap[v[i]] > ap[v[j]] && v[i] > v[j]) { // Am gasit un element mai mic
  25. // Punem valoarea din v[j] in v[i] si din v[i] in v[j]
  26. aux = v[i];
  27. v[i] = v[j];
  28. v[j] = aux;
  29. }
  30. }
  31. }
  32. cout << aux << "\n";
  33. return 0;
  34. }
Success #stdin #stdout 0s 544KB
stdin
12
89 42 36 86 35 36 45 36 35 3 35 9 --> 35
stdout
35