fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. int n;
  7. cin>>n;
  8. vector<int>arr(n,0);
  9. for(int i=0;i<n;i++){
  10. cin>>arr[i];
  11. }
  12. int k1;
  13. cin>>k1;
  14. int count=0;
  15. for(int i=0;i<n;i++){
  16. int maxi=-1e9;
  17. for(int j=i;j<n;j++){
  18. int sub=arr[j];
  19. maxi=max(maxi,sub);
  20. if(maxi==k1){
  21. count++;
  22. }
  23. }
  24. }
  25. cout<<"The number of subarrays with max element k is:"<<count;
  26. return 0;
  27. }
Success #stdin #stdout 0s 5308KB
stdin
5
8 2 5 1 10
5
stdout
The number of subarrays with max element k is:4