Maximum Subarray LeetCode Soution
Maximum Subarray LeetCode Solution :
Given an integer array nums, find the subarray with the largest sum, and return its sum.
The Maximum Subarray Problem can be solved using various algorithms, including the Kadane’s algorithm, which efficiently finds the maximum subarray sum in a single pass through the array.
Example 1 :
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation : The subarray [4,-1,2,1] has the largest sum 6.
Maximum Subarray LeetCode Solution :
Constraints :
1 <= nums.length <= 105
-104 <= nums[i] <= 104
Example 2 :
Input: nums = [1]
Output: 1
Explanation : The subarray [1] has the largest sum 1.
Example 3 :
Input:nums = [5,4,-1,7,8]
Output: 23
Explanation : The subarray [5,4,-1,7,8] has the largest sum 23.
Approach for Maximum Subarray LeetCode Solution : :
- We start by initializing two variables: maxSum and currentSum.
maxSum
represents the maximum sum encountered so far and is initially set to the minimum possible integer value to ensure that any valid subarray sum will be greater than it.currentSum
represents the current sum of the subarray being considered and is initially set to 0.
- We iterate through the
nums
array using a for loop, starting from the first element and going up to the last element. - For each element in the array, we add it to the current sum
currentSum
. This calculates the sum of the subarray ending at the current element. - Next, we check if the current sum
currentSum
is greater than the current maximum summaxSum
.- If it is, we update
maxSum
with the new value ofcurrentSum
. This means we have found a new maximum subarray sum.
- If it is, we update
- If the current sum
currentSum
becomes negative, it indicates that including the current element in the subarray would reduce the overall sum. In such cases, we resetcurrentSum
to 0. This effectively discards the current subarray and allows us to start a fresh subarray from the next element. - We repeat steps 3 to 5 for each element in the array.
- After iterating through the entire array, the variable
maxSum
will contain the maximum subarray sum encountered. - Finally, we return the value of
maxSum
as the result, representing the maximum sum of a contiguous subarray within the given array nums.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Code for Maximum Subarray LeetCode Solution : :
C++
Java
Python
C++
class Solution { public: int maxSubArray(vector& nums) { int maxSum = INT_MIN; int currentSum = 0; for (int i = 0; i < nums.size(); i++) { currentSum += nums[i]; if (currentSum > maxSum) { maxSum = currentSum; } if (currentSum < 0) { currentSum = 0; } } return maxSum; } };
Java
class Solution { public int maxSubArray(int[] nums) { int maxSum = Integer.MIN_VALUE; int currentSum = 0; for (int i = 0; i < nums.length; i++) { currentSum += nums[i]; if (currentSum > maxSum) { maxSum = currentSum; } if (currentSum < 0) { currentSum = 0; } } return maxSum; } }
Python
class Solution: def maxSubArray(self, nums: List[int]) -> int: maxSum = float('-inf') currentSum = 0 for num in nums: currentSum += num if currentSum > maxSum: maxSum = currentSum if currentSum < 0: currentSum = 0 return maxSum
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment