Setup
Given an array of integers, implement a function that will return the lowest possible number that can be formed from these digits. You cannot use the same number more than once, even if it appears in the array multiple times.
Examples
minValue({1, 3, 1})
==> return 13
minValue({5, 7, 5, 9, 7})
==> return 579
Tests
minValue({1, 2, 3, 4})
minValue({1, 1, 7, 0})
minValue({9, 2, 1, 4, 3, 9, 5})
Good luck!
This challenge comes from MrZizoScream on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (14)
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