InterviewHubPRO
Section Overview
Q10

Question 10 of 100

Part 2: Arrays, Strings & Hash Maps (6 - 18)

10%
DSA Patterns (Multi-Lang)•Part 2: Arrays, Strings & Hash Maps (6 - 18)

Maximum Subarray (Kadane’s Algorithm).

In Simple Words: Iterate through the array keeping track of currentSum. At each element, decide whether to add it to currentSum or start a new subarray fresh from nums[i].

  • Time: $O(N)$ | Space: $O(1)$
Multi-Language Solutions
Switch tabs or choose All
def maxSubArray(nums: list[int]) -> int:
    max_so_far = nums[0]
    curr = nums[0]
    for n in nums[1:]:
        curr = max(n, curr + n)
        max_so_far = max(max_so_far, curr)
    return max_so_far