- Related Questions & Answers
- How to get almost increasing sequence of integers in JavaScript ?
- Converting array into increasing sequence in JavaScript
- Strictly increasing sequence JavaScript
- Finding nth element of an increasing sequence using JavaScript
- Check if an Array has fixed size or not in C#
- Check if a string has white space in JavaScript?
- Removing least number of elements to convert array into increasing sequence using JavaScript
- Finding Fibonacci sequence in an array using JavaScript
- Check if given array is almost sorted (elements are at-most one position away) in Python
- How do I check if an array includes an object in JavaScript?
- How do we check if an object is an array in Javascript?
- How to check if an element has a class in jQuery?
- Check if bits of a number has count of consecutive set bits in increasing order in Python
- Check if list is strictly increasing in Python
- How to check if a variable is an array in JavaScript?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

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

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));

This will produce the following output on console −

true false

Advertisements