Skip to content

Commit d28480b

Browse files
author
Arpit Sharma
committed
Kadenes algorithim code simplified
1 parent b40a11e commit d28480b

File tree

2 files changed

+98
-87
lines changed

2 files changed

+98
-87
lines changed

Famous Algorithms/kadanes_algorithm.js

-87
This file was deleted.
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/* What is Kadane's Algorithm?
2+
Kadane's Algorithm is a way to find the maximum subarray sum in an array with a runtime of O(n).
3+
It is a dynamic programming algorithm that uses the fact that the maximum subarray sum ending at index i is either the value at index i or the maximum subarray sum ending at index i-1 plus the value at index i.
4+
5+
Note: The subarray must be contiguous, all the values in the subarray must be next to each other in the original array.
6+
7+
How does it work?
8+
In this algorithim we maintain two variables, one will hold the maximum sum of contagious subarray and the other variable will hold sum of next element + current sum from previous iteration.
9+
If at any point the current sum + next element sum is less then 0 then we will reset the current sum to 0 and current sum + next element sum will start again from the next element.
10+
If the current sum + next element sum is greater then 0 and it is also greater then the maximum sum of contagious subarray then the variable of maximum sum of contagious subarray will be updated with current sum + next element sum
11+
12+
13+
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3}
14+
In this example maxLargestSumTillNow holds the maximum sum of contagious subarray and newLargestSum hold the value of current sum + next element
15+
On initalizing max so far will be the max -ve number
16+
maxLargestSumTillNow = INT_MIN
17+
newLargestSum = 0
18+
19+
for i=0, a[0] = -2
20+
newLargestSum = newLargestSum + (-2)
21+
Set newLargestSum = 0 because newLargestSum < 0
22+
and set maxLargestSumTillNow = -2
23+
24+
for i=1, a[1] = -3
25+
newLargestSum = newLargestSum + (-3)
26+
Since newLargestSum = -3 and maxLargestSumTillNow = -2, maxLargestSumTillNow will remain -2
27+
Set newLargestSum = 0 because newLargestSum < 0
28+
29+
for i=2, a[2] = 4
30+
newLargestSum = newLargestSum + (4)
31+
newLargestSum = 4
32+
maxLargestSumTillNow is updated to 4 because newLargestSum greater
33+
than maxLargestSumTillNow which was -2 till now
34+
35+
for i=3, a[3] = -1
36+
newLargestSum = newLargestSum + (-1)
37+
newLargestSum = 3
38+
39+
for i=4, a[4] = -2
40+
newLargestSum = newLargestSum + (-2)
41+
newLargestSum = 1
42+
43+
for i=5, a[5] = 1
44+
newLargestSum = newLargestSum + (1)
45+
newLargestSum = 2
46+
47+
for i=6, a[6] = 5
48+
newLargestSum = newLargestSum + (5)
49+
newLargestSum = 7
50+
maxLargestSumTillNow is updated to 7 because newLargestSum is
51+
greater than maxLargestSumTillNow
52+
53+
for i=7, a[7] = -3
54+
newLargestSum = newLargestSum + (-3)
55+
newLargestSum = 4
56+
57+
Time Complexity: O(n)
58+
Space Complexity: O(1)
59+
*/
60+
61+
function largestSumOfSubArray(arr) {
62+
if (arr.lenth == 1) {
63+
return arr[0];
64+
}
65+
66+
// Variable for maintaining Maximum sum of the subarray
67+
var maxint = Math.pow(2, 53);
68+
var maxLargestSumTillNow = -maxint - 1;
69+
70+
// Variable to calclate the sum of subarray after each iteration
71+
var newLargestSum = 0;
72+
73+
// Looping through the entire array
74+
for (i = 0; i < arr.length - 1; i++) {
75+
// Calculating the largest sum on each iteration
76+
newLargestSum += arr[i];
77+
78+
// If the largest sum value is greater then the maximum largest subarray value we have maintained then we will assign new value to maintained maximum largest subarray
79+
if (maxLargestSumTillNow < newLargestSum) {
80+
maxLargestSumTillNow = newLargestSum;
81+
}
82+
83+
// If the largest sum is negative then we will reset the value of largest sum to 0 and start the calculation again of largest sum from next element
84+
if (newLargestSum < 0) {
85+
newLargestSum = 0;
86+
}
87+
}
88+
89+
// After the completion of iteration we will return the max largest sub array value
90+
return maxLargestSumTillNow;
91+
}
92+
93+
// Driver code
94+
var arr = [-2, -3, 4, -1, -2, 1, 5, -3];
95+
console.log(largestSumOfSubArray(arr));
96+
97+
// Input: arr = [-2, -3, 4, -1, -2, 1, 5, -3];
98+
//Output: 7

0 commit comments

Comments
 (0)