DEV Community

Daily Challenge #184 - Form the Minimum

dev.to staff on February 11, 2020

Setup Given an array of integers, implement a function that will return the lowest possible number that can be formed from these digits....
Collapse
 
craigmc08 profile image
Craig McIlwrath

Haskell. Also works for empty lists, returns 0.

import Data.List (nub, sort)
import Data.Maybe (fromMaybe) 
import Text.Read (readMaybe) 

minVal :: [Int] -> Int
minVal = fromMaybe 0 . readMaybe . concatMap show . sort . nub 
Collapse
 
not_jffrydsr profile image
@nobody

this is why Haskell scares me :o
that looks like Overlord Wizardry compared
to Clojure

Collapse
 
cipharius profile image
Valts Liepiņš • Edited

Ruby:

def minValue arr
    set = arr.uniq.sort
    set[0..1] = set[0..1].rotate if set[0] == 0 # Edge case for sets with 0
    set.join.to_i
end

Edit: Fixed the 0 edge case. Instead of adding 0 to the end of the number, put it in the 2. position.

Collapse
 
hyftar profile image
Simon Landry

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?

Collapse
 
cipharius profile image
Valts Liepiņš

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].

Collapse
 
viktordimitrievski profile image
Viktor

My try to solve this challenge :)

In my solution I don't include 0 in end result.

var minValue = arr => parseInt(arr.sort().map((e,i,a) => {if( e != a[i+1])return e;}).filter(x=>x).toString().replace(/,/g, ""));
Collapse
 
mategreen profile image
matej

Java

public String minValue(int[] ints) {
   return Arrays.stream(ints)
      //.filter(i -> i > 0) //does not include zeroes
      .distinct()
      .sorted()
      .mapToObj(i -> String.valueOf(i))
      .collect(Collectors.joining());
}
Collapse
 
emadsaber profile image
emadsaber

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);
}

Collapse
 
vidit1999 profile image
Vidit Sarkar • Edited

Here is my C++ code

vector<int> minValueHelper(vector<int> v){
    set<int> s;
    bool presentZero = false;
    for(int i : v){
        if(i==0)
            presentZero = true;
        s.insert(i);
    }
    if(presentZero){
        // because 0 cannot be in front of any number
        // minValue({1, 1, 7, 0}) should not return 017 but should return 107
        vector<int> v(s.begin(),s.end());

        // if only zero is given in vector return zero
        // else swaping
        if(v.size() > 1)
            swap(v[0],v[1]);
        return v;
    }
    return vector<int>(s.begin(),s.end());
}

int minValue(vector<int> v){
    vector<int> minVector = minValueHelper(v);
    string s = "";
    for(int i : minVector)
        s += to_string(i);
    return stoi(s);
}

Collapse
 
divyanshpratap profile image
divyansh-pratap • Edited

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);

for(i=0;i<a;i++)
{
    printf("enter the %dth element" , i+1);
    scanf("%d" , &A[i]);
}
s = min(A , a);
printf("/n %d" , s);

}
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;

  sort(A , n);
  s=A[0];
  for(i=1;i<n;i++)
  {

    j=A[i];

    for(k=0;k<i;k++)
    {
        if(A[i]==A[k])
        {
         l=0;
          }
      }
    if(l==1)
    {
        s=s*10 + j;
      }

     l=1 ;

  }

  return s;

}

Collapse
 
mellen profile image
Matt Ellen-Tsivintzeli • Edited
const minValue = ns => parseInt([...(new Set(ns))].sort().join(''));
Collapse
 
georgewl profile image
George WL • Edited

Even works for [7,0,2], neat

Collapse
 
savagepixie profile image
SavagePixie • Edited

JavaScript

const formMin = arr => +Array
  .from(new Set(arr))
  .sort((a, b) => a - b)
  .join('')
Collapse
 
maskedman99 profile image
Rohit Prasad

Python

var = input("Enter the numbers: ")
var = list(set(var))
var.sort()
var = ''.join(var)
print(var)