Logo

dev-resources.site

for different kinds of informations.

Kadane's Algorithm: Leetcode 53 Maximum subarray

Published at
1/10/2025
Categories
java
algorithms
leetcode
datastructures
Author
devn913
Author
7 person written this
devn913
open
Kadane's Algorithm: Leetcode 53 Maximum subarray

Intuition

We can build the intuition based on the two-point approach.

Approach

We will start with two variables maxSum and maxTillNow.

  • The first variable stores the max sum we have attained overall in the array.

  • The second variable stores the value of the maximum sum attained till the current index. Since the array has a negative value, this value will fluctuate, but whenever we get maxSum < maxTillNow, we update the maxSum.

  • The final case we have to handle will be if the maximum sum till any index reaches zero, i.e., maxTillNow < 0, reset the maxTillNow = 0 at the end of loop.

Complexity

  • Time complexity: O(N)

  • Space complexity: O(1)

Code

class Solution {
    public int maxSubArray(int[] nums) {
        int maxSum = Integer.MIN_VALUE;
        int maxTillNow = 0;
        for(int i =0;i<nums.length;i++){
            maxTillNow+=nums[i];
            maxSum = Math.max(maxTillNow,maxSum);
            if(maxTillNow<0) maxTillNow = 0;
        }
        return maxSum;
    }
}
Enter fullscreen mode Exit fullscreen mode

GitHub repo for more solutions: Git
Leetcode profile: Leetcode: devn007

algorithms Article's
30 articles in total
Favicon
From Bootcamp to Senior Engineer: Growing, Learning, and Feeling Green
Favicon
Neetcode Roadmap Part 1
Favicon
A técnica dos dois ponteiros
Favicon
2429. Minimize XOR
Favicon
How to learn DSA , Complete Roadmap with Resources
Favicon
2657. Find the Prefix Common Array of Two Arrays
Favicon
Wanna Know Big O Basics!
Favicon
Time Complexity, Big-O for Beginners
Favicon
I am a beginner in Python programming and I want to develop my skills.
Favicon
3223. Minimum Length of String After Operations
Favicon
LinearBoost: Faster Than XGBoost and LightGBM, Outperforming Them on F1 Score on Seven Famous Benchmark Datasets
Favicon
Hoof It
Favicon
2116. Check if a Parentheses String Can Be Valid
Favicon
Understanding Quick Sort in Kotlin : A Beginner's Guide
Favicon
External Merge Problem - Complete Guide for Gophers
Favicon
Greedy Algorithm With Examples
Favicon
From 0 to O(n): A Beginner's Guide to Big O Notation
Favicon
Understanding Selection Sort in Kotlin: A Beginner's Guide
Favicon
Entity and Relationship
Favicon
HackerRank’s Flipping the Matrix Problem
Favicon
Understanding the XOR Operator: A Powerful Tool in Computing
Favicon
I am currently reducing at least 22 proofs by at least 99312 steps total.
Favicon
Kadane's Algorithm: Leetcode 53 Maximum subarray
Favicon
Building a Production-Ready Trie Search System: A 5-Day Journey 🚀
Favicon
AI and Machine Learning Algorithms: Strengths, Weaknesses and Best Use Cases
Favicon
Best Way to Learn Data Science: A Comprehensive Guide for Aspiring Experts
Favicon
Symmetric data encryption with Python
Favicon
Disk Fragmenter
Favicon
Time and Space Complexity
Favicon
Простая задача с собеседования в Google: Merge Strings Alternately

Featured ones: