fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int n;
  7. cin>>n;
  8.  
  9. int b[n+1]; // 1 - based indexing
  10. for(int i=1; i<=n; i++)
  11. {
  12. cin>>b[i];
  13. }
  14.  
  15. int k;
  16. cin>>k;
  17.  
  18. int count = 0;
  19. int c = 0;
  20. for(int i=1; i<=n; i++)
  21. {
  22. if( i>=2 && (b[i] > b[i-1]))
  23. {
  24. c = c + 1; // Increment 'c' and you will be knowing the size of the island
  25. }
  26. else
  27. {
  28. // now you know the size of previous island which is 'c'
  29. int actual = c + 1;
  30.  
  31. if(actual>=k && k>=2)
  32. {
  33. count = count + (actual - k ) + 1;
  34. }
  35. c = 0;
  36. }
  37. }
  38.  
  39. // now to take care of the last island
  40. // ------------------- same here the above else condition logic copy paste here
  41. int actual = c + 1;
  42. if(actual>=k && k>=2)
  43. {
  44. count = count + (actual - k) + 1 ;
  45. }
  46. //------------------------------------------
  47.  
  48. if(k==1) // means the single element is subarrray so print n
  49. {
  50. cout<<n;
  51. }
  52. else// if k is not 1
  53. {
  54. cout<<count; // then print the count
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61. return 0;
  62. }
Success #stdin #stdout 0s 5340KB
stdin
8
5 3 5 7 8 1 2 3 
2
stdout
5