Javatpoint Logo
Javatpoint Logo

Stock span problem in C++

In the ever-evolving sphere of finance and investment, algorithmic problem-solving assumes a critical role. Among the challenges encountered by traders and investors, the Stock Span Problem stands out, demanding the computation of stock spans based on a given set of stock prices. This blog post aims to delve into the intricacies of the Stock Span Problem, highlighting its practical relevance, and offers a detailed C++ implementation complete with examples and outputs.

What is the Stock Span Problem?

The Stock Span Problem represents a classic algorithmic puzzle. Given an array of daily stock prices, the task is to determine, for each day 'i', the maximum number of consecutive days (inclusive of the current day) for which the stock price is less than or equal to the price on the day 'i'. Far from being a mere theoretical exercise, this problem serves as a valuable instrument for investors, providing insights into the trend and stability of a stock over a specific period.


Let's explore an efficient C++ implementation of the Stock Span Problem, characterized by its conciseness and elegance. Leveraging a stack to manage indices, this approach ensures the calculation of stock spans is not only accurate but also performance efficient.


Stock Prices: 100 80 60 70 60 75 85 
Stock Span: 1 1 1 2 1 4 6


  1. The calculateStockSpan function efficiently computes the stock span using a stack to manage indices, and the main function demonstrates its usage with a practical example.
  2. Classic algorithmic puzzle, determining consecutive days where stock prices surpass or equal the current day's price.
  3. The Stock Span Problem, though abstract in nature, carries substantial significance in real-world financial decision-making. Investors and traders perpetually seek tools and insights to make well-informed decisions regarding stock transactions. The stock span, acting as a pivotal metric, provides valuable insights into the momentum and stability of a specific stock.
  4. Picture yourself as a trader evaluating the historical performance of a stock. The stock span not only reveals the performance on a given day but also provides a broader perspective by capturing trends and patterns over consecutive days. A rising stock span indicates a sustained positive trend, potentially signaling a lucrative investment opportunity. Conversely, a fluctuating or decreasing span might denote volatility or a waning market interest.


In conclusion, the Stock Span Problem emerges as an indispensable tool in the dynamic realm of finance. Addressed through a concise and efficient C++ implementation, this algorithmic challenge empowers investors and traders to decipher trends and assess the stability of stocks over time. The example with stock prices {100, 80, 60, 70, 60, 75, 85} and corresponding spans {1, 1, 1, 2, 1, 4, 6} vividly demonstrates the practical application of this algorithm.

Beyond its algorithmic intricacies, the Stock Span Problem serves as a pragmatic guide for decision-making in financial markets. Investors can leverage the calculated stock spans to discern patterns, identify trends, and make informed choices in the ever-evolving realm of investments. As algorithmic solutions continue to shape financial strategies, the Stock Span Problem stands out as a valuable asset for those navigating the complexities of stock trading and investment.

Next Topicstd::tie in C++

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA