{
  "id": "stacks-queues/monotonic-stack",
  "version": 1,
  "deterministic": true,
  "url": "/dsa/stacks-queues/monotonic-stack",
  "topic": {
    "slug": "stacks-queues",
    "title": "Stacks & Queues"
  },
  "title": "Monotonic Stack",
  "tagline": "A stack kept in order — so one new value can answer many old questions at once.",
  "vizKind": "array",
  "complexity": {
    "time": {
      "best": "O(n)",
      "average": "O(n)",
      "worst": "O(n)"
    },
    "space": "O(n)",
    "growth": "linear",
    "note": "Each index is pushed once and popped at most once — two operations per element no matter how the inner while-loop looks. That's O(n), down from the O(n²) of checking every pair. The stack itself can hold up to n items."
  },
  "code": {
    "javascript": "// For each value, find the next value to its right that is bigger.\nfunction nextGreater(a) {\n  const result = new Array(a.length).fill(-1);\n  const stack = [];              // holds INDEXES, kept decreasing\n\n  for (let i = 0; i < a.length; i++) {\n    // Anything smaller than a[i] has just found its answer.\n    while (stack.length && a[stack[stack.length - 1]] < a[i]) {\n      const j = stack.pop();\n      result[j] = a[i];\n    }\n    stack.push(i);\n  }\n\n  return result;   // whatever is left never found a bigger value\n}",
    "python": "# For each value, find the next value to its right that is bigger.\ndef next_greater(a):\n    result = [-1] * len(a)\n    stack = []                   # holds INDEXES, kept decreasing\n\n    for i, v in enumerate(a):\n        # Anything smaller than v has just found its answer.\n        while stack and a[stack[-1]] < v:\n            j = stack.pop()\n            result[j] = v\n\n        stack.append(i)\n\n    return result   # whatever is left never found a bigger value\n",
    "java": "// For each value, find the next value to its right that is bigger.\nint[] nextGreater(int[] a) {\n  int[] result = new int[a.length];\n  Arrays.fill(result, -1);\n  Deque<Integer> stack = new ArrayDeque<>();   // INDEXES, decreasing\n\n  for (int i = 0; i < a.length; i++) {\n    // Anything smaller than a[i] has just found its answer.\n    while (!stack.isEmpty() && a[stack.peek()] < a[i]) {\n      result[stack.pop()] = a[i];\n    }\n    stack.push(i);\n  }\n\n  return result;   // whatever is left never found a bigger value\n}",
    "cpp": "// For each value, find the next value to its right that is bigger.\nvector<int> nextGreater(vector<int>& a) {\n  vector<int> result(a.size(), -1);\n  vector<int> stack;             // holds INDEXES, kept decreasing\n\n  for (int i = 0; i < (int)a.size(); i++) {\n    // Anything smaller than a[i] has just found its answer.\n    while (!stack.empty() && a[stack.back()] < a[i]) {\n      result[stack.back()] = a[i];\n      stack.pop_back();\n    }\n    stack.push_back(i);\n  }\n\n  return result;   // whatever is left never found a bigger value\n}"
  },
  "defaultInput": [
    4,
    9,
    3,
    6,
    2,
    8,
    1,
    7
  ],
  "defaultTarget": null,
  "inputHint": "Pink = still on the stack, waiting for an answer. Green = answered.",
  "pitfalls": [
    {
      "wrong": "Seeing the nested while-loop and assuming it's O(n²).",
      "right": "It isn't. The inner loop can only pop what was pushed, and each index is pushed once. Total work across the whole run is bounded by 2n — this is amortised analysis in action."
    },
    {
      "wrong": "Storing values on the stack instead of indexes.",
      "right": "Store indexes. You almost always need to know *where* the waiting element was — to write into a results array, or to compute a width. Values alone throw that away."
    },
    {
      "wrong": "Breaking the monotonic invariant.",
      "right": "If the stack isn't kept in order, you can't pop 'everything smaller' in one sweep — you'd have to search it, and the O(n) collapses. The ordering is the whole algorithm."
    },
    {
      "wrong": "Forgetting the leftovers.",
      "right": "Whatever is still on the stack at the end never found anything bigger. Those get −1 (or whatever your 'none' value is) — don't just drop them."
    }
  ],
  "quiz": [
    {
      "q": "What does the stack actually hold?",
      "options": [
        "The answers found so far",
        "Indexes whose answer is still unknown, kept in decreasing order",
        "The whole array, sorted",
        "The largest value seen"
      ],
      "answer": 1,
      "why": "It's a shortlist of positions still waiting for a bigger value, held in decreasing order. That ordering lets one new arrival settle several of them at once."
    },
    {
      "q": "There's a while-loop inside a for-loop. Why isn't it O(n²)?",
      "options": [
        "It is O(n²)",
        "Each index is pushed once and popped at most once, so the total work is bounded by 2n",
        "Because the stack is small",
        "Because the array is sorted"
      ],
      "answer": 1,
      "why": "The inner loop can only pop things that were pushed, and nothing is pushed twice. Across the entire run there are at most n pushes and n pops — O(n) total, however the loops look."
    },
    {
      "q": "A new, larger value arrives. What happens to the stack?",
      "options": [
        "It's cleared entirely",
        "Everything smaller than it is popped — they've all just found their answer",
        "The new value is discarded",
        "The stack is re-sorted"
      ],
      "answer": 1,
      "why": "Every waiting position smaller than the newcomer has found its next-greater element, all in that one instant. They're popped together — which is exactly why the pass stays linear."
    }
  ],
  "problems": [
    {
      "name": "Next Greater Element I",
      "difficulty": "Easy",
      "url": "https://leetcode.com/problems/next-greater-element-i/"
    },
    {
      "name": "Daily Temperatures",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/daily-temperatures/"
    },
    {
      "name": "Largest Rectangle in Histogram",
      "difficulty": "Hard",
      "url": "https://leetcode.com/problems/largest-rectangle-in-histogram/"
    },
    {
      "name": "Trapping Rain Water",
      "difficulty": "Hard",
      "url": "https://leetcode.com/problems/trapping-rain-water/"
    }
  ],
  "trace": {
    "note": "Each frame is one immutable step: data snapshot, pointers, per-index roles, executing code line, and a one-sentence explanation.",
    "length": 23,
    "frames": [
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [],
        "codeLine": 2,
        "variables": {},
        "explanation": "For every value, find the next value to its right that is bigger. The obvious way compares every pair — O(n²). A monotonic stack does it in one pass.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "active"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "i": 0,
          "value": 4,
          "stack": "empty"
        },
        "explanation": "Arriving at 4. The pink ones are still waiting for an answer — that's what the stack holds.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "pivot"
          }
        ],
        "codeLine": 12,
        "variables": {
          "pushed": 4,
          "stack": "4"
        },
        "explanation": "4 doesn't have its own answer yet, so it joins the stack. Notice the stack always reads largest-to-smallest — that's the \"monotonic\" part, and it's why one new value can answer several at once.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "pivot"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "pivot"
          },
          {
            "index": 1,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "i": 1,
          "value": 9,
          "stack": "4"
        },
        "explanation": "Arriving at 9. The pink ones are still waiting for an answer — that's what the stack holds.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 0,
            "role": "found"
          },
          {
            "index": 1,
            "role": "compare"
          }
        ],
        "codeLine": 9,
        "variables": {
          "answered": 4,
          "with": 9,
          "stack": "empty"
        },
        "explanation": "4 was waiting, and 9 is bigger — so 9 is its next greater element. Answered, and popped off the stack.",
        "stats": {
          "comparisons": 1,
          "swaps": 1
        },
        "accent": "found"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "pivot"
          }
        ],
        "codeLine": 12,
        "variables": {
          "pushed": 9,
          "stack": "9"
        },
        "explanation": "9 doesn't have its own answer yet, so it joins the stack. Notice the stack always reads largest-to-smallest — that's the \"monotonic\" part, and it's why one new value can answer several at once.",
        "stats": {
          "comparisons": 1,
          "swaps": 1
        },
        "accent": "pivot"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "pivot"
          },
          {
            "index": 2,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "i": 2,
          "value": 3,
          "stack": "9"
        },
        "explanation": "Arriving at 3. The pink ones are still waiting for an answer — that's what the stack holds.",
        "stats": {
          "comparisons": 1,
          "swaps": 1
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "pivot"
          },
          {
            "index": 2,
            "role": "pivot"
          }
        ],
        "codeLine": 12,
        "variables": {
          "pushed": 3,
          "stack": "9 3"
        },
        "explanation": "3 doesn't have its own answer yet, so it joins the stack. Notice the stack always reads largest-to-smallest — that's the \"monotonic\" part, and it's why one new value can answer several at once.",
        "stats": {
          "comparisons": 1,
          "swaps": 1
        },
        "accent": "pivot"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "pivot"
          },
          {
            "index": 2,
            "role": "pivot"
          },
          {
            "index": 3,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "i": 3,
          "value": 6,
          "stack": "9 3"
        },
        "explanation": "Arriving at 6. The pink ones are still waiting for an answer — that's what the stack holds.",
        "stats": {
          "comparisons": 1,
          "swaps": 1
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "pivot"
          },
          {
            "index": 2,
            "role": "found"
          },
          {
            "index": 3,
            "role": "compare"
          }
        ],
        "codeLine": 9,
        "variables": {
          "answered": 3,
          "with": 6,
          "stack": "9"
        },
        "explanation": "3 was waiting, and 6 is bigger — so 6 is its next greater element. Answered, and popped off the stack.",
        "stats": {
          "comparisons": 2,
          "swaps": 2
        },
        "accent": "found"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "pivot"
          },
          {
            "index": 3,
            "role": "pivot"
          }
        ],
        "codeLine": 12,
        "variables": {
          "pushed": 6,
          "stack": "9 6"
        },
        "explanation": "6 doesn't have its own answer yet, so it joins the stack. Notice the stack always reads largest-to-smallest — that's the \"monotonic\" part, and it's why one new value can answer several at once.",
        "stats": {
          "comparisons": 2,
          "swaps": 2
        },
        "accent": "pivot"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "pivot"
          },
          {
            "index": 3,
            "role": "pivot"
          },
          {
            "index": 4,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "i": 4,
          "value": 2,
          "stack": "9 6"
        },
        "explanation": "Arriving at 2. The pink ones are still waiting for an answer — that's what the stack holds.",
        "stats": {
          "comparisons": 2,
          "swaps": 2
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "pivot"
          },
          {
            "index": 3,
            "role": "pivot"
          },
          {
            "index": 4,
            "role": "pivot"
          }
        ],
        "codeLine": 12,
        "variables": {
          "pushed": 2,
          "stack": "9 6 2"
        },
        "explanation": "2 doesn't have its own answer yet, so it joins the stack. Notice the stack always reads largest-to-smallest — that's the \"monotonic\" part, and it's why one new value can answer several at once.",
        "stats": {
          "comparisons": 2,
          "swaps": 2
        },
        "accent": "pivot"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "pivot"
          },
          {
            "index": 3,
            "role": "pivot"
          },
          {
            "index": 4,
            "role": "pivot"
          },
          {
            "index": 5,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "i": 5,
          "value": 8,
          "stack": "9 6 2"
        },
        "explanation": "Arriving at 8. The pink ones are still waiting for an answer — that's what the stack holds.",
        "stats": {
          "comparisons": 2,
          "swaps": 2
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "pivot"
          },
          {
            "index": 3,
            "role": "pivot"
          },
          {
            "index": 4,
            "role": "found"
          },
          {
            "index": 5,
            "role": "compare"
          }
        ],
        "codeLine": 9,
        "variables": {
          "answered": 2,
          "with": 8,
          "stack": "9 6"
        },
        "explanation": "2 was waiting, and 8 is bigger — so 8 is its next greater element. Answered, and popped off the stack.",
        "stats": {
          "comparisons": 3,
          "swaps": 3
        },
        "accent": "found"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "pivot"
          },
          {
            "index": 3,
            "role": "found"
          },
          {
            "index": 5,
            "role": "compare"
          }
        ],
        "codeLine": 9,
        "variables": {
          "answered": 6,
          "with": 8,
          "stack": "9"
        },
        "explanation": "6 was waiting, and 8 is bigger — so 8 is its next greater element. Answered, and popped off the stack.",
        "stats": {
          "comparisons": 4,
          "swaps": 4
        },
        "accent": "found"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "pivot"
          },
          {
            "index": 5,
            "role": "pivot"
          }
        ],
        "codeLine": 12,
        "variables": {
          "pushed": 8,
          "stack": "9 8"
        },
        "explanation": "8 doesn't have its own answer yet, so it joins the stack. Notice the stack always reads largest-to-smallest — that's the \"monotonic\" part, and it's why one new value can answer several at once.",
        "stats": {
          "comparisons": 4,
          "swaps": 4
        },
        "accent": "pivot"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "pivot"
          },
          {
            "index": 5,
            "role": "pivot"
          },
          {
            "index": 6,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "i": 6,
          "value": 1,
          "stack": "9 8"
        },
        "explanation": "Arriving at 1. The pink ones are still waiting for an answer — that's what the stack holds.",
        "stats": {
          "comparisons": 4,
          "swaps": 4
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "pivot"
          },
          {
            "index": 5,
            "role": "pivot"
          },
          {
            "index": 6,
            "role": "pivot"
          }
        ],
        "codeLine": 12,
        "variables": {
          "pushed": 1,
          "stack": "9 8 1"
        },
        "explanation": "1 doesn't have its own answer yet, so it joins the stack. Notice the stack always reads largest-to-smallest — that's the \"monotonic\" part, and it's why one new value can answer several at once.",
        "stats": {
          "comparisons": 4,
          "swaps": 4
        },
        "accent": "pivot"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 7
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "pivot"
          },
          {
            "index": 5,
            "role": "pivot"
          },
          {
            "index": 6,
            "role": "pivot"
          },
          {
            "index": 7,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "i": 7,
          "value": 7,
          "stack": "9 8 1"
        },
        "explanation": "Arriving at 7. The pink ones are still waiting for an answer — that's what the stack holds.",
        "stats": {
          "comparisons": 4,
          "swaps": 4
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 7
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "pivot"
          },
          {
            "index": 5,
            "role": "pivot"
          },
          {
            "index": 6,
            "role": "found"
          },
          {
            "index": 7,
            "role": "compare"
          }
        ],
        "codeLine": 9,
        "variables": {
          "answered": 1,
          "with": 7,
          "stack": "9 8"
        },
        "explanation": "1 was waiting, and 7 is bigger — so 7 is its next greater element. Answered, and popped off the stack.",
        "stats": {
          "comparisons": 5,
          "swaps": 5
        },
        "accent": "found"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 7
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "pivot"
          },
          {
            "index": 5,
            "role": "pivot"
          },
          {
            "index": 7,
            "role": "pivot"
          }
        ],
        "codeLine": 12,
        "variables": {
          "pushed": 7,
          "stack": "9 8 7"
        },
        "explanation": "7 doesn't have its own answer yet, so it joins the stack. Notice the stack always reads largest-to-smallest — that's the \"monotonic\" part, and it's why one new value can answer several at once.",
        "stats": {
          "comparisons": 5,
          "swaps": 5
        },
        "accent": "pivot"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-9",
            "value": 9
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-6",
            "value": 6
          },
          {
            "id": "c4-2",
            "value": 2
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-1",
            "value": 1
          },
          {
            "id": "c7-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "pivot"
          },
          {
            "index": 5,
            "role": "pivot"
          },
          {
            "index": 7,
            "role": "pivot"
          }
        ],
        "codeLine": 16,
        "variables": {
          "answers": "9 — 6 8 8 — 7 —",
          "unanswered": 3,
          "pushesAndPops": 13
        },
        "explanation": "Answers: 9 — 6 8 8 — 7 —. The 3 still on the stack never met anything bigger, so they get −1. Every index was pushed once and popped at most once — that's why this is O(n), not O(n²).",
        "stats": {
          "comparisons": 5,
          "swaps": 5
        },
        "accent": "sorted"
      }
    ]
  }
}