From b63cee1dbcc3d9e219e1be1a1f00ad423a5feebc Mon Sep 17 00:00:00 2001 From: Rituja Date: Thu, 1 Oct 2020 17:24:49 +0530 Subject: [PATCH] added LargestSubarraySum algorithm --- LargestSubarraySum_6/LargestSubarraySum.js | 16 +++++++++ .../LargestSubarraySum.test.js | 33 +++++++++++++++++++ README.md | 22 ++++++++++--- 3 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 LargestSubarraySum_6/LargestSubarraySum.js create mode 100644 LargestSubarraySum_6/LargestSubarraySum.test.js diff --git a/LargestSubarraySum_6/LargestSubarraySum.js b/LargestSubarraySum_6/LargestSubarraySum.js new file mode 100644 index 0000000..4d4c1b4 --- /dev/null +++ b/LargestSubarraySum_6/LargestSubarraySum.js @@ -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 diff --git a/LargestSubarraySum_6/LargestSubarraySum.test.js b/LargestSubarraySum_6/LargestSubarraySum.test.js new file mode 100644 index 0000000..0b9aa05 --- /dev/null +++ b/LargestSubarraySum_6/LargestSubarraySum.test.js @@ -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) + }) + + }) diff --git a/README.md b/README.md index a78cad2..c19da9a 100644 --- a/README.md +++ b/README.md @@ -447,16 +447,30 @@ method tests whether all elements pass the test or not which is implemented by p

-6. Name +6. Largest SubArray Sum -__The challenge:__

+__The challenge:__

Write an efficient program to find the sum of contiguous subarray within a one-dimensional array of numbers which has the largest sum.

-__Algorithmic Thinking:__

+__Algorithmic Thinking:__

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

-__code Implementation:__

+__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 + + } + ```