# The Stock Span Problem

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} ## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

A Simple but inefficient method
Traverse the input price array. For every element being visited, traverse elements on left of it and increment the span value of it while elements on the left side are smaller.

Following is implementation of this method.

## C++

 `// C++ program for brute force method  ` `// to calculate stock span values  ` `#include ` `using` `namespace` `std;  ` ` `  `// Fills array S[] with span values  ` `void` `calculateSpan(``int` `price[], ``int` `n, ``int` `S[])  ` `{  ` `    ``// Span value of first day is always 1  ` `    ``S = 1;  ` ` `  `    ``// Calculate span value of remaining days   ` `    ``// by linearly checking previous days  ` `    ``for` `(``int` `i = 1; i < n; i++)  ` `    ``{  ` `        ``S[i] = 1; ``// Initialize span value  ` ` `  `        ``// Traverse left while the next element   ` `        ``// on left is smaller than price[i]  ` `        ``for` `(``int` `j = i - 1; (j >= 0) &&  ` `                ``(price[i] >= price[j]); j--)  ` `            ``S[i]++;  ` `    ``}  ` `}  ` ` `  `// A utility function to print elements of array  ` `void` `printArray(``int` `arr[], ``int` `n)  ` `{  ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `        ``cout << arr[i] << ``" "``;  ` `}  ` ` `  `// Driver code  ` `int` `main()  ` `{  ` `    ``int` `price[] = { 10, 4, 5, 90, 120, 80 };  ` `    ``int` `n = ``sizeof``(price) / ``sizeof``(price);  ` `    ``int` `S[n];  ` ` `  `    ``// Fill the span values in array S[]  ` `    ``calculateSpan(price, n, S);  ` ` `  `    ``// print the calculated span values  ` `    ``printArray(S, n);  ` ` `  `    ``return` `0;  ` `}  ` ` `  `// This is code is contributed by rathbhupendra `

## C

 `// C program for brute force method to calculate stock span values ` `#include ` ` `  `// Fills array S[] with span values ` `void` `calculateSpan(``int` `price[], ``int` `n, ``int` `S[]) ` `{ ` `    ``// Span value of first day is always 1 ` `    ``S = 1; ` ` `  `    ``// Calculate span value of remaining days by linearly checking ` `    ``// previous days ` `    ``for` `(``int` `i = 1; i < n; i++) { ` `        ``S[i] = 1; ``// Initialize span value ` ` `  `        ``// Traverse left while the next element on left is smaller ` `        ``// than price[i] ` `        ``for` `(``int` `j = i - 1; (j >= 0) && (price[i] >= price[j]); j--) ` `            ``S[i]++; ` `    ``} ` `} ` ` `  `// A utility function to print elements of array ` `void` `printArray(``int` `arr[], ``int` `n) ` `{ ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``printf``(``"%d "``, arr[i]); ` `} ` ` `  `// Driver program to test above function ` `int` `main() ` `{ ` `    ``int` `price[] = { 10, 4, 5, 90, 120, 80 }; ` `    ``int` `n = ``sizeof``(price) / ``sizeof``(price); ` `    ``int` `S[n]; ` ` `  `    ``// Fill the span values in array S[] ` `    ``calculateSpan(price, n, S); ` ` `  `    ``// print the calculated span values ` `    ``printArray(S, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation for brute force method to calculate stock span values ` ` `  `import` `java.util.Arrays; ` ` `  `class` `GFG { ` `    ``// method to calculate stock span values ` `    ``static` `void` `calculateSpan(``int` `price[], ``int` `n, ``int` `S[]) ` `    ``{ ` `        ``// Span value of first day is always 1 ` `        ``S[``0``] = ``1``; ` ` `  `        ``// Calculate span value of remaining days by linearly checking ` `        ``// previous days ` `        ``for` `(``int` `i = ``1``; i < n; i++) { ` `            ``S[i] = ``1``; ``// Initialize span value ` ` `  `            ``// Traverse left while the next element on left is smaller ` `            ``// than price[i] ` `            ``for` `(``int` `j = i - ``1``; (j >= ``0``) && (price[i] >= price[j]); j--) ` `                ``S[i]++; ` `        ``} ` `    ``} ` ` `  `    ``// A utility function to print elements of array ` `    ``static` `void` `printArray(``int` `arr[]) ` `    ``{ ` `        ``System.out.print(Arrays.toString(arr)); ` `    ``} ` ` `  `    ``// Driver program to test above functions ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `price[] = { ``10``, ``4``, ``5``, ``90``, ``120``, ``80` `}; ` `        ``int` `n = price.length; ` `        ``int` `S[] = ``new` `int``[n]; ` ` `  `        ``// Fill the span values in array S[] ` `        ``calculateSpan(price, n, S); ` ` `  `        ``// print the calculated span values ` `        ``printArray(S); ` `    ``} ` `} ` `// This code is contributed by Sumit Ghosh `

## Python3

 `# Python program for brute force method to calculate stock span values ` ` `  `# Fills list S[] with span values ` `def` `calculateSpan(price, n, S): ` `     `  `    ``# Span value of first day is always 1 ` `    ``S[``0``] ``=` `1` ` `  `    ``# Calculate span value of remaining days by linearly  ` `    ``# checking previous days ` `    ``for` `i ``in` `range``(``1``, n, ``1``): ` `        ``S[i] ``=` `1`   `# Initialize span value ` ` `  `        ``# Traverse left while the next element on left is ` `        ``# smaller than price[i] ` `        ``j ``=` `i ``-` `1` `        ``while` `(j>``=` `0``) ``and` `(price[i] >``=` `price[j]) : ` `                       ``S[i] ``+``=` `1` `                       ``j ``-``=` `1` `                        `  `# A utility function to print elements of array ` `def` `printArray(arr, n): ` ` `  `    ``for` `i ``in` `range``(n): ` `        ``print``(arr[i], end ``=` `" "``) ` ` `  `# Driver program to test above function     ` `price ``=` `[``10``, ``4``, ``5``, ``90``, ``120``, ``80``] ` `n ``=` `len``(price) ` `S ``=` `[``None``] ``*` `n ` ` `  `# Fill the span values in list S[] ` `calculateSpan(price, n, S) ` ` `  `# print the calculated span values ` `printArray(S, n) ` ` `  ` `  `# This code is contributed by Sunny Karira `

## C#

 `// C# implementation for brute force method ` `// to calculate stock span values ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// method to calculate stock span values ` `    ``static` `void` `calculateSpan(``int``[] price, ` `                              ``int` `n, ``int``[] S) ` `    ``{ ` ` `  `        ``// Span value of first day is always 1 ` `        ``S = 1; ` ` `  `        ``// Calculate span value of remaining ` `        ``// days by linearly checking previous ` `        ``// days ` `        ``for` `(``int` `i = 1; i < n; i++) { ` `            ``S[i] = 1; ``// Initialize span value ` ` `  `            ``// Traverse left while the next ` `            ``// element on left is smaller ` `            ``// than price[i] ` `            ``for` `(``int` `j = i - 1; (j >= 0) && (price[i] >= price[j]); j--) ` `                ``S[i]++; ` `        ``} ` `    ``} ` ` `  `    ``// A utility function to print elements ` `    ``// of array ` `    ``static` `void` `printArray(``int``[] arr) ` `    ``{ ` `        ``string` `result = ``string``.Join(``" "``, arr); ` `        ``Console.WriteLine(result); ` `    ``} ` ` `  `    ``// Driver function ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int``[] price = { 10, 4, 5, 90, 120, 80 }; ` `        ``int` `n = price.Length; ` `        ``int``[] S = ``new` `int``[n]; ` ` `  `        ``// Fill the span values in array S[] ` `        ``calculateSpan(price, n, S); ` ` `  `        ``// print the calculated span values ` `        ``printArray(S); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007. `

## PHP

 `= 0) &&  ` `            ``(``\$price``[``\$i``] >= ``\$price``[``\$j``]); ``\$j``--) ` `            ``\$S``[``\$i``]++; ` `    ``} ` `     `  `        ``// print the calculated  ` `        ``// span values ` `        ``for` `(``\$i` `= 0; ``\$i` `< ``\$n``; ``\$i``++) ` `        ``echo` `\$S``[``\$i``] . ``" "``;; ` `     `  `         `  `} ` ` `  `    ``// Driver Code ` `    ``\$price` `= ``array``(10, 4, 5, 90, 120, 80); ` `    ``\$n` `= ``count``(``\$price``); ` `    ``\$S` `= ``array``(``\$n``); ` ` `  `    ``// Fill the span values in array S[] ` `    ``calculateSpan(``\$price``, ``\$n``, ``\$S``); ` ` `  `// This code is contributed by Sam007 ` `?> `

Output :

```1 1 2 4 5 1
```

Time Complexity of the above method is O(n^2). We can calculate stock span values in O(n) time.

A Linear Time Complexity Method
We see that S[i] on day i can be easily computed if we know the closest day preceding i, such that the price is greater than on that day than the price on the day i. If such a day exists, let’s call it h(i), otherwise, we define h(i) = -1.
The span is now computed as S[i] = i – h(i). See the following diagram. To implement this logic, we use a stack as an abstract data type to store the days i, h(i), h(h(i)), and so on. When we go from day i-1 to i, we pop the days when the price of the stock was less than or equal to price[i] and then push the value of day i back into the stack.

Following is the implementation of this method.

## C++

 `// C++ linear time solution for stock span problem ` `#include ` `#include ` `using` `namespace` `std; ` ` `  `// A stack based efficient method to calculate ` `// stock span values ` `void` `calculateSpan(``int` `price[], ``int` `n, ``int` `S[]) ` `{ ` `    ``// Create a stack and push index of first ` `    ``// element to it ` `    ``stack<``int``> st; ` `    ``st.push(0); ` ` `  `    ``// Span value of first element is always 1 ` `    ``S = 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.top()] <= price[i]) ` `            ``st.pop(); ` ` `  `        ``// If stack becomes empty, then price[i] is ` `        ``// greater than all elements on left of it, ` `        ``// i.e., price, price, ..price[i-1].  Else ` `        ``// price[i] is greater than elements after ` `        ``// top of stack ` `        ``S[i] = (st.empty()) ? (i + 1) : (i - st.top()); ` ` `  `        ``// Push this element to stack ` `        ``st.push(i); ` `    ``} ` `} ` ` `  `// A utility function to print elements of array ` `void` `printArray(``int` `arr[], ``int` `n) ` `{ ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``cout << arr[i] << ``" "``; ` `} ` ` `  `// Driver program to test above function ` `int` `main() ` `{ ` `    ``int` `price[] = { 10, 4, 5, 90, 120, 80 }; ` `    ``int` `n = ``sizeof``(price) / ``sizeof``(price); ` `    ``int` `S[n]; ` ` `  `    ``// Fill the span values in array S[] ` `    ``calculateSpan(price, n, S); ` ` `  `    ``// print the calculated span values ` `    ``printArray(S, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java linear time solution for stock span problem ` ` `  `import` `java.util.Stack; ` `import` `java.util.Arrays; ` ` `  `public` `class` `GFG { ` `    ``// 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 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, price, ..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[]) ` `    ``{ ` `        ``System.out.print(Arrays.toString(arr)); ` `    ``} ` ` `  `    ``// Driver method ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `price[] = { ``10``, ``4``, ``5``, ``90``, ``120``, ``80` `}; ` `        ``int` `n = price.length; ` `        ``int` `S[] = ``new` `int``[n]; ` ` `  `        ``// Fill the span values in array S[] ` `        ``calculateSpan(price, n, S); ` ` `  `        ``// print the calculated span values ` `        ``printArray(S); ` `    ``} ` `} ` `// This code is contributed by Sumit Ghosh `

## Python3

 `# Python linear time solution for stock span problem ` ` `  `# A stack based efficient method to calculate s ` `def` `calculateSpan(price, S): ` `     `  `    ``n ``=` `len``(price) ` `    ``# Create a stack and push index of fist element to it ` `    ``st ``=` `[]  ` `    ``st.append(``0``) ` ` `  `    ``# Span value of first element is always 1 ` `    ``S[``0``] ``=` `1` ` `  `    ``# Calculate span values for rest of the elements ` `    ``for` `i ``in` `range``(``1``, n): ` `         `  `        ``# Pop elements from stack whlie stack is not ` `        ``# empty and top of stack is smaller than price[i] ` `        ``while``( ``len``(st) > ``0` `and` `price[st[``-``1``]] <``=` `price[i]): ` `            ``st.pop() ` ` `  `        ``# If stack becomes empty, then price[i] is greater ` `        ``# than all elements on left of it, i.e. price, ` `        ``# price, ..price[i-1]. Else the price[i] is ` `        ``# greater than elements after top of stack ` `        ``S[i] ``=` `i ``+` `1` `if` `len``(st) <``=` `0` `else` `(i ``-` `st[``-``1``]) ` ` `  `        ``# Push this element to stack ` `        ``st.append(i) ` ` `  ` `  `# A utility function to print elements of array ` `def` `printArray(arr, n): ` `    ``for` `i ``in` `range``(``0``, n): ` `        ``print` `(arr[i], end ``=``" "``) ` ` `  ` `  `# Driver program to test above function ` `price ``=` `[``10``, ``4``, ``5``, ``90``, ``120``, ``80``] ` `S ``=` `[``0` `for` `i ``in` `range``(``len``(price)``+``1``)] ` ` `  `# Fill the span values in array S[] ` `calculateSpan(price, S) ` ` `  `# Print the calculated span values ` `printArray(S, ``len``(price)) ` ` `  `# This code is contributed by Nikhil Kumar Singh (nickzuck_007) `

## C#

 `// C# linear time solution for ` `// stock span problem ` `using` `System; ` `using` `System.Collections; ` ` `  `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 st = ``new` `Stack(); ` `        ``st.Push(0); ` ` `  `        ``// Span value of first ` `        ``// element is always 1 ` `        ``S = 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.Count > 0 && price[(``int``)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, price, ..price[i-1]. Else price[i] ` `            ``// is greater than elements after top of stack ` `            ``S[i] = (st.Count == 0) ? (i + 1) : (i - (``int``)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` `i = 0; i < arr.Length; i++) ` `            ``Console.Write(arr[i] + ``" "``); ` `    ``} ` ` `  `    ``// Driver method ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{ ` `        ``int``[] price = { 10, 4, 5, 90, 120, 80 }; ` `        ``int` `n = price.Length; ` `        ``int``[] S = ``new` `int``[n]; ` ` `  `        ``// Fill the span values in array S[] ` `        ``calculateSpan(price, n, S); ` ` `  `        ``// print the calculated span values ` `        ``printArray(S); ` `    ``} ` `} ` ` `  `// This code is contributed by Arnab Kundu `

Output:

`1 1 2 4 5 1`

Time Complexity: O(n). It seems more than O(n) at first look. If we take a closer look, we can observe that every element of the array is added and removed from the stack at most once. So there are total 2n operations at most. Assuming that a stack operation takes O(1) time, we can say that the time complexity is O(n).

Auxiliary Space: O(n) in worst case when all elements are sorted in decreasing order.

Another approach: (without using stack)

## C++

 `// C++ program for a linear time solution for stock ` `// span problem without using stack ` `#include ` `#include ` `using` `namespace` `std; ` ` `  `// An efficient method to calculate stock span values ` `// implementing the same idea without using stack ` `void` `calculateSpan(``int` `A[], ``int` `n, ``int` `ans[]) ` `{ ` `    ``// Span value of first element is always 1 ` `    ``ans = 1; ` ` `  `    ``// Calculate span values for rest of the elements ` `    ``for` `(``int` `i = 1; i < n; i++) { ` `        ``int` `counter = 1; ` `        ``while` `((i - counter) >= 0 && A[i] >= A[i - counter]) { ` `            ``counter += ans[i - counter]; ` `        ``} ` `        ``ans[i] = counter; ` `    ``} ` `} ` ` `  `// A utility function to print elements of array ` `void` `printArray(``int` `arr[], ``int` `n) ` `{ ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``cout << arr[i] << ``" "``; ` `} ` ` `  `// Driver program to test above function ` `int` `main() ` `{ ` `    ``int` `price[] = { 10, 4, 5, 90, 120, 80 }; ` `    ``int` `n = ``sizeof``(price) / ``sizeof``(price); ` `    ``int` `S[n]; ` ` `  `    ``// Fill the span values in array S[] ` `    ``calculateSpan(price, n, S); ` ` `  `    ``// print the calculated span values ` `    ``printArray(S, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program for a linear time ` `// solution for stock span problem ` `// without using stack ` `class` `GFG { ` ` `  `    ``// An efficient method to calculate ` `    ``// stock span values implementing the ` `    ``// same idea without using stack ` `    ``static` `void` `calculateSpan(``int` `A[], ` `                              ``int` `n, ``int` `ans[]) ` `    ``{ ` `        ``// Span value of first element is always 1 ` `        ``ans[``0``] = ``1``; ` ` `  `        ``// Calculate span values for rest of the elements ` `        ``for` `(``int` `i = ``1``; i < n; i++) { ` `            ``int` `counter = ``1``; ` `            ``while` `((i - counter) >= ``0` `&& A[i] >= A[i - counter]) { ` `                ``counter += ans[i - counter]; ` `            ``} ` `            ``ans[i] = counter; ` `        ``} ` `    ``} ` ` `  `    ``// A utility function to print elements of array ` `    ``static` `void` `printArray(``int` `arr[], ``int` `n) ` `    ``{ ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `            ``System.out.print(arr[i] + ``" "``); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `price[] = { ``10``, ``4``, ``5``, ``90``, ``120``, ``80` `}; ` `        ``int` `n = price.length; ` `        ``int` `S[] = ``new` `int``[n]; ` ` `  `        ``// Fill the span values in array S[] ` `        ``calculateSpan(price, n, S); ` ` `  `        ``// print the calculated span values ` `        ``printArray(S, n); ` `    ``} ` `} ` ` `  `/* This code contributed by PrinciRaj1992 */`

## Python3

 `# Python3 program for a linear time ` `# solution for stock span problem  ` `# without using stack  ` ` `  `# An efficient method to calculate  ` `# stock span values implementing  ` `# the same idea without using stack  ` `def` `calculateSpan(A, n, ans): ` `     `  `    ``# Span value of first element ` `    ``# is always 1  ` `    ``ans[``0``] ``=` `1` ` `  `    ``# Calculate span values for rest ` `    ``# of the elements  ` `    ``for` `i ``in` `range``(``1``, n): ` `        ``counter ``=` `1` `         `  `        ``while` `((i ``-` `counter) >``=` `0` `and`  `              ``A[i] >``=` `A[i ``-` `counter]): ` `            ``counter ``+``=` `ans[i ``-` `counter] ` `        ``ans[i] ``=` `counter ` ` `  `# A utility function to print elements ` `# of array  ` `def` `printArray(arr, n): ` `     `  `    ``for` `i ``in` `range``(n): ` `        ``print``(arr[i], end ``=` `' '``) ` `    ``print``() ` ` `  `# Driver code ` `price ``=` `[ ``10``, ``4``, ``5``, ``90``, ``120``, ``80` `] ` `n ``=` `len``(price) ` `S ``=` `[``0``] ``*` `(n) ` ` `  `# Fill the span values in array S[]  ` `calculateSpan(price, n, S)  ` ` `  `# Print the calculated span values  ` `printArray(S, n) ` ` `  `# This code is contributed by Prateek Gupta  `

## C#

 `// C# program for a linear time ` `// solution for stock span problem ` `// without using stack ` `using` `System; ` `public` `class` `GFG { ` ` `  `    ``// An efficient method to calculate ` `    ``// stock span values implementing the ` `    ``// same idea without using stack ` `    ``static` `void` `calculateSpan(``int``[] A, ` `                              ``int` `n, ``int``[] ans) ` `    ``{ ` `        ``// Span value of first element is always 1 ` `        ``ans = 1; ` ` `  `        ``// Calculate span values for rest of the elements ` `        ``for` `(``int` `i = 1; i < n; i++) { ` `            ``int` `counter = 1; ` `            ``while` `((i - counter) >= 0 && A[i] >= A[i - counter]) { ` `                ``counter += ans[i - counter]; ` `            ``} ` `            ``ans[i] = counter; ` `        ``} ` `    ``} ` ` `  `    ``// A utility function to print elements of array ` `    ``static` `void` `printArray(``int``[] arr, ``int` `n) ` `    ``{ ` `        ``for` `(``int` `i = 0; i < n; i++) ` `            ``Console.Write(arr[i] + ``" "``); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{ ` `        ``int``[] price = { 10, 4, 5, 90, 120, 80 }; ` `        ``int` `n = price.Length; ` `        ``int``[] S = ``new` `int``[n]; ` ` `  `        ``// Fill the span values in array S[] ` `        ``calculateSpan(price, n, S); ` ` `  `        ``// print the calculated span values ` `        ``printArray(S, n); ` `    ``} ` `} ` `// This code has been contributed by 29AjayKumar `

Output:

`1 1 2 4 5 1`

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Article Tags :

73

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.