Strictly Increasing Sequence
A sequence of numbers is said to be in a strictly increasing sequence if every succeeding element in the sequence is greater than its preceding element.
We are required to write a JavaScript function that takes in an array of Numbers as the only argument. The function should check whether we can form a strictly increasing sequence of the numbers by removing no more than one element from the array.
Example
Following is the code −
const almostIncreasingSequence = (arr = []) => { if (isIncreasingSequence(arr)) { return true; }; for (let i = 0; i < arr.length > 0; i++) { let copy = arr.slice(0); copy.splice(i, 1); if (isIncreasingSequence(copy)) { return true; }; }; return false; }; const isIncreasingSequence = (arr = []) => { for (let i = 0; i < arr.length - 1; i++) { if (arr[i] >= arr[i + 1]) { return false; }; }; return true; }; console.log(almostIncreasingSequence([1, 3, 2, 1])); console.log(almostIncreasingSequence([1, 3, 2]));
Output
Following is the output on console −
false true