
Problem
Solution
Submissions
Maximum Subarray
Certification: Basic Level
Accuracy: 100%
Submissions: 10
Points: 5
Write a Java program to find the contiguous subarray which has the largest sum and return its sum using Kadane's Algorithm.
Example 1
- Input: nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
- Output: 6
- Explanation:
- Subarray [4, -1, 2, 1] has the maximum sum
Example 2
- Input: nums = [1]
- Output: 1
- Explanation:
- Only one element — that is the maximum
Constraints
- 1 ≤ nums.length ≤ 10^5
- -10^4 ≤ nums[i] ≤ 10^4
- Time Complexity: O(n)
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use Kadane's algorithm to solve this efficiently
- Keep track of the current sum and the maximum sum found so far
- If the current sum becomes negative, reset it to zero
- Consider the edge case of all negative numbers
- Iterate through the array only once