Skip to content

added LargestSubarraySum algorithm #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions LargestSubarraySum_6/LargestSubarraySum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Algorithm :- https://www.geeksforgeeks.org/largest-sum-contiguous-subarray/

let largestSubarraySum = (inputArray) => {
let currentSum = 0
let largestSum = 0

for(let number of inputArray){
currentSum = Math.max(0, (currentSum + number))
largestSum = Math.max(largestSum, currentSum)
}

return largestSum

}

module.exports = largestSubarraySum
33 changes: 33 additions & 0 deletions LargestSubarraySum_6/LargestSubarraySum.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const largestSubarraySum = require('./LargestSubarraySum.js');

describe("#largestSubarraySum", () => {

test("it returns the largest subarray sum given an array of positive integers", () => {
expect(largestSubarraySum([1, 100, 4, 15, 9, 30])).toEqual(159)
})

test("it returns the largest subarray sum given an array ending with negative numbers", () => {
expect(largestSubarraySum([1, 100, 4, 15, 9, 30, -1])).toEqual(159)
})

test("it returns the largest subarray sum given an array starting with negative numbers", () => {
expect(largestSubarraySum([-3, 1, 100, 4, 15, 9, 30])).toEqual(159)
})

test("it returns the largest subarray sum given an array starting and ending with negative numbers", () => {
expect(largestSubarraySum([-3, 1, 100, 4, 15, 9, 30, -1])).toEqual(159)
})

test("it returns the largest subarray sum given an array with negative numbers throughout", () => {
expect(largestSubarraySum([-2, -3, 4, -1, -2, 1, 5, -3])).toEqual(7)
})

test("it returns the largest subarray sum given an array where sum !== 0 from left to right", () => {
expect(largestSubarraySum([2, 1, -3, 4, -1, 2, 1, -5, 4])).toEqual(6)
})

test("it returns 0 given an array of all negative integers", () => {
expect(largestSubarraySum([-1, -1, -5, -3, -7, -4, -5, -6, -100, -4])).toEqual(0)
})

})
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,16 +447,30 @@ method tests whether all elements pass the test or not which is implemented by p
<hr>
<hr>

<b>6. Name </b>
<b>6. Largest SubArray Sum </b>

__The challenge:__ <p> </p>
__The challenge:__ <p> Write an efficient program to find the sum of contiguous subarray within a one-dimensional array of numbers which has the largest sum. </p>


__Algorithmic Thinking:__ <p> </p>
__Algorithmic Thinking:__ <p> Simple idea of the Kadane’s algorithm is to look for all positive contiguous segments of the array (max_ending_here is used for this). And keep track of maximum sum contiguous segment among all positive segments (max_so_far is used for this). Each time we get a positive sum compare it with max_so_far and update max_so_far if it is greater than max_so_far </p>


__code Implementation:__ <p> </p>
__code Implementation:__

```js
let largestSubarraySum = (inputArray) => {
let currentSum = 0
let largestSum = 0

for(let number of inputArray){
currentSum = Math.max(0, (currentSum + number))
largestSum = Math.max(largestSum, currentSum)
}

return largestSum

}
```
<hr>
<hr>

Expand Down