Maximum Subarray Sum
Maximum Subarray Sum
Given an integer array of n elements, find the contiguous subarray (containing at least one number) that has the largest sum and return its sum. A contiguous subarray is a sequence of consecutive elements from the original array. You cannot skip elements. Your solve(...) function receives these inputs directly as ready-made arguments — no input reading or parsing needed. Just compute and return the answer.
Input format — only if your code reads from stdin
Input format (stdin): - First line: integer n (the size of the array) - Second line: n space-separated integers Output: a single integer — the maximum subarray sum.
Approach
Apply Kadane's Algorithm: track a running sum and reset it when it drops below zero. Keep a global maximum updated at every step.
Input
Output
- •
1 ≤ n ≤ 10^5, -10^4 ≤ nums[i] ≤ 10^4
More Arrays problems
Browse all coding problems →Press Run to test your custom input, or Submit to judge against all test cases.
Sign in to continue
Create a free account to run code, submit solutions and track progress.
- Free forever — no credit card
- 10+ languages with live execution
- Points, streaks & leaderboards