A sorted array is such an array that its elements are arranged in a non-decreasing or non-increasing order, for example, {1, 1, 2, 4}

And the array {8, 7, 6, 222, 5, 4} will be sorted when the element "222" is deleted.

Provided that you can delete no more than one element (ie, 0 or 1), what will be the fastest algorithm for determining the "not completely" sorted array?

    1 answer 1

    Pass with ignoring one violation of sorting. Only if the problem is in the first three elements, it is necessary to consider the fourth, in order to understand, for example, what is superfluous in 1-0-5 - 0 or 5 :), i.e. diminishing or increasing.

    Faster O(n) does not work.

    • With a size of <4 will be O(1) . - αλεχολυτ
    • one
      @alexolut But if we recall that O(4) = O(1) , then the statement remains valid. :) - Yaant
    • @Harry Can you provide a rough description of the passage algorithm? - Ka_ktus
    • one
      Well, we compare the following with the current one, if everything is the same attitude - more or less there - we go on, no - we ignore the first discrepancy, according to the second we conclude that everything is bad :) Something like this. - Harry