fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. vector<int> printLeadersBruteForce(int arr[], int n) {
  5.  
  6. vector<int> ans;
  7.  
  8. for (int i = 0; i < n; i++) {
  9. bool leader = true;
  10.  
  11. //Checking whether arr[i] is greater than all
  12. //the elements in its right side
  13. for (int j = i + 1; j < n; j++)
  14. if (arr[j] > arr[i]) {
  15.  
  16. // If any element found is greater than current leader
  17. // curr element is not the leader.
  18. leader = false;
  19. break;
  20. }
  21.  
  22. // Push all the leaders in ans array.
  23. if (leader)
  24. ans.push_back(arr[i]);
  25.  
  26. }
  27.  
  28. return ans;
  29. }
  30.  
  31. int main() {
  32.  
  33. // Array Initialization.
  34. int n = 6;
  35. int arr[n] = {10, 22, 12, 3, 0, 6};
  36.  
  37. vector<int> ans = printLeadersBruteForce(arr,n);
  38.  
  39. for(int i = 0;i<ans.size();i++){
  40.  
  41. cout<<ans[i]<<" ";
  42. }
  43.  
  44. cout<<endl;
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0.01s 5268KB
stdin
Standard input is empty
stdout
22 12 6