Coding Challenge Day- 21: Problem 1: The Stock Span Problem

Hello! So I’m doing a 30 day coding challenge where I solve a few questions every day and thought of posting them here on my blog so that you guys can join the challenge too!

Welcome to Coding challenge Day 21: Problem 1! Be sure to post your answers, queries etc in the comments!

Problem: The Stock Span Problem: This is basically a financial problem. We are given the stock price for 7 consecutive days (refer to the above histogram). We need to find the span of the stock on each day. (Span basically means the max number of consecutive days before i th day having stock_price< stock_price[i] of i th day.

Eg. stock_price of day 3 is greater than stock price of day 2. So stock_span of day 3= 2.

You can refer to the video below to better understand the logic:

Sample: The stock span of [100, 80, 60, 70, 60, 75, 85] is [1, 1, 1, 2, 1, 4, 6]


package stack;

import java.util.Arrays;

public class Program006stock_span_problem {
	public static int[] span_finder (int price[], int n){
		int [] span= new int [n];
		for(int i=1; i<n; i++){
			for(int j=i-1; (j>=0) && (price[i]>= price[j]); j--){
		return span;
	public static void main (String args []){
		int price []= {100, 80, 60, 70, 60, 75, 85};
		System.out.println("The stock span of " + Arrays.toString(price) + " is " + Arrays.toString(span_finder(price, price.length)));


Download Code

Happy Learning!!

Leave a Reply


Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: