Company: Accolite
Question:
EXECUTION TIME: 0.23s
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}
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:
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:
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 200
1 ≤ C[i] ≤ 800
Example:
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
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
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
Post a Comment