GeeksforGeeks/Java/Medium/Help Classmates/help-classmates.java
<reponame>AyushAgnihotri2025/CP-Solutions<filename>GeeksforGeeks/Java/Medium/Help Classmates/help-classmates.java//{ Driver Code Starts
//Initial Template for Java


import java.util.*; 
import java.io.*; 

class GFG 
{ 
	public static void main (String[] args) { 
		Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t > 0){
        	int n = sc.nextInt();
            int array[] = new int[n];
            for (int i = 0; i < n; ++i)
            {
                array[i] = sc.nextInt();
            }

            Solution ob = new Solution();

            int ans[] = ob.help_classmate(array,n);

           	for (int i=0; i<n; i++) 
                System.out.print(ans[i]+" "); 
            System.out.println();
            t--;
        }
	} 
} 


// } Driver Code Ends


//User function Template for Java

class Solution {
	public static int[] help_classmate(int arr[], int n) 
	{ 
	    // Your code goes here
	    Stack<Integer> s = new Stack<>();
        int[] nxtSmaller = new int[arr.length];
        for(int i=arr.length-1; i>=0; i--) {
            while(!s.isEmpty() && arr[s.peek()] >= arr[i])
                s.pop();
            if(s.isEmpty())
                nxtSmaller[i] = -1;
            else
                nxtSmaller[i] = arr[s.peek()];
            s.push(i);
        }
        return nxtSmaller;
	} 
}
