Are they consecutive?
- O(n log n) · Hard
- Full plan
- Python
- JavaScript
- lists
- sorting
- booleans
Problem
Tickets 14, 12, 15 and 13 are consecutive: line them up and they come one after another with no gaps.
Write a function that takes a list of whole numbers and returns true if they are consecutive and false if they are not.
The rules: the order they come in does not matter. None of the numbers along the way can be missing, so 1, 2, 3, 5 and 6 are not consecutive. No number can repeat either: 1, 2 and 1 are not consecutive, because the 1 shows up twice. Negatives count too. A single number is consecutive, and with an empty list you return false.
Examples
One after another
[1, 2, 3, 4, 5] → true
The 4 is missing
[1, 2, 3, 5, 6] → false
A repeat
[1, 2, 1] → false
Out of order
[14, 12, 15, 13] → true
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def are_consecutive(numbers):
passJavaScript
function areConsecutive(numbers) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.