Skip to content

Commit d29dff1

Browse files
authored
Merge pull request #11 from vishwak1002/main
Added Maximum Subarray Sum
2 parents 9897674 + 0e8a4fc commit d29dff1

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ Write a function called `removeDuplicates` that takes an array as its parameter
122122

123123
[Solution Explanation](./solutions/ch_13_Remove_Duplicate_From_Array/readme.md)
124124

125+
## Challenge 14: Find the Maximum Subarray Sum (Kadane's Algorithm)
126+
Write a function that takes an array as input and returns the Maximum subarray sum. Subarrays are arrays inside another array which only contains contiguous elements. For example, if the array is [-3, -4, 5, -1, 2, -4, 6, -1], the function should return 8 since subarray [5, -1, 2, -4, 6] is the max sum contiguous subarray with sum 8.
127+
128+
Write a function `MaxSubArraySum` that takes an array as its parameter and returns the maximum subarray sum.
129+
130+
[Solution Explanation](./solutions/ch_14_Max_Subarray_Sum/readme.md)
131+
125132
<!-- Add new challenges before this comment -->
126133

127134
## License
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
function MaxSubArraySum(arr)
3+
{
4+
var max = Number.MIN_VALUE
5+
var temp_max = 0
6+
7+
for (var i = 0; i < arr.length; i++)
8+
{
9+
temp_max = temp_max + arr[i]
10+
if (max < temp_max)
11+
max = temp_max
12+
13+
if (temp_max < 0)
14+
temp_max = 0
15+
}
16+
return max
17+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Challenge 13: Find the Maximum Subarray Sum (Kadane's Algorithm)
2+
Write a function that takes an array as input and returns the Maximum subarray sum. Subarrays are arrays inside another array which only contains contiguous elements. For example, if the array is [-3, -4, 5, -1, 2, -4, 6, -1], the function should return 8 since subarray [5, -1, 2, -4, 6] is the max sum contiguous subarray with sum 8.
3+
4+
Write a function `MaxSubArraySum` that takes an array as its parameter and returns the maximum subarray sum.
5+
6+
## Answer
7+
8+
```javascript
9+
function MaxSubArraySum(arr)
10+
{
11+
var max = Number.MIN_VALUE
12+
var temp_max = 0
13+
14+
for (var i = 0; i < arr.length; i++)
15+
{
16+
temp_max = temp_max + arr[i]
17+
if (max < temp_max)
18+
max = temp_max
19+
20+
if (temp_max < 0)
21+
temp_max = 0
22+
}
23+
return max
24+
}
25+
```
26+
27+
## Answer Explanation
28+
29+
The function takes an array `arr` as input and returns the maximum subarray sum .First we assign a `max` variable and set it to Number.Minimum Value and then we take a temporary variable `temp_max` .Then we loop over the array and assign the value of `max` only when the `temp_max` value is greater than `max` value.In the last phase of loop , we check if the value of temp_max is less than zero and if it is ,then we change the value of `temp_num` to zero.
30+
Finally we return the max value to get the Maximum Subarray Sum.
31+
32+
Here's an example usage of the function.
33+
34+
```javascript
35+
const arr = [-2, 3, -1, 2];
36+
const max = MaxSubArraySum(arr);
37+
console.log(max); // Output: 4
38+
```
39+
In this example, the `MaxSubArraySum` function correctly returns the Maximum SubArray Sum of the array.

0 commit comments

Comments
 (0)