From b66c15c9ae28ec03d968a8f253e5d87b4ac6883a Mon Sep 17 00:00:00 2001 From: Pravin Ranjan Date: Wed, 28 Dec 2022 16:28:47 +0530 Subject: [PATCH] max sum of subarray --- DynamicPrograms/maxSubArray.go | 59 ++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 DynamicPrograms/maxSubArray.go diff --git a/DynamicPrograms/maxSubArray.go b/DynamicPrograms/maxSubArray.go new file mode 100755 index 0000000..5410f2a --- /dev/null +++ b/DynamicPrograms/maxSubArray.go @@ -0,0 +1,59 @@ +/* +53. Maximum Subarray + +Given an integer array nums, find the subarray +which has the largest sum and return its sum. + +Example 1: + +Input: nums = [-2,1,-3,4,-1,2,1,-5,4] +Output: 6 +Explanation: [4,-1,2,1] has the largest sum = 6. +Example 2: + +Input: nums = [1] +Output: 1 +Example 3: + +Input: nums = [5,4,-1,7,8] +Output: 23 +*/ + +// solution: +/* + 1. Find the currSum at every index, by adding curSum and current value. + 2. If currSum > maxSum: + maxSum = curSum + 3. if currSum < 0, means, adding current value is not meaningful, then make + curSum = 0, and proceed for next element +*/ + +package main + +import "fmt" + +func maxSubArray(nums []int) int { + n := len(nums) + maxSum := nums[0] + currSum := 0 + for i := 0; i < n; i++ { + currSum = currSum + nums[i] + + if currSum > maxSum { + maxSum = currSum + } + + if currSum < 0 { + currSum = 0 + } + } + return maxSum +} + +func main() { + nums := []int{-2, 1, -3, 4, -1, 2, 1, -5, 4} + fmt.Println("max subarray sum:", maxSubArray(nums)) +} + +// output: +// max subarray sum: 6