Split the List into Groups of Consecutive Numbers

Published by Mubashir Hassan in

The function is given two parameters: an array of integers and the group’s length. Determine if it is possible to split all numbers from the array into groups of the specified length such that there are consecutive numbers in each group, return true / false.

Examples

consecutiveNums([1, 3, 5], 1) ➞ true
// It is always possible to create groups of length 1.

consecutiveNums([5, 6, 3, 4], 2) ➞ true
// Two groups of length 2: [3, 4], [5, 6]

consecutiveNums([1, 3, 4, 5], 2) ➞ false
// It is possible to make one group of length 2, but not a second one.

consecutiveNums([1, 2, 3, 6, 2, 3, 4, 7, 8], 3) ➞ true
// [1, 2, 3], [2, 3, 4], [6, 7, 8]

consecutiveNums([1, 2, 3, 4, 5], 4) ➞ false
// The list length is not divisible by the group’s length.

Notes

N/A

Watch a quick demo on how Edabit works.