fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int n;
  7. cin>>n; // 5
  8.  
  9. int b[n+1]; // 1 - based indexing
  10. for(int i=1; i<=n; i++)
  11. {
  12. cin>>b[i]; // 1 2 3 4 5
  13. }
  14.  
  15. int k;
  16. cin>>k; // 3
  17.  
  18. int count = 0;
  19. for(int i=1; i<=n-k+1; i++) // i=1 starting point till n-k+1 means end point 5 - 3 + 1 = 3 --> i < 3
  20. {
  21. // i --> start point of Each Subarray of size 'k' which we are considering
  22. int start = i;
  23. bool check = true;
  24. // 'i' index it means start index + k - 1
  25. int end = i + k - 1; // 1 + 3 -1 --> 3 it means till third index it will go
  26.  
  27. while(start < end) // Becoz I am comparing start with one index Ahead which is start + 1
  28. {
  29. if(b[start] < b[start + 1]) // All the Adjacent Elements should be increasing order
  30. {
  31. check = true;
  32. }
  33. else
  34. {
  35. check = false;
  36. }
  37. start = start + 1;
  38. // O(k)
  39. }
  40. // O(N)
  41.  
  42. if(check == true)
  43. {
  44. count = count + 1;
  45. }
  46. }
  47. cout<<count;
  48.  
  49. return 0;
  50. }
  51.  
  52. /*
  53. input:
  54. 5
  55. 1 2 3 4 5
  56. 3
  57.  
  58. output:
  59. 3
  60.  
  61. How did the output 3 came
  62. 1 < 2 < 3 < 4 < 5
  63.  
  64. k = 3 subarray size should be 3
  65.  
  66. 1 < 2 < 3 --> 1 subarray
  67. 2 < 3 < 4 --> 2 subarray
  68. 3 < 4 < 5 --> 3 subarray
  69.  
  70.  
  71. */
Success #stdin #stdout 0s 5348KB
stdin
5
1 2 3 4 5
3
stdout
3