Sunday, 24 January 2016

Kata Day 58: How many are smaller than me


Problem Description: Write a function smaller(arr) that given an array arr, you have to return the amount of numbers that are smaller than arr[i] to the right.

For example:

smaller([5, 4, 3, 2, 1]) === [4, 3, 2, 1, 0]
smaller([1, 2, 0]) === [1, 1, 0]

Level: 7kyu

Link To Kata: How many are smaller than me 

In this Kata we have to return the amount of numbers that are smaller than the given number at the index of given array.

We start solving this Kata by we loop through the given array and initialize another index variable j to 0 for every looping index. We also initialize a count variable to 0.Then we check how many elements to the right of the element at current index are less than the element at current index. For every match we increase the count by 1. In the end we push the count to a new answer array. We repeat this process for every element in the given array and return the answer array as the solution to this Kata in the end.

Here is the link to code: Kata Day 58

Please mention your suggestions or your doubts in the comment below.

Happy coding. :)

1 comment:

  1. You're proposing a quadradic solution to a problem that can be solved with O(n log n) temporal complexity.

    ReplyDelete