fork download
  1. // C++ program for the above approach
  2.  
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. // Method to find the maximum for each
  7. // and every contiguous subarray of size K.
  8. void printKMax(int arr[], int N, int K)
  9. {
  10. int j, max;
  11.  
  12. for (int i = 0; i <= N - K; i++) {
  13. max = arr[i];
  14.  
  15. for (j = 1; j < K; j++) {
  16. if (arr[i + j] > max)
  17. max = arr[i + j];
  18. }
  19. cout << max << " ";
  20. }
  21. }
  22.  
  23. // Driver's code
  24. int main()
  25. {
  26. int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  27. int N = sizeof(arr) / sizeof(arr[0]);
  28. int K = 3;
  29.  
  30. // Function call
  31. printKMax(arr, N, K);
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
3 4 5 6 7 8 9 10