Setup
Given an array of integers, implement a function that will return the lowest possible number that can be formed from these digits....
For further actions, you may consider blocking this person and/or reporting abuse
Haskell. Also works for empty lists, returns 0.
this is why Haskell scares me :o
that looks like Overlord Wizardry compared
to
Clojure
Ruby:
Edit: Fixed the 0 edge case. Instead of adding 0 to the end of the number, put it in the 2. position.
Wouldn't rotating the array bring the biggest number at the front?
I think swapping the 1st and 2nd number would be ideal.
Also is having a leading zero really against the rules?
Yeah, swapping seems to be better, would keep the large digits in the lesser valuable positions.
I interpreted the rules this way, because it didn't seem right to lose a digit in the resulting number. That would be like assuming input set
[1,2,0]
as equivelent to[1,2]
.My try to solve this challenge :)
In my solution I don't include 0 in end result.
Java
Javascript
function getMinimum(arr){
let sorted = arr.sort();
let distinct = '';
sorted.forEach(function(element){
if(distinct.indexOf(element) < 0){
distinct =
${distinct}${element}
;}
});
console.log(parseInt(distinct));
return parseInt(distinct);
}
Here is my C++ code
this is solution in c . I have first sorted the array then solved it using simple logics . suggestions are welcome .
include
int sort(int A[] , int n );
int min(int A[] , int n);
int main()
{
int A[30];
int a , i , s;
printf("enter the number of elements you want to enter");
scanf("%d" ,&a);
}
int sort(int A[] , int n )
{
int i;
int temp , j;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(A[j]<A[i])
{
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
}
}
int min(int A[] ,int n)
{
int s , j=0 , k , i , l=1;
}
Even works for [7,0,2], neat
JavaScript
Python