Skip to content

Make Array Consecutive

Trump got statues of different sizes as a present from Hilary 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 solution(statues) = 3. Trump needs statues of sizes 4, 5 and 7.

Input

  • Input: array.integer statues An array of distinct non-negative integers. Guaranteed constraints: 1 ≤ statues.length ≤ 10, 0 ≤ statues[i] ≤ 20.

Output

The minimal number of statues that need to be added to existing statues such that it contains every integer size from an interval [L, R] (for some L, R) and no other sizes.

Solution

Solution
rust
fn solution(statues: Vec<i32>) -> i32 {
    statues.iter().max().unwrap() - statues.iter().min().unwrap() + 1 - (statues.len() as i32)
}
fn solution(statues: Vec<i32>) -> i32 {
    statues.iter().max().unwrap() - statues.iter().min().unwrap() + 1 - (statues.len() as i32)
}