1

I am beginning to learn Typescript on Codesignal platform. I came across this exercise

Here is the question for those who cannot access the link:

Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, each statue having an 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.

Example

For statues = [6, 2, 3, 8], the output should be makeArrayConsecutive2(statues) = 3. Ratiorg needs statues of sizes 4, 5 and 7.

My solution was this

function makeArrayConsecutive2(statues: number[]): number   {
let sorted: number[] = statues.sort();
let full = [];
for (let i = sorted[0]; i <= sorted[sorted.length-1]; i++)    {
    full.push(i);
}
var diff = full.filter(myCallBack);


function myCallBack(el) {
    return sorted.indexOf(el) < 0;
}

return diff.length;

When I ran the tests, only 9/10 of them were passed. The one I failed was a hidden test.

Also, when I try not to use the filter() method and instead calculate the difference between the sorted and full array by:

return full.length - sorted.length;

I got only 8/10.

Could anyone give me the reason(s)?

El Niño
  • 87
  • 1
  • 1
  • 7
  • 1
    A guess: `statues.sort();` should be `statues.sort((a, b) => a - b);` [How to sort an array of integers correctly](https://stackoverflow.com/q/1063007) – VLAZ Jul 07 '21 at 06:31
  • 1
    That's a quite complicated way. Get the smallest, get the biggest and then return `(biggest - smallest) - + 1`. – Andreas Jul 07 '21 at 06:33
  • 1
    @Andreas assuming each number is unique. – VLAZ Jul 07 '21 at 06:34
  • 2
    Just FWIW: Assuming (because these descriptions are always inadequate) that they never give you the same size twice, you don't need to sort the array, and you don't need to filter anything. Make a single pass through the array to find the `min` and `max` values, and then it's literally `(max - min) + 1 - statues.length` . – T.J. Crowder Jul 07 '21 at 06:35
  • 1
    @VLAZ There's nothing in the description that would indicate problems like that. – Andreas Jul 07 '21 at 06:36
  • @T.J.Crowder: In fact, I have read the solutions and many others did just that, which is a beautiful answer. However I still want to figure out why my naive approach did not work. – El Niño Jul 07 '21 at 06:45
  • @ElNiño - They're probably marking it down for complexity. – T.J. Crowder Jul 07 '21 at 06:48
  • @VLAZ: Indeed, it was stated in the question that these statues are of DIFFERENT sizes. So the numbers must be unique. – El Niño Jul 07 '21 at 06:52
  • @T.J.Crowder Not sure if I'm getting what you said, but this is not a problem of complexity. There were 10 tests designed by the website to check if my solution covered all the possbile cases. As I said in the question, mine passed only 9 out of 10. – El Niño Jul 07 '21 at 06:54
  • 1
    @ElNiño - I can't speak for that specific website, but some of them absolutely do test for complexity and mark down solutions that are over-complex. Or it's what VLAZ pointed out half an hour ago (your `sort` is incorrect). That seems the more likely reason. – T.J. Crowder Jul 07 '21 at 06:58
  • @VLAZ : I just adjusted my code following your suggestion. Worked like a charm! Thank you all for your time and effort! – El Niño Jul 07 '21 at 07:09

0 Answers0