🤖
@qwertyvipul | code
  • Code
  • DSA
    • Disjoint Set
    • Segment Tree
    • Bit Operations
    • Binary Exponential
    • Kadane's Algorithm
    • Modulus Multiplicative Inverse
  • Quick Notes
    • Design Patterns
    • System Design
    • React.js
  • LeetCode With JavaScript
Powered by GitBook
On this page
  • Code
  • Test
  1. DSA

Kadane's Algorithm

Code

def maxSubArraySum(nums):
    globalMax = float("-inf")
    localMax = 0
    
    for num in nums:
        localMax = localMax + num
        globalMax = max(globalMax, localMax)
        if localMax < 0: localMax = 0
            
    return globalMax

Test

nums = [-2, -3, 4, -1, -2, 1, 5, -3]
print(maxSubArraySum(nums)) # 7
PreviousBinary ExponentialNextModulus Multiplicative Inverse

Last updated 1 year ago