1 test case is failing

Hi,

Could you please check, what’s wrong in my code.

import java.util.*;
public class Main {

public static void getNextGreaterElement(int[] arr, int n){
	
	if(n == 1){
		System.out.print("-1 ");
		return;
	}
	
	Stack<Integer> stack = new Stack<>();
	stack.push(arr[0]);

	int i =0, c = i+1;

	while(c != i){

		if(c == n){
			c = 0;
		}

		while(!stack.isEmpty() && arr[c] > stack.peek())
		{
			System.out.print(arr[c] + " ");
			stack.pop();
		}

		
		
		if(c == i){
			break;
		}
		
		stack.push(arr[c]);
		c++;
	}
	while(!stack.isEmpty()){
	
		System.out.print("-1 ");
		stack.pop();
	}
}

public static void main(String args[]) {
    // Your Code Here
	Scanner scan = new Scanner(System.in);
	int n = scan.nextInt();

	int[] arr = new int[n];

	for(int i=0; i<n; i++){
		arr[i] = scan.nextInt();
	}

	getNextGreaterElement(arr, n);
}

}

For input

3
3 2 1

Output should be

-1 3 3

Because array is circular,debug for this

Hi @aa1,
Doubt Resolved. Thank you so much ! :slightly_smiling_face:

1 Like