This is my code . I think there are some mistakes in my code .
Can you please correct my code ?
import java.util.*;
public class Main {
public static void main(String args[]) throws Exception{
// Your Code Here
Scanner scan = new Scanner(System.in) ;
int n = scan.nextInt() ;
long[] arr = new long[n] ;
for(int i = 0 ; i < n ; i ++){
arr[i] = scan.nextLong() ;
}
nextGreater(arr) ;
}
public static void nextGreater(long[] arr) throws Exception{
Stack stack = new Stack() ;
for(int i = 0 ; i < arr.length ; i ++){
while(!stack.isEmpty() && arr[i] > stack.top()) {
System.out.print(arr[i]+" ") ;
stack.pop() ;
}
stack.push(arr[i]) ;
}
while(!stack.isEmpty()){
boolean visited = false ;
for(int i = 0 ; i < arr.length-1 ; i ++){
if(arr[i] > stack.top()){
visited = true ;
System.out.print(arr[i]+" ") ;
stack.pop() ;
break ;
}
}
if(!visited)
break ;
}
if(!stack.isEmpty()){
while(!stack.isEmpty()){
System.out.print(-1+" ") ;
stack.pop() ;
}
}
}
}