Javatpoint Logo
Javatpoint Logo

Stock Span Problem

"The Stock Span Problem" comes under the financial aspect. The stock span for each day's stock price is determined in this problem. Its span is the greatest number of consecutive days shortly before any given day when the stock price is less than or equal to the stock price on those days.

The greatest number of consecutive days just before the given day for which the stock's price on the present day is less than or equal to its price on the given day is described as the span Si of the stock's price on that day.

For example, if an array of seven-day prices is supplied as 100, 80, 60, 70, 60, 75, 85, the span values for the seven days are 1, 1, 1, 2, 1, 4, 6.

There are two methods to find the solution to this Stock Span problem. These two methods are:

  • Inefficient method
  • Efficient method

In this stock span problem, traverse the price array in the input. While items on the left side are smaller, traverse elements on the left side and increase the span value of each visited element.

Now let's understand the Stock span problem with the help of codes written in three different programming languages C++, Java, and Python.

C++ Code

Let's understand this problem with the help of the following C++ code.

Output

The above code gives the following output.

Enter the size of the span array::
7
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
16
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
67
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
44
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
93 
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
33
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
2
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
86
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
2
Span Values Calculated Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
3
New calculated Span values:
1 2 1 4 1 1 3 
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
n

In the above c++ code, we have created a class that has the functions to find the span values for the inserted prices of the stocks. The flow of the code is like this: the user is given three choices: add the prices of the stocks to calculate the span value for those inserted stock prices, and then print the calculated span values of the stock prices. The user enters the prices of the stocks; once the prices are added successfully, the calculateSpan() function is called, which calculates the span values for those inserted price values. Finally, with the help of the last option, those span values are printed.

Java Code

Let's understand this problem also with the help of the Java code.

Output

The above Java code gives the following output.

Enter the size of the span array:: 
9
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the price value that you want to add to the array :
14
Data Added Successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the price value that you want to add to the array :
287
Data Added Successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1 
Enter the price value that you want to add to the array :
32
Data Added Successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the price value that you want to add to the array :
40
Data Added Successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the price value that you want to add to the array :
533
Data Added Successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the price value that you want to add to the array :
6
Data Added Successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the price value that you want to add to the array :
700
Data Added Successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the price value that you want to add to the array :
855
Data Added Successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the price value that you want to add to the array :
9002
Data Added Successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
2
Span Values Calculated Successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
3
New calculated Span values.
Span of 9 days : 1 2 1 2 5 1 7 8 9 
Do you want to continue (Type y or n)
n

In the above java code, we have created a class that has the functions to find the span values for the inserted prices of the stocks. The flow of the code is like this; the user is given three choices: add the prices of the stocks, calculate the span value for those inserted stock prices, and then print the calculated span values of the stock prices. Now, the user enters the prices of the stocks. Once the prices are added successfully, the calculateSpan() function is called, which calculates the span values for those inserted price values. Finally, with the help of the last option, those span values are printed.

Python

Now we find the solution to this problem through this Python code.

Output

The above python code produces the following output.

Enter the size of the span array::
7
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
16
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
67
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
44
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
93 
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
33
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
2
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
86
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
2
Span Values Calculated Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
3
New calculated Span values:
1 2 1 4 1 1 3 
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
n

In the above python code, we have created a class that has the functions to find the span values for the inserted prices of the stocks. The flow of the code is like this; the user is given three choices: add the prices of the stocks, calculate the span value for those inserted stock prices, and then print the calculated span values of the stock prices. Now, the user enters the prices of the stocks. Once the prices are added successfully, the calculateSpan() function is called, which calculates the span values for those inserted price values. Finally, with the help of the last option, those span values are printed.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA