import java.util.*;
public class Solution {
// Define a pair class to store value and index
public static class pair{
int val;
int ind;
pair(int val, int ind){
this.val = val;
this.ind = ind;
}
}
public static int[] NGE_Left(int arr[], int n){
int res[] = new int[n];
Stack<pair> s = new Stack<>();
for(int i = 0; i < n; i++){
while(s.size() > 0 && s.peek().val < arr[i]){
s.pop();
}
if(s.size() == 0){
res[i] = -1;
} else if(s.peek().val >= arr[i]){
res[i] = s.peek().ind;
}
s.push(new pair(arr[i], i));
}
return res;
}
public static int[] findStockSpans(int []prices) {
int ans[] = NGE_Left(prices, prices.length);
for(int i = 0; i < prices.length; i++){
ans[i] = i - ans[i];
}
return ans;
}
}