-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kadane's Algorithm.cpp
68 lines (43 loc) · 1.11 KB
/
Kadane's Algorithm.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
Task: Given an array arr of N integers. Find the contiguous sub-array with maximum sum.
Example 1:
Input:
N = 5
arr[] = {1,2,3,-2,5}
Output:
9
Explanation:
Max subarray sum is 9
of elements (1, 2, 3, -2, 5) which
is a contiguous subarray.
Example 2:
Input:
N = 4
arr[] = {-1,-2,-3,-4}
Output:
-1
Explanation:
Max subarray sum is -1
of element (-1)
Your Task:
You don't need to read input or print anything. The task is to complete the function maxSubarraySum() which takes arr and N as input parameters and returns the sum of subarray with maximum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 106
-107 ≤ A[i] <= 107
Solution:
int maxSubarraySum(int arr[], int n){
// Your code here
int max_so_far = INT_MIN, max_ending_here = 0;
for(int i=0;i<n;i++){
max_ending_here+=arr[i];
if(max_so_far<max_ending_here){
max_so_far=max_ending_here;
}
if(max_ending_here<0){
max_ending_here=0;
}
}
return max_so_far;
}
};