# JavaScript: Check if array has an almost increasing sequence

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

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.

The sequence a0, a1, ..., an is considered to be a strictly increasing if a0 < a1 < ... < an. Sequence containing only one element is also considered to be strictly increasing.

## Example

For sequence = [1, 3, 2, 1], the output should be −

almostIncreasingSequence(sequence) = false.

There is no one element in this array that can be removed in order to get a strictly increasing sequence.

For sequence = [1, 3, 2], the output should be −

almostIncreasingSequence(sequence) = true.

We can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, we can remove 2 to get the strictly increasing sequence [1, 3].

## Example

Following is the code −

const arr1 = [3, 5, 67, 98, 3];
const arr2 = [4, 3, 5, 67, 98, 3];
const almostIncreasingSequence = sequence => {
let removed = 0;
let i = 0;
let prev = -Infinity;
while(removed < 2 && i < sequence.length) {
if(sequence[i] > prev) {
prev = sequence[i];
}else{
prev = Math.min(prev, sequence[i]);
removed++;
}
i++;
}
return removed < 2;
};
console.log(almostIncreasingSequence(arr1));
console.log(almostIncreasingSequence(arr2));

## Output

This will produce the following output on console −

true
false
Updated on 01-Oct-2020 10:25:39