# Consecutive Coderbyte Solution

2

Have the function Consecutive(arr) take the array of integers stored in arr and return the minimum number of integers needed to make the contents of arr consecutive from the lowest number to the highest number.

For example: If arr contains [4, 8, 6] then the output should be 2 because two numbers need to be added to the array (5 and 7) to make it a consecutive array of numbers from 4 to 8.Negative numbers may be entered as parameters and no array will have less than 2 elements.

Consecutive Coderbyte Solution In Java

import java.util.Arrays;
import java.util.Scanner;

public class Consecutive {
int Consecutive(int[] arr) {
Arrays.sort(arr);

int amount = 0;
for (int i = 1; i < arr.length; i++) {
amount += Math.abs(arr[i] - arr[i - 1]) - 1;
}

return amount;

}

public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
Consecutive c = new Consecutive();
System.out.print(c.Consecutive(new
int[] {-2,10,4}));
}
}

Consecutive Coderbyte Solution In JavaScript

function Consecutive(arr) {
var highest = arr[0];
var lowest = arr[0];
var numbersBetween = [];

for (var i = 0; i < arr.length; i++) {
if (arr[i] > highest) {
highest = arr[i];
} else if (arr[i] < lowest) {
lowest = arr[i];
};
};

for (var j = lowest; j <= highest; j++) {
if (arr.indexOf(j) == -1) {
numbersBetween.push(j);
};
};

return numbersBetween.length;
};

Consecutive Coderbyte Solution In Python

def Consecutive(arr):
big = set(range(min(arr),max(arr)+1))
small = set(arr)
return len(big - small)

Tags