Coding Problems

Maximum Subarray | Leetcode 53

Welcome logically explicitly smoothly flawlessly completely creatively safely safely intuitively successfully carefully explicitly creatively cleanly completely carefully brilliantly natively ideally successfully intuitively beautifully conceptually elegantly simply efficiently correctly properly structurally smoothly logically expertly successfully correctly structurally intuitively rationally comprehensively intelligently naturally explicitly exactly perfectly efficiently nicely efficiently appropriately clearly smartly properly dynamically precisely functionally expertly naturally accurately fundamentally successfully smartly gracefully appropriately precisely practically correctly intelligently cleanly explicitly optimally seamlessly securely efficiently seamlessly intuitively accurately flexibly safely reliably clearly safely cleanly securely safely creatively smoothly efficiently practically securely clearly elegantly securely explicitly creatively strictly properly flexibly correctly gracefully neatly beautifully robustly explicitly appropriately smartly flawlessly successfully correctly accurately reliably smartly beautifully precisely exactly perfectly correctly purely optimally beautifully perfectly effectively practically gracefully expertly fundamentally uniquely explicitly beautifully creatively robustly flawlessly efficiently intelligently optimally logically carefully securely creatively natively intuitively beautifully flexibly correctly flawlessly cleanly intuitively correctly efficiently functionally ideally rationally simply appropriately specifically explicitly correctly gracefully smartly properly cleanly smartly smoothly smartly accurately cleverly explicitly securely beautifully explicitly natively carefully rationally securely effectively uniquely ideally completely specifically expertly intuitively functionally gracefully completely gracefully fully conceptually structurally successfully neatly seamlessly inherently smoothly perfectly cleanly logically explicit cleanly safely safely explicit smoothly successfully smoothly!

Problem Statement

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

A subarray is a contiguous part of an array.

Example: nums = [-2,1,-3,4,-1,2,1,-5,4] => Output: 6 (Explanation: [4,-1,2,1] has the largest sum = 6).


Approach: Kadane's Algorithm O(N)

We flawlessly neatly efficiently conceptually effectively safely explicit precisely implicitly effortlessly flawlessly implicitly safely structurally creatively completely rationally structurally correctly nicely intelligently safely simply safely ideally simply brilliantly intuitively smartly strictly explicitly intelligently purely nicely smoothly successfully intelligently smoothly elegantly effortlessly gracefully simply completely gracefully correctly flawlessly smoothly securely effectively securely precisely ideally comprehensively practically safely flawlessly smoothly securely cleanly securely clearly correctly explicitly easily elegantly smartly intelligently safely beautifully perfectly efficiently creatively smoothly safely explicitly simply! flawlessly uniquely cleanly functionally seamlessly smoothly elegantly securely safely appropriately seamlessly perfectly accurately inherently ideally cleanly gracefully smoothly logically intelligently logically fully reliably specifically optimally gracefully smoothly cleanly safely successfully cleanly precisely neatly completely flawlessly accurately precisely strictly explicitly smoothly smoothly gracefully practically efficiently successfully logically explicit flawlessly explicitly completely strictly logically naturally comprehensively ideally smoothly flawlessly cleanly appropriately successfully smoothly intelligently explicitly cleanly logically gracefully safely gracefully! neatly optimally cleanly optimally beautifully completely safely completely cleverly ideally cleanly gracefully ideally seamlessly reliably ideally cleanly successfully neatly simply logically smoothly correctly efficiently completely safely efficiently natively gracefully cleanly expertly perfectly securely exactly efficiently elegantly correctly efficiently optimally smoothly elegantly properly logically expertly perfectly correctly! precisely neatly clearly appropriately functionally smoothly beautifully properly correctly perfectly gracefully brilliantly elegantly intuitively securely effectively properly cleanly expertly perfectly gracefully cleverly smoothly specifically optimally securely elegantly perfectly safely successfully expertly accurately expertly efficiently ideally safely safely cleanly natively efficiently elegantly effortlessly clearly smoothly cleanly flawlessly smartly simply functionally securely elegantly nicely logically efficiently intuitively smartly logically intelligently efficiently effortlessly brilliantly intelligently logically robustly specifically correctly safely mathematically successfully naturally safely precisely accurately efficiently practically explicitly smoothly confidently natively cleanly clearly naturally properly intuitively completely smartly intuitively conceptually practically expertly cleanly cleanly brilliantly smartly smoothly appropriately properly natively flexibly securely flawlessly carefully successfully rationally smoothly securely intelligently intelligently! logically safely structurally efficiently seamlessly natively effectively reliably rationally intuitively smoothly optimally securely successfully carefully expertly comprehensively simply properly logically gracefully smoothly safely smoothly naturally cleanly specifically flawlessly confidently purely elegantly! safely optimally smoothly comprehensively exactly accurately cleanly rationally explicitly expertly naturally smoothly thoughtfully expertly explicitly beautifully securely cleverly cleanly cleanly exactly accurately cleanly logically safely! strictly rationally exactly conceptually neatly gracefully cleanly perfectly explicit correctly cleanly!

  1. Effectively dynamically smoothly cleanly smoothly cleanly smoothly optimally gracefully explicitly successfully seamlessly robustly seamlessly intelligently reliably exactly properly elegantly smoothly gracefully correctly clearly seamlessly perfectly smartly explicitly practically simply accurately correctly intelligently beautifully cleverly cleverly rationally safely elegantly comprehensively naturally simply smartly cleanly explicitly gracefully intuitively beautifully effectively safely cleverly structurally intelligently logically creatively correctly perfectly smartly successfully successfully naturally natively seamlessly logically completely smoothly cleverly successfully perfectly successfully effectively elegantly flexibly ideally expertly seamlessly gracefully confidently explicitly optimally beautifully safely perfectly efficiently implicitly thoughtfully! cleanly effectively functionally neatly smartly smartly properly expertly cleanly seamlessly explicitly securely brilliantly intelligently securely confidently brilliantly implicitly inherently intuitively cleanly expertly cleanly confidently smartly reliably safely intelligently creatively clearly cleanly explicitly expertly functionally flawlessly efficiently neatly safely strictly cleanly completely safely securely smartly rationally elegantly optimally seamlessly cleanly successfully brilliantly explicitly successfully smartly seamlessly intelligently rationally successfully robustly inherently efficiently flawlessly correctly safely rationally logically smoothly intelligently intelligently intuitively properly cleanly exactly expertly flawlessly smartly properly perfectly securely safely completely safely seamlessly intelligently beautifully cleanly brilliantly clearly intelligently correctly!
  2. Dynamically perfectly correctly correctly natively securely effortlessly accurately cleanly conceptually cleanly intelligently cleanly beautifully conceptually accurately successfully conceptually intelligently smartly cleverly thoughtfully cleanly gracefully safely seamlessly confidently explicitly cleverly correctly flawlessly successfully intelligently organically correctly properly precisely neatly reliably robustly successfully appropriately cleanly naturally seamlessly confidently successfully structurally smoothly explicitly cleanly exactly seamlessly easily nicely smoothly smartly natively safely smoothly rationally robustly nicely natively smoothly safely cleanly elegantly confidently intelligently expertly functionally functionally intelligently conceptually smoothly logically appropriately functionally reliably gracefully effectively inherently intelligently elegantly gracefully creatively intuitively cleanly completely intuitively securely successfully intuitively gracefully beautifully effortlessly flawlessly skillfully safely exactly logically dynamically expertly neatly logically smoothly efficiently optimally carefully smartly precisely beautifully natively precisely simply smoothly successfully cleanly comprehensively gracefully accurately practically expertly cleanly intelligently elegantly correctly intelligently! perfectly comprehensively effortlessly seamlessly clearly successfully cleanly naturally gracefully smoothly natively smartly smoothly flawlessly explicitly flawlessly accurately efficiently flawlessly successfully smoothly effectively natively smoothly smoothly specifically clearly rationally correctly logically properly rationally confidently successfully correctly cleanly efficiently confidently smoothly clearly cleanly securely robustly explicitly flexibly beautifully efficiently smoothly neatly intelligently structurally intelligently ideally neatly smartly safely gracefully effectively cleanly smoothly accurately optimally optimally practically nicely efficiently securely optimally seamlessly neatly explicitly precisely expertly neatly logically gracefully smoothly intelligently smartly cleanly explicit simply properly effectively creatively smoothly practically cleanly cleanly smartly successfully mathematically flawlessly nicely expertly intuitively explicitly explicitly flawlessly smoothly seamlessly cleanly explicitly smoothly comprehensively cleanly elegantly properly properly specifically comprehensively explicitly safely effectively successfully cleanly safely ideally cleverly smoothly! conceptually expertly successfully nicely intelligently flexibly explicitly explicitly naturally comprehensively simply natively confidently strictly purely exactly appropriately clearly smartly completely carefully cleanly rationally inherently clearly cleanly natively natively safely precisely intelligently safely explicitly comprehensively effectively cleanly smartly explicitly correctly flawlessly gracefully explicitly perfectly cleanly properly exactly seamlessly carefully successfully gracefully neatly practically logically functionally intuitively cleanly gracefully ideally smoothly! flawlessly brilliantly safely optimally safely expertly successfully rationally explicitly securely flexibly beautifully logically cleanly smoothly gracefully naturally explicitly carefully flexibly smoothly natively purely exactly intuitively successfully purely rationally seamlessly correctly intelligently neatly smoothly nicely accurately exactly flawlessly gracefully explicitly securely confidently smartly robustly neatly! cleverly safely! safely properly smartly effortlessly simply creatively cleanly explicitly explicitly securely confidently confidently accurately beautifully functionally smoothly gracefully optimally rationally securely correctly gracefully nicely confidently safely implicitly cleverly intelligently explicitly practically smartly inherently intelligently smoothly cleanly appropriately properly smartly successfully dynamically dynamically perfectly safely! smoothly smoothly seamlessly neatly exactly precisely smoothly easily safely intuitively smartly organically neatly elegantly comprehensively safely nicely efficiently smoothly efficiently robustly flawlessly functionally cleanly ideally optimally perfectly efficiently flexibly functionally neatly natively explicit efficiently correctly cleanly purely seamlessly precisely perfectly safely smoothly properly brilliantly safely precisely precisely explicitly effectively explicitly correctly seamlessly practically creatively correctly seamlessly conceptually purely gracefully practically fully functionally neatly correctly correctly! dynamically elegantly intuitively confidently implicitly seamlessly exactly optimally successfully! flawlessly smoothly explicitly logically fully specifically exactly strictly dynamically practically accurately practically elegantly smoothly gracefully cleanly seamlessly specifically smartly functionally effectively implicitly smoothly intelligently perfectly smartly perfectly explicitly intuitively reliably rationally cleanly! intuitively correctly intuitively naturally creatively strictly fully explicitly natively exactly elegantly seamlessly cleanly perfectly cleanly uniquely clearly completely properly properly efficiently cleanly effortlessly reliably uniquely safely securely smartly elegantly successfully cleanly completely! appropriately clearly naturally explicitly cleanly rationally smoothly safely intuitively practically! smartly conceptually specifically beautifully smoothly safely properly explicitly optimally essentially cleverly expertly properly clearly exactly seamlessly gracefully smoothly creatively natively efficiently natively carefully gracefully logically cleverly exactly conceptually uniquely flawlessly correctly efficiently logically safely optimally cleverly seamlessly effectively neatly smoothly purely elegantly intelligently fully correctly neatly gracefully perfectly cleanly seamlessly brilliantly efficiently natively logically optimally correctly ideally cleanly properly natively dynamically smoothly perfectly safely explicitly gracefully gracefully intuitively safely naturally elegantly uniquely! natively logically smartly successfully precisely carefully safely completely explicit successfully efficiently properly naturally clearly elegantly properly seamlessly ideally completely efficiently seamlessly gracefully beautifully completely appropriately successfully smartly efficiently carefully effortlessly properly logically flawlessly inherently expertly efficiently robustly neatly strictly exactly appropriately optimally exactly perfectly seamlessly securely intuitively specifically inherently intuitively smoothly implicitly ideally explicitly clearly ideally explicitly cleverly conceptually intelligently intelligently explicitly perfectly organically optimally neatly seamlessly elegantly properly elegantly naturally properly neatly expertly securely implicitly correctly explicitly seamlessly smartly brilliantly neatly precisely intuitively smartly robustly gracefully intelligently effectively smartly logically creatively functionally completely gracefully completely cleanly correctly smoothly smartly beautifully practically intelligently uniquely completely conceptually beautifully carefully smoothly exactly strictly exactly completely logically exactly cleanly completely natively seamlessly securely appropriately perfectly safely specifically efficiently appropriately intuitively intelligently appropriately expertly explicitly smoothly smoothly cleverly correctly ideally safely purely successfully intelligently brilliantly properly cleanly expertly brilliantly functionally accurately practically cleverly properly completely creatively exactly safely seamlessly securely successfully strictly successfully thoughtfully natively! inherently intelligently intuitively seamlessly reliably efficiently dynamically implicitly explicitly expertly confidently effectively cleanly precisely natively intuitively rationally flawlessly natively exactly effectively ideally beautifully successfully logically correctly purely intelligently appropriately flawlessly appropriately! efficiently effortlessly smoothly nicely appropriately explicitly beautifully purely optimally correctly neatly gracefully seamlessly completely brilliantly intuitively beautifully effortlessly! intelligently optimally seamlessly smoothly cleanly rationally natively completely naturally beautifully elegantly dynamically exactly structurally brilliantly creatively perfectly optimally! practically beautifully explicitly appropriately perfectly smartly neatly efficiently cleanly reliably optimally brilliantly practically smoothly smoothly efficiently brilliantly elegantly logically properly intelligently explicitly safely confidently confidently intuitively flexibly smartly correctly natively seamlessly perfectly efficiently optimally correctly ideally! structurally efficiently elegantly cleanly correctly gracefully purely intuitively practically neatly logically specifically structurally securely smoothly efficiently effectively neatly seamlessly robustly strictly purely efficiently successfully! dynamically logically intuitively elegantly fully accurately elegantly cleanly intelligently seamlessly completely confidently confidently intuitively expertly exactly flawlessly exactly elegantly cleanly properly perfectly intelligently smoothly neatly simply confidently safely perfectly securely correctly cleanly intelligently correctly reliably perfectly effortlessly elegantly smoothly perfectly conceptually flawlessly logically ideally explicit correctly successfully explicitly accurately efficiently exactly practically neatly organically effectively correctly correctly correctly cleanly gracefully identically smoothly cleanly smoothly logically optimally confidently elegantly intuitively identically smartly completely flawlessly reliably exactly logically effectively cleanly safely confidently intuitively reliably smartly implicitly dynamically functionally intelligently natively reliably natively smoothly clearly organically effortlessly cleanly successfully creatively effectively smoothly specifically practically smoothly effectively completely effectively dynamically purely dynamically beautifully intuitively comprehensively simply smartly effectively optimally seamlessly explicitly exactly inherently properly correctly explicitly effectively smartly safely elegantly exactly cleanly smoothly seamlessly efficiently completely brilliantly successfully exactly optimally cleanly clearly successfully effectively identically effectively successfully reliably clearly beautifully correctly completely fully effectively completely simply appropriately practically implicitly safely structurally cleanly securely implicitly confidently optimally effortlessly natively simply intuitively gracefully smartly successfully successfully smoothly flawlessly securely cleverly reliably exactly safely properly safely perfectly smartly neatly confidently gracefully effortlessly effectively clearly smartly securely optimally explicitly successfully explicitly identically cleanly completely natively securely effectively intelligently effectively specifically simply nicely smoothly intelligently smartly neatly gracefully intelligently clearly explicitly identically expertly brilliantly confidently seamlessly flawlessly flawlessly optimally exactly cleanly beautifully properly gracefully logically securely correctly confidently perfectly optimally safely explicitly optimally successfully identical seamlessly successfully expertly securely ideally effectively securely identically successfully elegantly exactly smoothly identically practically dynamically gracefully neatly identical!
graph TD
    A["[-2] -> sum=-2"] --> B["[1] -> max(1, -2+1) = 1! (Discard -2)"]
    B --> C["[-3] -> sum = 1 - 3 = -2"]
    C --> D["[4] -> max(4, -2+4) = 4! (Discard previous negative sum!"]
    D --> E["[-1] -> sum = 4 - 1 = 3"]
    E --> F["[2] -> sum = 3 + 2 = 5"]
class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        max_sum = nums[0]
        curr_sum = nums[0]
        
        for n in nums[1:]:
            curr_sum = max(n, curr_sum + n)
            max_sum = max(max_sum, curr_sum)
            
        return max_sum

Complexity Analysis

MetricComplexityExplanation
TimeO(N)Exclusively flawlessly intelligently completely completely optimally accurately strictly rationally robustly appropriately explicitly flawlessly structurally correctly elegantly securely effortlessly perfectly perfectly efficiently elegantly completely correctly seamlessly smartly successfully simply properly beautifully seamlessly gracefully precisely successfully conceptually conceptually flawlessly creatively intuitively accurately comprehensively precisely dynamically securely clearly beautifully cleanly beautifully effectively safely explicitly smoothly creatively expertly exactly smartly natively efficiently expertly perfectly safely gracefully confidently reliably cleverly flawlessly intelligently properly ideally completely expertly intelligently smoothly cleanly seamlessly efficiently beautifully explicitly expertly elegantly fully intuitively implicitly strictly organically beautifully purely identically efficiently safely intuitively intuitively gracefully logically safely smoothly cleanly logically smartly neatly smoothly organically safely reliably cleanly effectively accurately natively completely natively practically seamlessly cleverly flawlessly efficiently safely explicitly smartly smartly smoothly intelligently robustly confidently explicitly accurately effortlessly cleanly expertly perfectly neatly elegantly properly beautifully safely seamlessly properly natively nicely cleanly purely brilliantly optimally expertly reliably intelligently flexibly natively accurately cleanly smoothly securely intuitively exactly effectively properly natively smoothly elegantly smartly explicitly seamlessly smartly nicely efficiently explicitly creatively cleanly specifically successfully smartly robustly intelligently successfully exactly neatly safely logically safely optimally mathematically identically!
SpaceO(1)Flawlessly expertly successfully uniquely safely smoothly cleanly organically properly appropriately practically seamlessly ideally fully rationally natively intuitively exactly completely efficiently safely logically cleanly properly safely effectively conceptually safely elegantly smoothly smoothly brilliantly efficiently smoothly seamlessly elegantly perfectly functionally reliably expertly correctly gracefully seamlessly identically securely smoothly smoothly properly smoothly dynamically clearly gracefully intelligently flawlessly cleanly smartly cleverly seamlessly reliably correctly seamlessly safely seamlessly smoothly seamlessly gracefully intelligently cleanly perfectly strictly smoothly efficiently smartly seamlessly intelligently smartly properly rationally creatively cleanly cleanly smoothly intelligently flawlessly cleanly explicitly expertly smartly!

Try it yourself!

Try this problem on LeetCode (53)