Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Dynamic Programming/1D / Fibonacci-style

Technique

Kadane's Algorithm

best_ending_here = max(x, best_ending_here + x); the answer is the max over all positions.

Learn first1D / Fibonacci-style
0%
mastery
0/8 solved
Practise nextMaximum Product Subarray
IdeaPracticePitfalls

Recognise it

The signal

maximum (or minimum) sum contiguous subarray

  1. 1Brute force

    All O(n2) subarrays.

  2. 2The observation

    Recomputing sums: a negative running prefix is never worth keeping.

  3. 3Why it works

    cur equals the best sum of a subarray ending at the current index.

Reach for it when

  • +circular max subarray (total - min subarray)
  • +max product subarray (track min too)

Not when

  • ×length constraints (prefix-sum + mono-deque or ordered-set)
  • ×need count of subarrays rather than max

Practice

Next

Maximum Product Subarray

0/8 solved
  • Maximum Subarray SumCSESEasy
  • Just Eat It!Codeforces1300
  • Yarik and ArrayCodeforces1100
  • Best Time to Buy and Sell StockAmazon, MetaEasy
  • Maximum Product SubarrayAmazon, AdobeMedium
  • Maximum SubarrayAmazon, MicrosoftMedium
  • Maximum Sum on Even PositionsCodeforces1600
  • Yet Another Yet Another TaskCodeforces2000

Pitfalls

Misconceptions

  • !all-negative arrays (do not initialise answer to 0)
Go deeperresources

Resources

  • DP lecture notesErrichto · video
  • AtCoder Educational DP ContestAtCoder · sheet
  • Introduction to DPUSACO Guide · course
  • Dynamic Programming PatternsLeetCode discuss · article
  • Dynamic Programming playlistAditya Verma · video
  • Bitmask DPUSACO Guide · course
  • Digit DPCodeforces · blog
TodayReviewPatternsProblems