1 December 2014

Quiz 60: Make the shortest numbers

Problem:

Given a number, find the smallest possible number using the digits of original number without any leading 0

Input Format: 

Number

Output Format: 

Smallest possible number


Constraints: 

None

Sample Input

70205008


Sample Output:

20000578

Explanations:

meets the problem statement


Solution:

chomp($a=<STDIN>);
@arr=split(//,$a);
@arr=sort(@arr);
$len=@arr;
$j=0;
for($i=0;$i<$len;$i++)
{
if($arr[$i]==0)
{
$j++;
}
else
{
last;
}
}
if($j>0)
{
$arr[0]=$arr[$j];
$arr[$j]=0;
}
print @arr;





Tips:

Sort array and then count number of  leading zeros and code accordingly

No comments:

Post a Comment