VOOZH about

URL: https://www.geeksforgeeks.org/dsa/the-stock-span-problem/

⇱ Stock Span Problem - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Stock Span Problem

Last Updated : 25 Sep, 2025

Given an array arr[] of daily stock prices, the stock span for the i-th day is the count of consecutive days up to and including day i, such that each of those days had a stock price less than or equal to the price on day i.

Examples:

Input: arr[] = [100, 80, 60, 120]
Output: [1, 1, 1, 4]
Explanation: For 100, there are no previous higher prices, so span = 1. For 80 and 60, each is smaller than the previous, so their spans remain 1. For 120, it is greater than all earlier prices (100, 80, 60), so the span extends back across all four days, giving span = 4.

Input: arr[] = [10, 4, 5, 90, 120, 80]
Output: [1, 1, 2, 4, 5, 1]
Explanation: For 10 and 4, no earlier prices are smaller, so span = 1 each. For 5, it is greater than 4, so span = 2. For 90, it is greater than 10, 5, and 4, so span = 4. For 120, it is greater than all previous prices, giving span = 5. Finally, 80 is smaller than 120, so span = 1.

[Naive Approach] Using Nested Loop - O(n2) Time and O(1) Space

The idea is to check, for each day, how many consecutive previous days had stock prices less than or equal to the current day’s price. This can be done by moving leftwards from the current index until a higher price is found or the beginning of the array is reached.


Output
1 1 2 4 5 1 

[Expected Approach] Using Stack - O(n) Time and O(n) Space

In the previous approach, for each day we kept moving leftward and comparing prices one by one. We continued as long as the current day’s price was greater than or equal to the previous day’s price. But the moment we find a price greater than the current day’s price, we stop.

That means we are always stopping at the first previous greater element.

The idea is to use a stack to directly find this first previous greater element for every day, instead of checking all consecutive smaller ones. Once we know that index, we can compute the span as:

Span[i] = currentIndex − indexOfPreviousGreaterElement

👁 Image

Output
1 1 2 4 5 1 
Comment
Article Tags: