# Strictly increasing sequence JavaScript

JavascriptWeb DevelopmentObject Oriented Programming

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

For example −

For sequence = [1, 3, 2, 1], the output should be function(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 function(sequence) = true. You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to get the strictly increasing sequence [1, 3].

## Strictly increasing sequence

It is a mathematical term that represents an arrangement of numbers where every succeeding number is greater than its preceding number. Other than this there exists increasing sequence where the succeeding element is greater than or equal to the preceding element.

The same logic applies for decreasing sequence and strictly decreasing sequence.

## Approach

We will loop over the array checking whether the succeeding element is greater than preceding element or not. If it’s greater, then it’s fine with us, but if it is not greater (remember it has to be greater and not greater or equal because we want to form a strictly increasing sequence.) we will keep a count of unwantedElements and increase it by 1 every time this happens.

If during iteration, the count exceeds 1, we return false then and there otherwise if we go through the whole with unwantedElements <= 1, we return true.

Therefore, let’s write the code for this function −

## Example

const isStrictlyIncreasing = (arr) => {
let unwantedElements = 0;
for(let i = 0; i < arr.length - 1; i++){
if(arr[i] >= arr[i+1]){
unwantedElements++;
if(unwantedElements > 1){
return false;
};
};
};
return true;
};
console.log(isStrictlyIncreasing([1, 3, 2, 1]));
console.log(isStrictlyIncreasing([1, 3, 2]));

## Output

The output in the console will be −

false
true
Published on 24-Aug-2020 12:38:43