Finding the immediate bigger number formed with the same digits in JavaScript

We need to write a JavaScript function that takes a number and rearranges its digits to form the smallest number that is just bigger than the input number using the same digits.

For instance, if the input number is 112, the output should be 121. If no bigger number can be formed (like 321), we return -1.

Algorithm Approach

The brute force approach involves:

  • Find the maximum possible number by sorting digits in descending order
  • Check each number from input+1 to maximum
  • Return the first number that uses the same digits

Example Implementation

const num = 112;
const findNextBigger = (num = 1) => {
    const sortedDigits = (num = 1) => {
        return String(num)
            .split('')
            .sort((a, b) => b - a);
    };
    let max = sortedDigits(num).join('');
    max = Number(max);
    for(let i = num + 1; i <= max; i++){
        if(max === +sortedDigits(i).join('')){
            return i;
        };
    };
    return -1;
};
console.log(findNextBigger(num));
121

Testing Multiple Cases

const findNextBigger = (num = 1) => {
    const sortedDigits = (num = 1) => {
        return String(num)
            .split('')
            .sort((a, b) => b - a);
    };
    let max = sortedDigits(num).join('');
    max = Number(max);
    for(let i = num + 1; i <= max; i++){
        if(max === +sortedDigits(i).join('')){
            return i;
        };
    };
    return -1;
};

console.log("Input: 112, Output:", findNextBigger(112));
console.log("Input: 123, Output:", findNextBigger(123));
console.log("Input: 321, Output:", findNextBigger(321));
console.log("Input: 1234, Output:", findNextBigger(1234));
Input: 112, Output: 121
Input: 123, Output: 132
Input: 321, Output: -1
Input: 1234, Output: 1243

How It Works

The solution works by:

  1. Finding the maximum: Sort digits in descending order to get the largest possible number
  2. Iterating: Check each number from input+1 to maximum
  3. Comparing digits: For each candidate, sort its digits and compare with the maximum
  4. Returning result: Return the first match, or -1 if none found

Optimized Approach

For better performance with large numbers, we can use the next permutation algorithm:

const findNextBiggerOptimized = (num) => {
    let digits = String(num).split('');
    let i = digits.length - 2;
    
    // Find the first digit that is smaller than the digit next to it
    while (i >= 0 && digits[i] >= digits[i + 1]) {
        i--;
    }
    
    // If no such digit found, return -1
    if (i < 0) return -1;
    
    // Find the smallest digit greater than digits[i]
    let j = digits.length - 1;
    while (digits[j] <= digits[i]) {
        j--;
    }
    
    // Swap digits[i] and digits[j]
    [digits[i], digits[j]] = [digits[j], digits[i]];
    
    // Reverse the suffix starting at i+1
    let left = i + 1;
    let right = digits.length - 1;
    while (left < right) {
        [digits[left], digits[right]] = [digits[right], digits[left]];
        left++;
        right--;
    }
    
    return Number(digits.join(''));
};

console.log("Optimized - Input: 112, Output:", findNextBiggerOptimized(112));
console.log("Optimized - Input: 123, Output:", findNextBiggerOptimized(123));
console.log("Optimized - Input: 321, Output:", findNextBiggerOptimized(321));
Optimized - Input: 112, Output: 121
Optimized - Input: 123, Output: 132
Optimized - Input: 321, Output: -1

Conclusion

The brute force method works for small numbers but becomes inefficient for larger inputs. The optimized next permutation algorithm provides O(n) time complexity and is recommended for production use.

Updated on: 2026-03-15T23:19:00+05:30

373 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements