Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Smallest number formed by shuffling one digit at most in JavaScript
We need to write a JavaScript function that takes a positive number and can perform at most one operation: remove a digit from any position and insert it at another position to create the smallest possible number.
Problem Statement
Given a positive number, we can choose any digit, remove it from its current position, and insert it at any other position (including the same position). The goal is to find the smallest number possible with this single operation.
Algorithm Approach
The strategy is to find the smallest digit in the number and move it to the front (leftmost position) to minimize the overall value. We use the reduce() method to find the smallest digit and its index, then use splice() and unshift() to rearrange the digits.
Example Implementation
const num = 354166;
const smallestShuffle = (num) => {
// Convert number to array of digit strings
const arr = String(num).split('');
// Find the smallest digit and its index
const { ind } = arr.reduce((acc, val, index) => {
let { value, ind } = acc;
if (value > val) {
value = val;
ind = index;
}
return { value, ind };
}, { value: Infinity, ind: -1 });
// Remove the smallest digit and move it to the front
const [item] = arr.splice(ind, 1);
arr.unshift(item);
// Convert back to number
return Number(arr.join(''));
};
console.log(smallestShuffle(num));
135466
Step-by-Step Breakdown
Step 1: Convert the number 354166 to an array: ['3', '5', '4', '1', '6', '6']
Step 2: Find the smallest digit '1' at index 3
Step 3: Remove '1' from index 3 and insert it at the beginning
Step 4: Result array becomes ['1', '3', '5', '4', '6', '6']
Step 5: Convert back to number: 135466
Alternative Approach with Multiple Test Cases
const smallestShuffle = (num) => {
const arr = String(num).split('');
let minValue = num;
// Try moving each digit to every possible position
for (let i = 0; i < arr.length; i++) {
const digit = arr[i];
const remaining = [...arr.slice(0, i), ...arr.slice(i + 1)];
for (let j = 0; j <= remaining.length; j++) {
const newArr = [...remaining.slice(0, j), digit, ...remaining.slice(j)];
const newNum = Number(newArr.join(''));
minValue = Math.min(minValue, newNum);
}
}
return minValue;
};
// Test with different numbers
console.log(smallestShuffle(354166)); // 135466
console.log(smallestShuffle(9876)); // 6987
console.log(smallestShuffle(1234)); // 1234
135466 6987 1234
Key Points
- The first approach is more efficient as it directly moves the smallest digit to the front
- The second approach tries all possible combinations but guarantees the absolute minimum
- Both approaches handle the constraint of performing at most one shuffle operation
- The
reduce()method efficiently finds the minimum value and its index in a single pass
Conclusion
Moving the smallest digit to the leftmost position typically produces the smallest possible number. The first approach provides an efficient O(n) solution for this digit rearrangement problem.
