For the above input my output is coming out to be -5 7 -5 10 ,can you please tell me my mistake

import java.util.Scanner;
import java.util.Stack;

public class ShriRamAndArrows {

public static void main(String[] args) {
	Scanner scn = new Scanner(System.in);
	int n = scn.nextInt();
	int[] arr = new int[n];

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

	remainingArrows(arr);
	

}

public static void remainingArrows(int[] a) {

	Stack<Integer> st = new Stack<>();
	for (int i = 0; i < a.length; i++) {
		int x = a[i];
		if (!st.isEmpty() && x < 0 && st.peek() > 0) {
			
			while (!st.isEmpty() && x < 0 && st.peek() > 0) {

				if (Math.abs(x) > Math.abs(st.peek())) {
					st.pop();
					st.push(x);
				} else if (Math.abs(x) == Math.abs(st.peek())) {
					st.pop();
				} else if(Math.abs(x) < Math.abs(st.peek())) {
					break;
				}

			}
		} else {
			st.push(x);
		}
	}

	Stack<Integer> st2=new Stack<>();
	while(!st.isEmpty()) {
		st2.push(st.peek());
		st.pop();
	}
	while(!st2.isEmpty()) {
		System.out.print(st2.pop()+" ");
	}
	
}

}

@Utkarsh-Patiyal-2067329909961088
do u in theory see the logic how -5 7 10 is coming as output

the stacks top is compared to


the element that is inserted to it.

do u want to update it byself

Hint:
when u insert -5 u compare stack.top() to newly inserted element and when u see +ve and -ve u pop the smaller one
so
the stack becomes
-5 7 10

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.