-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakeArrayConsecutive2.java
34 lines (29 loc) · 1.09 KB
/
MakeArrayConsecutive2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package Codesignal;
import java.util.Arrays;
public class MakeArrayConsecutive2 {
/*
* 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.
*
* */
int makeArrayConsecutive2(int[] statues) {
int count = 0;
Arrays.sort(statues);
for (int i = 0; i < statues.length-1; i++) {
if (statues[i+1] - statues[i] > 1) {
for (int temp = statues[i]+1; temp < statues[i+1]; temp++){
count++;
}
}
}
return count;
}
}