Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

The Product Polarity

Learn
Play
Prove
Problem context and objectives
Mission briefing

Energy-route planner · A runner crosses a line of power stations

Build each decision from smaller outcomes already solved: The Product Polarity.

Re-solving the same future branches drains exponential energy.

How you win

  1. 1Recognize when Maximum product subarray matches the clues
  2. 2Keep this true after every move: maxEnd and minEnd are the extreme products of subarrays ending at the current index
  3. 3Reach the result within O(n) time and O(1) space

Rules and pressure

  • Target cost: O(n) time and O(1) space
  • State rule: maxEnd and minEnd are the extreme products of subarrays ending at the current index
Lesson 1 of 3

Live algorithm trace

Maximum product subarray

Complete execution
1 of 5

Initialize both ending extremes to the first value.

1maxEnd = minEnd = first value
2for each next value
3compute value, value*oldMax, value*oldMin
4maxEnd = maximum candidate
5minEnd = minimum candidate
6update and return global maximum
maxEnd = 2minEnd = 2answer = 2
Truth to preserve / Cost target
Truth to preserve

maxEnd and minEnd are the extreme products of subarrays ending at the current index

Cost target

O(n) time and O(1) space

A negative value swaps the roles of the largest and smallest ending products. Track both extremes, plus the option to restart at the current value.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead