Find Next Smallest Number by Rearranging Same Digits

package org.wesome.dsalgo;

import java.util.Arrays;

public class NextSmallestNumber {
    static int[] nextSmallestNumber(int[] arr) {
        int indx, size = arr.length;
        /*  loop from left end towards start and find the point where current element arr[i] is greater than its right arr[i-1] */
        for (indx = size - 1; indx > 0; indx--) {
            if (arr[indx] < arr[indx - 1]) {
                break;
            }
        }
        /*  if indx reached till start, means number is already in the smallest combination */
        if (indx == 0) {
            System.out.println(Arrays.toString(arr) + " is already in smallest combination");
        } else {
            /*  loop from indx to end and find the max element which is less than arr[indx-1] and greater than arr[indx] ie a[indx-1] < max < arr[indx]    */
            int max = indx;
            for (int j = indx + 1; j < size; j++) {
                if (arr[j] < arr[indx - 1] && arr[j] > arr[max]) {
                    max = j;
                }
            }
            /*  Swap arr[indx-1] and min   */
            int temp = arr[indx - 1];
            arr[indx - 1] = arr[max];
            arr[max] = temp;
            /*  Sort all remaining elements after min in ascending order    */
            Arrays.sort(arr, indx, size);
            /*  reverse element from indx to end    */
            reverse(arr, indx, size - 1);
            System.out.print("smaller arr with same set of digits is " + Arrays.toString(arr));
        }
        return arr;
    }

    static void reverse(int str[], int start, int end) {
        int temp;
        while (start <= end) {
            temp = str[start];
            str[start] = str[end];
            str[end] = temp;
            start++;
            end--;
        }
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.wesome.dsalgo.NextSmallestNumber.nextSmallestNumber;

public class NextSmallestNumberTest {
    @Test
    void nextSmallestNumberTest() {
        int arr[] = {2, 6, 2, 3, 4, 5};
        Assertions.assertArrayEquals(new int[]{2, 5, 6, 4, 3, 2}, nextSmallestNumber(arr));
    }

    @Test
    void nextSmallestNumberTest1() {
        int arr[] = {7, 2, 3, 4, 5};
        Assertions.assertArrayEquals(new int[]{5, 7, 4, 3, 2}, nextSmallestNumber(arr));
    }

    @Test
    void nextSmallestNumberTest2() {
        int arr[] = {1, 2, 3};
        Assertions.assertArrayEquals(new int[]{1, 2, 3}, nextSmallestNumber(arr));
    }

    @Test
    void nextSmallestNumberTest3() {
        int arr[] = {1, 3, 2};
        Assertions.assertArrayEquals(new int[]{1, 2, 3}, nextSmallestNumber(arr));
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.2.0"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on