Program to Rearrange the given Number to form the Smallest Number

Python Program to Rearrange the given Number to form the Smallest Number

In Python, we will write a program that will rearrange the digits of a given number to produce the smallest possible number. We will rearrange the number so that it creates the smallest possible number with the number and the number digits being the same as the given number.

Examples:

Example1:

Input:

Given number =17831047264891

Output:

The smallest number that can be formed from 17831047264891 is [ 10112344677889 ]

Example2:

Input:

Given number =4851381128859729830005768

Output:

The smallest number that can be formed from 4851381128859729830005768 is [ 1000112233455567788888899 ]

Program to Rearrange the given Number to form the Smallest Number

Below are the ways to rearrange the given Number to form the smallest Number in Python.

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Method #1: Using Sorting (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Convert the given number to a string and sort it.
  • After sorting join it using the join() function.
  • Count the number of 0’s present in this number using the count() function and store it in a variable say m.
  • Convert the given number to a list of digits using the list() function.
  • Swap the first index digit and mth index digit in the list using ‘,’ operator.
  • Join the list into the string using the join() function.
  • Print the result.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
numb = 17831047264891
# Convert the given number to a string and sort it.
strnumb = str(numb)
strnumb = sorted(strnumb)
# After sorting join it using the join() function.
sortednumb = ''.join(strnumb)
# Count the number of 0's present in this number
# using the count() function and store it in a variable say m.
m = sortednumb.count('0')
# Convert the given number to a list of digits using the list() function.
numbdigi = list(sortednumb)
# Swap the first index digit and mth index digit in the list using ',' operator.
numbdigi[0], numbdigi[m] = numbdigi[m], numbdigi[0]
# Join the list into the string using the join() function.
finalres = ''.join(numbdigi)
# Print the result
print('The smallest number that can be formed from',
      numb, 'is [', finalres, ']')

Output:

The smallest number that can be formed from 17831047264891 is [ 10112344677889 ]

Method #2: Using Sorting (User Input)

Approach:

  • Give the number as user input using int(input()) and store it in a variable.
  • Convert the given number to a string and sort it.
  • After sorting join it using the join() function.
  • Count the number of 0’s present in this number using the count() function and store it in a variable say m.
  • Convert the given number to a list of digits using the list() function.
  • Swap the first index digit and mth index digit in the list using ‘,’ operator.
  • Join the list into the string using the join() function.
  • Print the result.
  • The Exit of the Program.

Below is the implementation:

# Give the number as user input using int(input()) and store it in a variable.
numb = int(input('Enter some random number = '))
# Convert the given number to a string and sort it.
strnumb = str(numb)
strnumb = sorted(strnumb)
# After sorting join it using the join() function.
sortednumb = ''.join(strnumb)
# Count the number of 0's present in this number
# using the count() function and store it in a variable say m.
m = sortednumb.count('0')
# Convert the given number to a list of digits using the list() function.
numbdigi = list(sortednumb)
# Swap the first index digit and mth index digit in the list using ',' operator.
numbdigi[0], numbdigi[m] = numbdigi[m], numbdigi[0]
# Join the list into the string using the join() function.
finalres = ''.join(numbdigi)
# Print the result
print('The smallest number that can be formed from',
      numb, 'is [', finalres, ']')

Output:

Enter some random number = 4851381128859729830005768
The smallest number that can be formed from 4851381128859729830005768 is [ 1000112233455567788888899 ]

Time Complexity: O(N log N) where N is the number len of the number.
Related Programs: