-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConsecutiveArray.java
More file actions
31 lines (23 loc) · 924 Bytes
/
Copy pathConsecutiveArray.java
File metadata and controls
31 lines (23 loc) · 924 Bytes
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
package questions.codesignal.consecutivearray;
import java.util.Arrays;
public class ConsecutiveArray {
public static int makeArrayConsecutive2(int[] statues) {
int N = statues.length;
if (N < 2) return 0;
// compare and track min and max of first two value in array
int min = Math.min(statues[0], statues[1]);
int max = Math.max(statues[0], statues[1]);
// compare and alter min and max with each item
for (int i = 2; i < N; i++) {
min = Math.min(min, statues[i]);
max = Math.max(max, statues[i]);
}
// max - min + 1 : gives the count of numbers (e.g. 2 to 8 is 7 nos.)
return max - min + 1 - N;
}
public static void main(String[] args) {
int[] statues = {6, 2, 3, 8};
System.out.println(Arrays.toString(statues));
System.out.println(makeArrayConsecutive2(statues));
}
}