forked from dimpeshmalviya/C-Language-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKadane's algorithm.c
More file actions
50 lines (39 loc) · 1.22 KB
/
Kadane's algorithm.c
File metadata and controls
50 lines (39 loc) · 1.22 KB
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
#include <stdio.h>
#include <limits.h>
void kadane(int arr[], int n) {
int max_so_far = INT_MIN; // Holds the maximum sum found so far
int max_ending_here = 0; // Holds the current subarray sum
int start = 0, end = 0, s = 0; // To track start and end indices of the subarray
for (int i = 0; i < n; i++) {
max_ending_here += arr[i];
// Update max_so_far if we found a new maximum
if (max_so_far < max_ending_here) {
max_so_far = max_ending_here;
start = s;
end = i;
}
// If current sum becomes negative, reset it and move the start index
if (max_ending_here < 0) {
max_ending_here = 0;
s = i + 1;
}
}
printf("\nMaximum Subarray Sum = %d\n", max_so_far);
printf("Subarray indices: %d to %d\n", start, end);
printf("Subarray elements: ");
for (int i = start; i <= end; i++)
printf("%d ", arr[i]);
printf("\n");
printf("\n");
}
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
kadane(arr, n);
return 0;
}