알고리즘 38 - Find the first non-consecutive number
Q.
Your task is to find the first element of an array that is not consecutive.
By not consecutive we mean not exactly 1 larger than the previous element of the array.
E.g. If we have an array [1,2,3,4,6,7,8] then 1 then 2 then 3 then 4 are all consecutive but 6 is not, so that's the first non-consecutive number.
If the whole array is consecutive then return null2.
The array will always have at least 2 elements1 and all elements will be numbers. The numbers will also all be unique and in ascending order. The numbers could be positive or negative and the first non-consecutive could be either too!
If you like this Kata, maybe try this one next: https://www.codewars.com/kata/represent-array-of-numbers-as-ranges
1 Can you write a solution that will return null2 for both [] and [ x ] though? (This is an empty array and one with a single number and is not tested for, but you can write your own example test. )
2
Swift, Ruby and Crystal: nil
Haskell: Nothing
Python, Rust, Scala: None
Julia: nothing
Nim: none(int) (See options)
A)
function firstNonConsecutive (arr) {
for (let i = 1; i < arr.length; i++) {
if (arr[i] - 1 !== arr[i - 1])
return arr[i];
}
return null;
}
Author And Source
이 문제에 관하여(알고리즘 38 - Find the first non-consecutive number), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://velog.io/@pearpearb/알고리즘-38-Find-the-first-non-consecutive-number
저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
function firstNonConsecutive (arr) {
for (let i = 1; i < arr.length; i++) {
if (arr[i] - 1 !== arr[i - 1])
return arr[i];
}
return null;
}
Author And Source
이 문제에 관하여(알고리즘 38 - Find the first non-consecutive number), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@pearpearb/알고리즘-38-Find-the-first-non-consecutive-number저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)