{"id":13655,"date":"2021-10-01T10:00:02","date_gmt":"2021-10-01T04:30:02","guid":{"rendered":"https:\/\/python-programs.com\/?p=13655"},"modified":"2021-11-22T18:33:31","modified_gmt":"2021-11-22T13:03:31","slug":"python-program-to-rearrange-the-given-number-to-form-the-smallest-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-rearrange-the-given-number-to-form-the-smallest-number\/","title":{"rendered":"Python Program to Rearrange the given Number to form the Smallest Number"},"content":{"rendered":"
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.<\/p>\n
Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to rearrange the given Number to form the smallest Number in Python.<\/p>\n Beginners and experienced programmers can rely on these Best Java Programs Examples<\/a> and code various basic and complex logics in the Java programming language with ease.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Time Complexity: O(N log N) where N is the number len of the 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 …<\/p>\nGiven number =17831047264891<\/pre>\n
The smallest number that can be formed from 17831047264891 is [ 10112344677889 ]<\/pre>\n
Given number =4851381128859729830005768<\/pre>\n
The smallest number that can be formed from 4851381128859729830005768 is [ 1000112233455567788888899 ]<\/pre>\n
Program to Rearrange the given Number to form the Smallest Number<\/h2>\n
\n
Method #1: Using Sorting (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\nnumb = 17831047264891\r\n# Convert the given number to a string and sort it.\r\nstrnumb = str(numb)\r\nstrnumb = sorted(strnumb)\r\n# After sorting join it using the join() function.\r\nsortednumb = ''.join(strnumb)\r\n# Count the number of 0's present in this number\r\n# using the count() function and store it in a variable say m.\r\nm = sortednumb.count('0')\r\n# Convert the given number to a list of digits using the list() function.\r\nnumbdigi = list(sortednumb)\r\n# Swap the first index digit and mth index digit in the list using ',' operator.\r\nnumbdigi[0], numbdigi[m] = numbdigi[m], numbdigi[0]\r\n# Join the list into the string using the join() function.\r\nfinalres = ''.join(numbdigi)\r\n# Print the result\r\nprint('The smallest number that can be formed from',\r\n numb, 'is [', finalres, ']')\r\n<\/pre>\n
The smallest number that can be formed from 17831047264891 is [ 10112344677889 ]<\/pre>\n
Method #2: Using Sorting (User Input)<\/h3>\n
\n
# Give the number as user input using int(input()) and store it in a variable.\r\nnumb = int(input('Enter some random number = '))\r\n# Convert the given number to a string and sort it.\r\nstrnumb = str(numb)\r\nstrnumb = sorted(strnumb)\r\n# After sorting join it using the join() function.\r\nsortednumb = ''.join(strnumb)\r\n# Count the number of 0's present in this number\r\n# using the count() function and store it in a variable say m.\r\nm = sortednumb.count('0')\r\n# Convert the given number to a list of digits using the list() function.\r\nnumbdigi = list(sortednumb)\r\n# Swap the first index digit and mth index digit in the list using ',' operator.\r\nnumbdigi[0], numbdigi[m] = numbdigi[m], numbdigi[0]\r\n# Join the list into the string using the join() function.\r\nfinalres = ''.join(numbdigi)\r\n# Print the result\r\nprint('The smallest number that can be formed from',\r\n numb, 'is [', finalres, ']')\r\n<\/pre>\n
Enter some random number = 4851381128859729830005768\r\nThe smallest number that can be formed from 4851381128859729830005768 is [ 1000112233455567788888899 ]<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n