fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int getCount(int n,vector<int>&arr,int k){
  4. int count=0;
  5. for(int i=0;i<n;i++){
  6. int maxi=arr[i];
  7. bool got=(arr[i]==k);
  8. for(int j=i;j<n;j++){
  9. if(arr[j]==k){
  10. got=true;
  11. }
  12. maxi=max(maxi,arr[j]);
  13. if(got==true && maxi==k){
  14. count++;
  15. }
  16. }
  17. }
  18. return count;
  19. }
  20.  
  21. int main() {
  22. // your code goes here
  23. int n;
  24. cin>>n;
  25. vector<int>v(n);
  26. for(int i=0;i<n;i++){
  27. cin>>v[i];
  28. }
  29. int k;
  30. cin>>k;
  31. cout<<"The count of subarray with maximum element k is:"<<getCount(n,v,k);
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0.01s 5272KB
stdin
5
8 2 5 1 10
5
stdout
The count of subarray with maximum element k is:4