Given 2 numbers a and b find the smallest number greater than b by interchanging the digits of a and if not possible print -1 I have made the solution for this question it is based on backtracking. Can anyone suggest me how to make it work faster, I haven't practiced backtracking that much, although my solution seems to work. Please check it out

    class Test {
    static int ans = Integer.MAX_VALUE;

    static void f(char[] a, String s, int r, boolean[] vis, int b) {
        // System.out.println(s);
        if (r == 0) {
            int x = Integer.parseInt(s);
            if (x &gt; b &amp;&amp; x &lt; ans)
                ans = Math.min(x, ans);

            return;
        }

        for (int i = 0; i &lt; a.length; i++) {
            if (!vis[i]) {
                s += a[i];
                vis[i] = true;
                f(a, s, r - 1, vis, b);
                s = s.substring(0, s.length() - 1);
                vis[i] = false;
            }
        }

    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[] a = sc.next().toCharArray();
        // System.out.println(Arrays.toString(a));
        boolean[] vis = new boolean[a.length];
        int b = sc.nextInt();
        f(a, "", a.length, vis, b);

        System.out.println(ans == Integer.MAX_VALUE ? -1 : ans);

    }
} 



vis array is used to track which element is already picked same thing
which we do in DFS.
r is the count of remaining elements in a.
main()

When I started your program the first time, I didn't know what to do, because your program didn't tell me. Before scanning a user input, I would tell the user to input something:

Scanner sc = new Scanner(System.in);
System.out.println("Please enter number a:");
char[] a = sc.next().toCharArray();
System.out.println("Please enter number b:");
int b = sc.nextInt();




For number a, I would also use an int instead of a char[]. Then I would convert it to a char[], but not in the main-function:

System.out.println("Please enter number a:");
int a = sc.nextInt();
System.out.println("Please enter number b:");
int b = sc.nextInt();




Another problem now is that your program doesn't validate the user input. The user could enter a letter instead of a number and your program would just break. This problem can be solved like this:

System.out.println("Please enter number a:");
int a;
while(true) {
    try {
        a = sc.nextInt();
        break;  
    }
    catch(InputMismatchException e) {
         System.out.println("That's not a number!");
         sc.nextLine();
    }
}
System.out.println("Please enter number b:");
int b;
while(true) {
    try {
        b = sc.nextInt();
        break;
    }
    catch(InputMismatchException e) {
        System.out.println("That's not a number!");
        sc.nextLine();
    }
}




f()

Create a method static void f(int numberA, int b) that creates the char-array and the boolean[] vis and then calls the method static void f(char[] a, String s, int r, boolean[] vis, int b). That way you don't have to do work in the main-method that doesn't belong to the main-method:

static void f(int numberA, int b) {
        char[] a = ("" + numberA).toCharArray();
        boolean[] vis = new boolean[a.length];

        f(a, "", a.length, vis, b);
        System.out.println(ans == Integer.MAX_VALUE ? -1 : ans);

}




I don't like most of your variable names. You should (almost) always use names that tell the person who reads your code, what the variable does.



Your algorithm looks solid. Well done.



All in all it looks like this:

import java.util.Scanner;
import java.util.InputMismatchException;

class ChangeDigits {
    static int ans = Integer.MAX_VALUE;

    static void changeDigits(int numberA, int numberB) {
        char[] change = ("" + numberA).toCharArray();
        boolean[] pickedElements = new boolean[change.length];
        int remaining = change.length;
        changeDigits(change, "", remaining, pickedElements, numberB);
        System.out.println("Solution: " + (ans == Integer.MAX_VALUE ? -1 : ans));

    }

    static void changeDigits(char[] change, String substring, int remaining, boolean[] pickedElements, int numberB) {
        if (remaining == 0) {
            int substrNumber = Integer.parseInt(substring);
            if (substrNumber &gt; numberB &amp;&amp; substrNumber &lt; ans)
                ans = Math.min(substrNumber, ans);
            return;
        }

        for (int i = 0; i &lt; change.length; i++) {
            if (!pickedElements[i]) {
                substring += change[i];
                pickedElements[i] = true;
                changeDigits(change, substring, remaining - 1, pickedElements, numberB);
                substring = substring.substring(0, substring.length() - 1);
                pickedElements[i] = false;
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter number a:");
        int numberA;
        while(true) {
            try {
                numberA = sc.nextInt();
                break;
            }
            catch(InputMismatchException e) {
                System.out.println("That's not a number!");
                sc.nextLine();
            }
        }

        System.out.println("Please enter number b:");
        int numberB;
        while(true) {
            try {
                numberB = sc.nextInt();
                break;
            }
            catch(InputMismatchException e) {
                System.out.println("That's not a number!");
                sc.nextLine();
            }
        }

        changeDigits(numberA, numberB);
    }
}