Simple java solution using stack and traversing from back of the array


#1

Comment body goes here.import java.util.Stack;
public class Solution {
public int[] nextGreater(int[] A) {

    int arr[]=new int[A.length];
    Stack<Integer> s1=new Stack<Integer>();
    for(int i=A.length-1;i>=0;i--)
    {
        while(!s1.empty() && A[i]>=s1.peek())
        {
            s1.pop();
        }
        
        
        if(s1.empty())
        {
        arr[i]=-1;
        }
        else
        {
            arr[i]=s1.peek();
        }
        
        s1.push(A[i]);
}
return arr;

}}