All submissions

Sliding Window Sum

Code

#include <stdio.h>

void sliding_window_sum(int arr[], int n, int k) {
    // Your logic here
    for (int i = 0; i < n-k+1; ++i) {
        int sum = 0;
        for (int j = 0; j < k; ++j) {
            sum += arr[i+j];
        }

        printf("%d", sum);
        if (i < n-k) {
            printf(" ");
        }
    }
}

int main() {
    int n, k, arr[100];
    scanf("%d %d", &n, &k);
    for (int i = 0; i < n; i++) scanf("%d", &arr[i]);

    sliding_window_sum(arr, n, k);
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

5 3 1 2 3 4 5

Expected Output

6 9 12