fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int findMode(const vector<int>& nums, int i) {
  5. unordered_map<int, int> freq;
  6. for (int num = 0; num<=i; num++) {
  7. freq[num]++;
  8. }
  9. int mode = nums[0];
  10. int maxCount = 0;
  11. for (const auto& pair : freq) {
  12. if (pair.second > maxCount) {
  13. maxCount = pair.second;
  14. mode = pair.first;
  15. }
  16. }
  17.  
  18. return mode;
  19. }
  20.  
  21. int main() {
  22. int t;
  23. cin>>t;
  24. while(t--)
  25. {
  26. int n;
  27. cin>>n;
  28. int arr[n];
  29. vector<int> sequence;
  30. for(int i=0; i<n; i++)
  31. {
  32. int a;
  33. cin>>a;
  34. sequence.push_back(a);
  35. }
  36. // brute force
  37. int b[n];
  38. for(int i=0; i<n; i++)
  39. {
  40. b[i]=findMode(sequence,i);
  41. cout<<b[i]<<" ";
  42. }
  43. cout<<endl;
  44. }
  45. }
  46.  
Success #stdin #stdout 0.01s 5288KB
stdin
4
2
1 2
4
1 1 1 2
8
4 5 5 5 1 1 2 1
10
1 1 2 2 1 1 3 3 1 1
stdout
0 1 
0 1 2 3 
0 1 2 3 4 5 6 7 
0 1 2 3 4 5 6 7 8 9