Created
May 24, 2021 15:08
-
-
Save kharioki/2d9bba0090a110ad2ff7c2b76f464b3f to your computer and use it in GitHub Desktop.
Finding the maximum subarray.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let arr1 = [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7]; | |
| function maxSubArray (nums) { | |
| let maxC = nums[0]; | |
| let maxG = nums[0]; | |
| for (let i = 1; i < nums.length; i++) { | |
| maxC = Math.max(nums[i], maxC + nums[i]); | |
| maxG = maxC > maxG ? maxC : maxG; | |
| } | |
| // console.log(maxG); | |
| return maxG; | |
| } | |
| maxSubArray(arr1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment