integer manipulation
digit swapping
algorithm design
smallest integer
computing techniques

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.

Practice algorithms

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

  1. Input Conversion: Convert the given integer into a list of its digits for easy manipulation.
  2. 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.
  3. Track Minimum: Maintain variables to track the positions of digits that produce the smallest possible swap at any iteration.
  4. Perform Optimal Swap: Once the optimal pair of swap positions is determined, execute the swap using these indices.
  5. 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 2 with subsequent digits - no improvement by swapping 2.
  • Compare 7:
    • Swap 7 and 3: [2, 3, 7, 6] results in 2376.
    • Swap 7 and 6: [2, 6, 3, 7] results in 2637.
  • The smallest number from these swaps is 2376.

Complexity

  • Time Complexity: The algorithm runs in O(n2)O(n^2), where nn 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 O(n)O(n).

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 O(n)O(n) 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms