Next Greater Element Geeks For Geeks And InterviewBit
Approach 1: Using ArrayList ArrayList<Long> aa=new ArrayList<Long>(); Stack<Long>stack=new Stack<Long>(); for(int i=n-1;i>-1;i--){ if(stack.isEmpty()==true){ aa.add(Long.valueOf(-1)); } else{ if(stack.peek()>arr[i]){ aa.add(stack.peek()); } else{ while(!stack.isEmpty() && stack.peek()<=arr[i]){ ...