C
C
cicatrix2018-05-18 13:49:08
Programming
cicatrix, 2018-05-18 13:49:08

How to determine the continuity of array values ​​and find breaks?

There is a pre-sorted array A of N integer values.
It is required to determine whether it is continuous, i.e. A[n+1] = A[n]+1 and split this array into subarrays, wherever this condition is met.
So far, nothing has come to mind, except for a stupid search to check the condition A[n+1] = A[n]+1, I wanted to ask if there are faster ways?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
#
#, 2018-05-18
@cicatrix

value step fixed?
is it "1" ?
binary search is at your fingertips. you can predict the value of any

A[n]
both the original array and the fragment. based on the deviation, we can conclude that there is a gap (or several)
we must remember that binary sampling will be effective in terms of determining continuous fragments, to clarify the gaps, a heuristic modification of the
ps SharuPoNemnogu algorithm may be required exactly! .. in the general case, all reasoning about arithmetic progression and binary search are true for any progression step (not just 1)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question