-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find minimum sum subarray of size `k`
54 lines (42 loc) · 1.19 KB
/
Find minimum sum subarray of size `k`
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <limits.h>
// Find the minimum sum subarray of a given size `k`
void findSubarray(int arr[], int n, int k)
{
// base case
if (n == 0 || n <= k) {
return;
}
// stores the sum of elements in the current window
int window_sum = 0;
// stores the sum of minimum sum subarray found so far
int min_window = INT_MAX;
// stores ending index of the minimum sum subarray found so far
int last = 0;
for (int i = 0; i < n; i++)
{
// add the current element to the window
window_sum += arr[i];
// if the window size is more than equal to `k`
if (i + 1 >= k)
{
// update the minimum sum window
if (min_window > window_sum)
{
min_window = window_sum;
last = i;
}
// remove a leftmost element from the window
window_sum -= arr[i + 1 - k];
}
}
printf("The minimum sum subarray is (%d, %d)", last - k + 1, last);
}
int main(void)
{
int arr[] = { 10, 4, 2, 5, 6, 3, 8, 1 };
int k = 3;
int n = sizeof(arr)/sizeof(arr[0]);
findSubarray(arr, n, k);
return 0;
}