-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc14.c
More file actions
40 lines (35 loc) · 974 Bytes
/
c14.c
File metadata and controls
40 lines (35 loc) · 974 Bytes
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
//Subarray with given sum (Two pointer / Sliding window)
#include <stdio.h>
void subarrayWithSum(int arr[], int n, int target)
{
int start = 0, end = 0, curr_sum = 0;
while (end < n)
{
curr_sum += arr[end];
// Shrink window from left if sum exceeds target
while (curr_sum > target && start < end)
{
curr_sum -= arr[start];
start++;
}
// Check if current window matches target
if (curr_sum == target)
{
printf("Subarray found from index %d to %d:\n", start, end);
for (int i = start; i <= end; i++)
printf("%d ", arr[i]);
printf("\n");
return;
}
end++;
}
printf("No subarray with sum %d found.\n", target);
}
int main()
{
int arr[] = {1, -4, 20, -3, 10, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 30;
subarrayWithSum(arr, n, target);
return 0;
}