Skip to main content

Stock span problem [EASY]

Company: Accolite

Question: 
The stock span problem is a financial problem where we have a series of n daily price quotes for a stock and we need to calculate span of stock’s price for all n days. 
The span Si of the stock’s price on a given day i is defined as the maximum number of consecutive days just before the given day, for which the price of the stock on the current day is less than or equal to its price on the given day.
For example, if an array of 7 days prices is given as {100, 80, 60, 70, 60, 75, 85}, then the span values for corresponding 7 days are {1, 1, 1, 2, 1, 4, 6}
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N,N is the size of array.
The second line of each test case contains N input C[i].

Output:
Print the span values.

Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 200
1 ≤ C[i] ≤ 800

Example:
Input
2
7
100 80 60 70 60 75 85
6
10 4 5 90 120 80
Output
1 1 1 2 1 4 6
1 1 2 4 5 1
CODE:
import java.util.*;
import java.lang.*;
import java.io.*;


import java.util.Stack;
import java.util.Arrays;

public class GFG
{
// a linear time solution for stock span problem
// A stack based efficient method to calculate stock span values
static void calculateSpan(int price[], int n, int S[])
{
// Create a stack and push index of first element to it
Stack<Integer> st= new Stack<>();
st.push(0); 
// Span value of first element is always 1
S[0] = 1;
// Calculate span values for rest of the elements
for (int i = 1; i < n; i++)
{
// Pop elements from stack while stack is not empty and                             //top of
// stack is smaller than price[i]
while (!st.empty() && price[st.peek()] <= price[i])
st.pop();
// If stack becomes empty, then price[i] is greater than all                           //elements
// on left of it, i.e., price[0], price[1],..price[i-1]. Else price[i]
// is greater than elements after top of stack
S[i] = (st.empty())? (i + 1) : (i - st.peek());
// Push this element to stack
st.push(i);
}
}
// A utility function to print elements of array
static void printArray(int arr[])
{
for(int i2=0;i2<arr.length;i2++){System.out.print(arr[i2]+" ");}
}
// Driver method
public static void main(String[] args) 
{   Scanner sc = new Scanner (System.in);
    int m=sc.nextInt();
    for(int mi=0;mi<m;mi++){
    int n = sc.nextInt();     
int price[] = new int[n];
for(int i1=0;i1<n;i1++){price[i1]=sc.nextInt();}
int S[]=new int[n];
// Fill the span values in array S[]
calculateSpan(price, n, S);

// print the calculated span values
printArray(S);
System.out.println();
    }
}
}

EXECUTION TIME: 0.23s

Comments

Popular Posts

TARUNKUMAR RAWAT:Fourier Series and Fourier Transform

Tarunkumar Rawat Fourier Series: Click here to download Tarun Rawat Fourier Series Tarunkumar Rawat Fourier Transform: Click here to download TarunRawat Fourier Transform Steps to follow: 1) Click on the above link 2) Wait for 3 secs and click on the link 3) Wait for 5 secs and click on skip ad NOTE: We do require these ads to fund the site and provide books for free. Besides these are harmless and just require 5 secs waiting and nothing else. Please be patient for 5 secs and get your desired book for FREE!  IMPORTANT: We believe that every author deserves some respect and the same should be shown towards them by purchasing the books and lauding the efforts of author. Hence we recommend to buy the book. You can buy the book for an affordable rate in given below link: Using of PDF will be at your own risk and EXTC RESOURCES IS NOT RESPONSIBLE for any consequences of the same. We provide links for book and donot endorse piracy.  

Modern Digital and Analog Communication (BP Lathi,3rd ed)

Another Analog communication book: Modern Digital and Analog Communication (BP Lathi,3rd ed) : Click here to download Modern Digital and Analog Communication (BP Lathi,3rd ed) Copyright Disclaimer: This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.

Numerical methods with MATLAB

For Numerical methods along with MATLAB the best book is: Chapra Applied Numerical Methods MATLAB Engineers Scientists 3rd edition : Click here to download Steven Chapra with MATLAB For altenate link : Click here to download Steen Chapra with MATLAB