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. int last=-1;
  6. int found=-1;
  7. for(int i=0;i<n;i++){
  8. if(arr[i]>k){
  9. last=i;
  10. }
  11. if(arr[i]==k){
  12. found=i;
  13. }
  14. if(last>found){
  15. count=count+(last-found);
  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 5320KB
stdin
5
8 2 5 1 10
5
stdout
The count of subarray with maximum element k is:4