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
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);
    }
}