Coders Packet

Stock Span Problem using Java

By Soham Ghosh

Given an array of n daily prices for a stock, this project calculates the span of stock's price for all n days using Java.

The project uses Stack class which models and implements the Stack data structure through its predefined methods such as push(), pop(), peek() and Scanner class for user input. The stack stores the days from 0 to n-1 in an order such that when the stock price of day to be pushed is greater than equal to the stock price of the top, we pop elements from the stack until this condition is negated.

The span of the stock's price on a given day is defined as the maximum number of consecutive days including the given day, for which the stock price on current day is less than or equal to the price on the given day. The span value for each price is given as: span[i] = i - stack.top(). 

Example:

Input:

Enter the number of days

7

Enter the stock prices

100

80

60

70

60

75

85

Output:

The stock span values are:

1

1

1

2

1

4

6

Download project

Reviews Report

Submitted by Soham Ghosh (sohamghosh1509)

Download packets of source code on Coders Packet