DSA: shapeArea

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.ExampleFor statues = [6, 2, 3, 8], the output should be
makeArrayConsecutive2(statues) = 3.
Ratiorg needs statues of sizes 4, 5 and 7.
function makeArrayConsecutive2(statues) {   
let o = 0;
}
function makeArrayConsecutive2(statues) {    
let o = 0;
statues.sort((a, b) => b - a);
}
function makeArrayConsecutive2(statues) {    
let o = 0;
statues.sort((a, b) => b - a);
for(let i = 0; i < statues.length - 1; i++) {
o += statues[i] - statues[i + 1] - 1;
}
}
function makeArrayConsecutive2(statues) {    
let o = 0;
statues.sort((a, b) => b - a);
for(let i = 0; i < statues.length - 1; i++) {
o += statues[i] - statues[i + 1] - 1;
}
return o;
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store