Strictly increasing sequence JavaScript

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].

What is a 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.

Example

const isStrictlyIncreasing = (arr) => {
    let unwantedElements = 0;
    for(let i = 0; 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

false
true

Enhanced Solution with Edge Cases

The basic solution has a limitation - it doesn't consider which element to remove. Here's an improved version that handles more complex cases:

const canBeStrictlyIncreasing = (arr) => {
    let violations = 0;
    let problemIndex = -1;
    
    // Find violations
    for(let i = 0; i = arr[i + 1]){
            violations++;
            problemIndex = i;
            if(violations > 1) return false;
        }
    }
    
    // No violations - already strictly increasing
    if(violations === 0) return true;
    
    // Check if removing either element at problem index works
    return canRemoveAt(arr, problemIndex) || canRemoveAt(arr, problemIndex + 1);
};

const canRemoveAt = (arr, indexToRemove) => {
    for(let i = 0; i = arr.length) break;
        
        if(arr[i] >= arr[next]) return false;
    }
    return true;
};

// Test cases
console.log(canBeStrictlyIncreasing([1, 3, 2, 1])); // false
console.log(canBeStrictlyIncreasing([1, 3, 2]));    // true
console.log(canBeStrictlyIncreasing([1, 2, 10, 5, 7])); // true
console.log(canBeStrictlyIncreasing([2, 3, 1, 2])); // false

Output

false
true
true
false

Conclusion

The basic approach counts violations but doesn't validate which element to remove. The enhanced solution properly checks if removing specific elements creates a valid strictly increasing sequence.

Updated on: 2026-03-15T23:18:59+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements