Given an unsorted array of integers, find the smallest number in the array, the largest number in the array, and the smallest number between the two array bounds that are not in the array.
For instance, given the array [-1, 4, 5, -23, 24], the smallest number is -23, the largest number is 24, and the smallest number between the array bounds is -22. You may assume the input is well-formed.
Your solution should return an array [smallest, minimumAbsent, largest]
The smallest
integer should be the integer from the array with the lowest value.
The largest
integer should be the integer from the array with the highest value.
The minimumAbsent
is the smallest number between the largest and the smallest number that is not in the array.
minMinMax([-1, 4, 5, -23, 24]); //[-23, -22, 24]
minMinMax([1, 3, -3, -2, 8, -1]); //[-3, 0, 8]
minMinMax([2, -4, 8, -5, 9, 7]); //[-5, -3,9]
This challenge comes from kodejuice 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!
Discussion (16)
Ruby:
Can you explain this part? I haven't seen sort used like this before.
Sure. But
Array#sort
is secondary here, it works with any array on the right hand side of an assignment.I wrote a post for my old blog about this, that should help clarify it:
legacyblog.citizen428.net/blog/201...
Holy crap I had no idea you could do that in Ruby. Thank ya for the old post that explained it very clearly.
That's something I mention in my post from 2010 and it still hasn't changed much. I think one of the problems is that it's not a common idiom in Rails and for many people Ruby == Rails.
BTW, it's exactly this destructuring mechanism that allows you to iterate over hashes in Ruby like this:
Hash#each
actually turns the key-value-pairs into two element arrays, which then essentially become the right hand side of the assignment to the block variables:That is fascinating, I had no idea. I will keep that in mind.
Haskell solution:
The
\\
operator is the list difference, and the..
can be used on any enum type to build a list. This means this works for a large number of types, like:My solution in Swift, I check if the array is not empty and bigger than two elements :
I'm sure there's less time complexity one but this boi will get its job done
Runnable example:
Here is the PHP code snippets:
Elm
Playground
Here.
TypeScript with tests
github.com/kiliman/dev-to-daily-ch...
Test
Here is a solution with LabVIEW. Not tried LabVIEW? The community edition beta is now open!🎉🎉
Explanatory post. Thanks so much for the details. I also want to share information. If you need a professional resume, write to these guys resumeedge.com/