0PricingLogin
Competitive Programming Academy · Lesson

Monotonic Stack: Next Greater Element

Answer span queries in one pass.

The Next Greater Problem

For each number, you want the first bigger value to its right. Brute force is O(n squared), but a monotonic stack does it in one pass.

What Monotonic Means

A monotonic stack keeps its values in sorted order, here decreasing, so the moment that order would break we know an answer is found.

All lessons in this course

  1. Stacks for Matching Brackets
  2. Monotonic Stack: Next Greater Element
  3. Queues and collections.deque
  4. Sliding Window Maximum with Deque
← Back to Competitive Programming Academy