- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

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.

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 −

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

The output in the console will be −

false true

- Related Questions & Answers
- Strictly increasing or decreasing array - JavaScript
- Longest subarray which only contains strictly increasing numbers JavaScript
- Make Array Strictly Increasing in C++
- Count Strictly Increasing Subarrays in C++
- Converting array into increasing sequence in JavaScript
- A strictly increasing linked list in Python
- Program to check whether list is strictly increasing or strictly decreasing in Python
- Find Maximum Sum Strictly Increasing Subarray in C++
- Check if list is strictly increasing in Python
- JavaScript: Check if array has an almost increasing sequence
- Finding nth element of an increasing sequence using JavaScript
- Find an element in an array such that elements form a strictly decreasing and increasing sequence in Python
- Print all n-digit strictly increasing numbers in C++
- Three strictly increasing numbers (consecutive or non-consecutive). in an array in JavaScript
- How to get almost increasing sequence of integers in JavaScript ?

Advertisements