https://www.geeksforgeeks.org/problems/next-larger-element-1587115620/1?itm_source=geeksforgeeks&itm_medium=article&itm_campaign=practice_card class Solution { // Function to find the next greater element for each element of the array. public ArrayList nextLargerElement(int[] arr) { // code here ArrayList al=new ArrayList<>(); Stack s=new Stack<>(); for(int i=arr.length-1;i>=0;i--){ int x=arr[i]; while(!s.isEmpty() && x>=s.peek()){ s.pop(); } if(s.isEmpty()){ al.add(-1); }else{ al.add(s.peek()); } //6 2 6 //S= 6 // 6 -1 //9 1 3 2 5 //S=5 3 1 //arr=-1 3 5 5 -1 s.push(x); } Collections.reverse(al); return al; // int arr1[]=new int[al.size()]; // int j=0; // for(Integer i:al){ // arr1[j]=i; // j++; // } // return arr1; } } ============================================================ https://www.geeksforgeeks.org/problems/smallest-number-on-left3403/1?itm_source=geeksforgeeks&itm_medium=article&itm_campaign=practice_card class Solution{ static List leftSmaller(int n, int a[]) { //code here Stack stack=new Stack<>(); ArrayList al=new ArrayList<>(); for(int i=0;i