Find the Smallest number (Not leading Zeros) which can be obtained by rearranging the digits of a given number.
Examples:
Input: n = 846903
Output: 304689
Input: n = 55010
Output: 10055
Input: n = -40505
Output: -55400
Steps to find the smallest number.
- Count the frequency of each digit in the number.
- If it is a non-negative number then
- Place the smallest digit (except 0) at the left most of the required number.
and decrement the frequency of that digit by 1. - Place all remaining digits in ascending order from left to right.
- Else if it is a negative number then
- Place the largest digit at the left most of the required number. and decrement the frequency of that digit by 1.
- Place all remaining digits in descending order from right to left.
This solution is based on counting sort.
C++
#include<iostream>
using namespace std;
int smallest( int num)
{
int freq[10] = {0};
bool is_pos = (num>0);
num = abs (num);
while (num)
{
int d = num % 10;
freq[d]++;
num = num / 10;
}
int result = 0;
if (is_pos)
{
for ( int i = 1 ; i <= 9 ; i++)
{
if (freq[i])
{
result = i;
freq[i]--;
break ;
}
}
for ( int i = 0 ; i <= 9 ; i++)
while (freq[i]--)
result = result * 10 + i;
}
else
{
for ( int i = 9 ; i >= 1 ; i--)
{
if (freq[i])
{
result = i;
freq[i]--;
break ;
}
}
for ( int i = 9 ; i >=0 ; i--)
while (freq[i]--)
result = result * 10 + i;
result = -result;
}
return result;
}
int main()
{
int num = 570107;
cout << smallest(num) << endl;
int num2 = -691005;
cout << smallest(num2);
return 0;
}
|
Java
import java.lang.Math;
public class GFG {
static int smallest( int num)
{
int [] freq = new int [ 10 ];
boolean is_pos = (num> 0 );
num = Math.abs(num);
while (num > 0 )
{
int d = num % 10 ;
freq[d]++;
num = num / 10 ;
}
int result = 0 ;
if (is_pos)
{
for ( int i = 1 ; i <= 9 ; i++)
{
if (freq[i] != 0 )
{
result = i;
freq[i]--;
break ;
}
}
for ( int i = 0 ; i <= 9 ; i++)
while (freq[i]-- != 0 )
result = result * 10 + i;
}
else
{
for ( int i = 9 ; i >= 1 ; i--)
{
if (freq[i] != 0 )
{
result = i;
freq[i]--;
break ;
}
}
for ( int i = 9 ; i >= 0 ; i--)
while (freq[i]-- != 0 )
result = result * 10 + i;
result = -result;
}
return result;
}
public static void main(String args[])
{
int num = 570107 ;
System.out.println(smallest(num));
int num2 = - 691005 ;
System.out.println(smallest(num2));
}
}
|
Python3
def smallest(lst):
for i,n in enumerate (lst):
if n ! = '0' :
tmp = lst.pop(i)
break
return str (tmp) + ''.join(lst)
if __name__ = = '__main__' :
lst = list ( str ( 570107 ))
lst.sort()
print (smallest(lst))
|
C#
using System;
public class GFG {
static int smallest( int num)
{
int [] freq = new int [10];
while (num > 0)
{
int d = num % 10;
freq[d]++;
num = num / 10;
}
int result = 0;
for ( int i = 1 ; i <= 9 ; i++)
{
if (freq[i] != 0)
{
result = i;
freq[i]--;
break ;
}
}
for ( int i = 0 ; i <= 9 ; i++)
while (freq[i]-- != 0)
result = result * 10 + i;
return result;
}
public static void Main()
{
int num = 570107;
Console.WriteLine(smallest(num));
}
}
|
PHP
<?php
function smallest( $num )
{
$freq = array_fill (0, 10, 0);
while ( $num )
{
$d = $num % 10;
$freq [ $d ]++;
$num = (int)( $num / 10);
}
$result = 0;
for ( $i = 1 ; $i <= 9 ; $i ++)
{
if ( $freq [ $i ])
{
$result = $i ;
$freq [ $i ]--;
break ;
}
}
for ( $i = 0 ; $i <= 9 ; $i ++)
while ( $freq [ $i ] > 0)
{
$result = $result * 10 + $i ;
$freq [ $i ] -= 1;
}
return $result ;
}
$num = 570107;
echo smallest( $num );
?>
|
Javascript
<script>
function smallest(num)
{
var freq = Array(10).fill(0);
while (num)
{
var d = num % 10;
freq[d]++;
num = parseInt(num / 10);
}
var result = 0;
for ( var i = 1 ; i <= 9 ; i++)
{
if (freq[i])
{
result = i;
freq[i]--;
break ;
}
}
for ( var i = 0 ; i <= 9 ; i++)
while (freq[i]--)
result = result * 10 + i;
return result;
}
var num = 570107;
document.write(smallest(num));
</script>
|
Time complexity: O(n), where n is the number of digits in the given number.
Space complexity: O(1), as only a constant amount of memory is used for the freq array.
Another Approach:Find smallest permutation of given number
This article is contributed by Ajay Kumar Agrahari(aJy aGr). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.