Given a list of integers (which may include negative numbers), find the contiguous subarray with the largest sum and return that sum. This is known as the Maximum Subarray Problem, which can be solved efficiently using Kadane's algorithm. Return null for null input and 0 for empty list.
Examples:
Input: numbers = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
Output: 6
Explanation: The contiguous subarray [4, -1, 2, 1] has the largest sum = 6
Input: numbers = [1, 2, 3, 4]
Output: 10
Explanation: The entire array [1, 2, 3, 4] has the largest sum = 10
Input: numbers = [-1, -2, -3]
Output: -1
Explanation: The single element [-1] has the largest sum = -1
Input: numbers = [5]
Output: 5
Explanation: Single element array returns that element
Input: numbers = []
Output: 0
Explanation: Empty list returns 0
Input: numbers = null
Output: null
Explanation: Null input returns null
Apex Code Editor
Welcome to Lightning Challenge!
Create an Account
Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.
Create accountHow It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay Feb 2026
Contest runs February 1 - 28. Complete challenges to climb the leaderboard!
Points are calculated for challenges completed during the contest period.