Kotlin - Smart Solutions #0

henrique_horbovyi

Henrique Horbovyi

Posted on April 12, 2020

Kotlin - Smart Solutions #0

Hello World, this is the first post of a series in which I want to share to you some cool Kotlin features.

The Problem (Make Array Consecutive)

I've faced this problem at Code Signal (it's a code challenge site). The code challenge description is the follows:

  • "Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, each statue having a non-negative integer size. Since he likes to make things perfect, he wants to arrange them from smallest to largest so that each statue will be bigger than the previous one exactly by 1. He may need some additional statues to be able to accomplish that. Help him figure out the minimum number of additional statues needed."

There is one example to make things more clear

For statues = [6, 2, 3, 8], the output should be
makeArrayConsecutive(statues) = 3.

Ratiorg needs statues of sizes 4, 5 and 7.

The Solution 💡

So basically, we need to sort all statues and count the number of missing statues between each one, but how? Take a minute to think about it and then check the solution in the code snippet below.

The Solution

Debugging and Understanding 👨‍💻

  • First thing, we need to sort all statues to guarantee that
    [6, 2, 3, 8] will become [2, 3, 6, 8]

  • Next, I used subtract() method to remove the original list items from a new list which consists in a range between statues.first() and statues.last(), which is: statues.first()..statues.last()

    This range operation will return an int iterable like:
    [2, 3, 4, 5, 6, 7, 8]
    Then, after subtract() it will result in this list:
    [4, 5, 7], and that's awesome, these are the missing numbers in the original statues list.

  • Now, we need to count() the returned list and we have the final result. Simple no? :D

Conclusion

In this article, we explored range operator, subtract, and count functions of Kotlin, how to use it and apply it in a problem.

And if you have some questions, any suggestions, a better solution or even some new ideas for the next Kotlin Smart Solutions post, don't be shy, let me know and leave me a comment. ;D

I hope you learned something new around here.
Thank you!
#KeepLearning

💖 💪 🙅 🚩
henrique_horbovyi
Henrique Horbovyi

Posted on April 12, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related