Algorithm to find smallest integer by swapping a pair of digits in given integer
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Finding the smallest integer by swapping a pair of digits in a given integer can be a fascinating problem that combines elements of number theory with efficient algorithmic thinking. This article explores the technical intricacies required to tackle this problem.
Problem Definition
Given a non-negative integer, we aim to find the smallest possible integer that can be obtained by swapping exactly one pair of its digits. The objective is to utilize the least number of operations to derive the solution in the most efficient manner.
Technical Explanation
Step-by-Step Algorithm
- Input Conversion: Convert the given integer into a list of its digits for easy manipulation.
- Attempting Swaps: Iterate over each index in the list and attempt to find a smaller digit located after it which can be swapped. The goal is to find the lexicographically smallest result through a single swap.
- Track Minimum: Maintain variables to track the positions of digits that produce the smallest possible swap at any iteration.
- Perform Optimal Swap: Once the optimal pair of swap positions is determined, execute the swap using these indices.
- Output: Convert the modified list of digits back to an integer and return the result.
Example
Consider the number 2736:
- Initial check:
[2, 7, 3, 6] - Compare
2with subsequent digits - no improvement by swapping2. - Compare
7:- Swap
7and3:[2, 3, 7, 6]results in2376. - Swap
7and6:[2, 6, 3, 7]results in2637.
- The smallest number from these swaps is
2376.
Complexity
- Time Complexity: The algorithm runs in , where is the number of digits in the integer. This involves comparing each digit with all subsequent digits.
- Space Complexity: Since the space is primarily used for maintaining swap indices and digit storage, it is .
Edge Cases
- If the number is already the smallest permutation of its digits, no swap will yield a smaller integer.
- If the integer has all identical digits, any swap will result in the same integer.
Implementation
Here is a Python implementation of the described algorithm:
- Optimizations: Consider an approach, where you track the rightmost smallest digits during iteration to reduce redundant swaps.
- Numerical Properties: Investigate properties of digits' permutation to infer the bounds of possible results without iterating every possible swap.
Related reading
- Algorithm to find solution to puzzle
- Algorithm to find the intersection of two or more songs
- algorithm to find the largest area
- Algorithm to find the maximum sum in a sequence of overlapping intervals
- Algorithm to find the minimum number of rectangles covering certain elements in a 2d array
- Algorithm to find the minimum value point of a function
- Algorithm to find the most common substrings in a string
- Algorithm to find the next number in a sequence

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.