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 numberConstraints:
NoneSample Input
70205008
Sample Output:
20000578Explanations:
meets the problem statement
Solution:
@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