DSA Continued: Make Array Consecutive 2

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.
function makeArrayConsecutive2(statues) {    
let statuesNeeded = 0;
function makeArrayConsecutive2(statues) {    
let statuesNeeded = 0;
// Sort array smallest to largest
statues.sort((a, b) => {
return a - b;
})
function makeArrayConsecutive2(statues) {    
let statuesNeeded = 0;
// Sort array smallest to largest
statues.sort((a, b) => {
return a - b;
})
// Iterate through array and find gaps in values
for(let i = 0; i < statues.length; i++) {
function makeArrayConsecutive2(statues) {    
let statuesNeeded = 0;
// Sort array smallest to largest
statues.sort((a, b) => {
return a - b;
})
// Iterate through array and find gaps in values
for(let i = 0; i < statues.length; i++) {
if(statues[i + 1] - statues[i] > 1) {
statuesNeeded += statues[i + 1] - statues[i] - 1;
}
}
function makeArrayConsecutive2(statues) {    
let statuesNeeded = 0;
// Sort array smallest to largest
statues.sort((a, b) => {
return a - b;
})
// Iterate through array and find gaps in values
for(let i = 0; i < statues.length; i++) {
if(statues[i + 1] - statues[i] > 1) {
statuesNeeded += statues[i + 1] - statues[i] - 1;
}
}
return statuesNeeded;
}

--

--

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