|  | 
|  | 1 | +package me.ramswaroop.arrays; | 
|  | 2 | + | 
|  | 3 | +import me.ramswaroop.arrays.sorting.QuickSort; | 
|  | 4 | + | 
|  | 5 | +/** | 
|  | 6 | + * Created by IntelliJ IDEA. | 
|  | 7 | + * | 
|  | 8 | + * @author: ramswaroop | 
|  | 9 | + * @date: 10/30/15 | 
|  | 10 | + * @time: 11:01 AM | 
|  | 11 | + */ | 
|  | 12 | +public class NextLargerNumber { | 
|  | 13 | + | 
|  | 14 | +    /** | 
|  | 15 | +     * Finds the closest number which is larger | 
|  | 16 | +     * than {@param n} by using only those digits | 
|  | 17 | +     * present in {@param n} and using any digit | 
|  | 18 | +     * only once. | 
|  | 19 | +     * | 
|  | 20 | +     * @param n | 
|  | 21 | +     * @return | 
|  | 22 | +     */ | 
|  | 23 | +    public static int findNextLargerNumber(Integer n) { | 
|  | 24 | + | 
|  | 25 | +        String str = n.toString(); | 
|  | 26 | +        int len = str.length(); | 
|  | 27 | +        int[] a = new int[len]; | 
|  | 28 | +        int minIndex; | 
|  | 29 | + | 
|  | 30 | +        // construct int array containing all  | 
|  | 31 | +        // digits in number {@param n}   | 
|  | 32 | +        for (int i = 0; i < len; i++) { | 
|  | 33 | +            a[i] = Integer.parseInt(str.charAt(i) + ""); | 
|  | 34 | +        } | 
|  | 35 | + | 
|  | 36 | +        int i = len - 1; | 
|  | 37 | +        while (i > 0) { | 
|  | 38 | +            if (a[i] > a[i - 1]) break; | 
|  | 39 | +            i--; | 
|  | 40 | +        } | 
|  | 41 | + | 
|  | 42 | +        if (i <= 0) return -1; | 
|  | 43 | +         | 
|  | 44 | +        minIndex = i; | 
|  | 45 | +        int j = len - 1; | 
|  | 46 | +        while (j >= i) { | 
|  | 47 | +            if (a[j] < a[minIndex] && a[j] > a[i - 1]) { | 
|  | 48 | +                minIndex = j; | 
|  | 49 | +            } | 
|  | 50 | +            j--; | 
|  | 51 | +        } | 
|  | 52 | + | 
|  | 53 | +        swap(a, i - 1, minIndex); | 
|  | 54 | + | 
|  | 55 | +        QuickSort.quickSort(a, i, len - 1); | 
|  | 56 | + | 
|  | 57 | +        StringBuilder builder = new StringBuilder(); | 
|  | 58 | +        for (int k = 0; k < len; k++) { | 
|  | 59 | +            builder.append(a[k]); | 
|  | 60 | +        } | 
|  | 61 | + | 
|  | 62 | +        return Integer.parseInt(builder.toString()); | 
|  | 63 | +    } | 
|  | 64 | + | 
|  | 65 | +    /** | 
|  | 66 | +     * Swaps variables in {@param a} at {@param index1} with {@param index2}. | 
|  | 67 | +     * | 
|  | 68 | +     * @param a | 
|  | 69 | +     * @param index1 | 
|  | 70 | +     * @param index2 | 
|  | 71 | +     */ | 
|  | 72 | +    private static void swap(int[] a, int index1, int index2) { | 
|  | 73 | +        int temp = a[index1]; | 
|  | 74 | +        a[index1] = a[index2]; | 
|  | 75 | +        a[index2] = temp; | 
|  | 76 | +    } | 
|  | 77 | + | 
|  | 78 | +    public static void main(String a[]) { | 
|  | 79 | +        System.out.println(findNextLargerNumber(56)); | 
|  | 80 | +        System.out.println(findNextLargerNumber(65)); | 
|  | 81 | +        System.out.println(findNextLargerNumber(3451)); | 
|  | 82 | +        System.out.println(findNextLargerNumber(534976)); | 
|  | 83 | +    } | 
|  | 84 | +} | 
0 commit comments