나는 이렇게 학습한다/Algorithm & SQL

0825. Find the first non-consecutive number

daco2020 2022. 8. 25. 23:42
반응형

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!


Solution:

def first_non_consecutive(arr):
    expected_value = arr.pop(0)
    for actual_value in arr:
        expected_value += 1
        if actual_value != expected_value:
            return actual_value


반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

0827. Sum Arrays  (0) 2022.08.27
0826. A wolf in sheep's clothing  (0) 2022.08.26
0824. N-th Power  (0) 2022.08.24
Alternate capitalization  (0) 2022.08.23
Deodorant Evaporator  (0) 2022.08.22