fork download
  1. /*Write a program to take an integer array arr and an integer k as inputs. The task is to find the first negative
  2. integer in each subarray of size k moving from left to right. If no negative exists in a window, print "0" for that
  3. window. Print the results separated by spaces as output.*/
  4. #include <stdio.h>
  5.  
  6. int main() {
  7. int n, k;
  8. scanf("%d %d", &n, &k);
  9.  
  10. int arr[n];
  11. for (int i = 0; i < n; i++)
  12. scanf("%d", &arr[i]);
  13.  
  14. for (int i = 0; i <= n - k; i++) {
  15. int found = 0;
  16.  
  17. for (int j = i; j < i + k; j++) {
  18. if (arr[j] < 0) {
  19. printf("%d", arr[j]);
  20. found = 1;
  21. break;
  22. }
  23. }
  24.  
  25. if (!found) printf("0");
  26.  
  27. if (i != n - k) printf(" ");
  28. }
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0.01s 5288KB
stdin
7 3
12 -1 -7 8 -15 30 16
stdout
-1 -1 -7 -15 -15