/** * @param {number[]} nums * @return {number} */ var maxSumOfSubArray = function(nums) { let localMax =0; let GlobalMax = Number.NEGATIVE_INFINITY; for(let i=0; i<nums.length; i++){ localMax = localMax+ nums[i]; GlobalMax = Math.max(localMax, GlobalMax); if(localMax <0){ localMax =0; } } return GlobalMax; };
In this problem we need to find the maximum sum of a subarray. We used Kadane’s Algorithm to solve this.
for e.g
Array = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Subarray : [4,-1,2,1] has the largest sum = 6.
Leave a Reply