Anzar
Anzar

Reputation: 35

Almost Increasing Sequence, Few Test Cases Fail

Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.

def almostIncreasingSequence(sequence):
    i = 0
    count = 0
    
    while i < len(sequence) - 1:
        if sequence[i] > sequence[i+1]:
            count += 1
        i += 1
            
    if count == 1:
        return True    
    else:
        return False

The code fails the following sequence [40, 50, 60, 10, 20, 30]

It'd output false but I got true

Upvotes: 0

Views: 178

Answers (1)

user2357112
user2357112

Reputation: 281519

You're not checking the property you were supposed to check. You were supposed to check if you can get a strictly increasing sequence by removing 0 or 1 elements from the input. Instead, you've checked whether there is exactly 1 pair of decreasing successive elements in the input.

Finding a decreasing pair is still useful, because if there is such a pair, then you have to remove one of the elements involved. (Remove anything else, and you still have that decreasing pair, and the sequence is still not strictly increasing.) One straightforward way to solve this problem is to search for a decreasing pair, then just check if the sequence would have been increasing without one of the two elements involved. (If you don't find a decreasing pair, you also return True.)

Upvotes: 1

Related Questions