2 testcases failing

import java.util.*;
public class Main {
public static void main (String args[]) {

	Scanner sc= new Scanner(System.in);
	int num= sc.nextInt();

	List<Integer> list= new ArrayList<>();

	while(num>0){
		list.add(num%10);
		num=num/10;
	}
	Collections.reverse(list);

	int i= list.size()-1;
	int min=list.size()-1;
	while(i>0){
		if(list.get(i)>list.get(i-1)){
			break;
		}
		min= (list.get(i)<list.get(min)) ? i : min;
		i--;
	}
	if(i==0){
		System.out.println(-1);
		return;
	}
	i--;

	//swap
	int temp= list.get(i);
	list.set(i,list.get(min));
	list.set(min, temp);

	Collections.sort(list.subList(i+1,list.size()));
	num=0;
	for(int x:list){
		num=num*10 + x;
	}

	System.out.println(num);
	
}

}

Hello, @ANUKUL1509,

Your Implementation is wrong, for cases like this it is printing the wrong answer,
N = 231
Your Output = 123
Correct Output = 312

@ANUKUL1509,

You can check out my implementation if you are having some difficulty.
Implementation for Large Number

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.