
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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].
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
- Related Articles
- Strictly increasing or decreasing array - JavaScript
- Longest subarray which only contains strictly increasing numbers JavaScript
- Converting array into increasing sequence in JavaScript
- Make Array Strictly Increasing in C++
- Count Strictly Increasing Subarrays in C++
- A strictly increasing linked list in Python
- Program to check whether list is strictly increasing or strictly decreasing in Python
- Find an element in an array such that elements form a strictly decreasing and increasing sequence in Python
- Three strictly increasing numbers (consecutive or non-consecutive). in an array in JavaScript
- JavaScript: Check if array has an almost increasing sequence
- Finding nth element of an increasing sequence using JavaScript
- Find Maximum Sum Strictly Increasing Subarray in C++
- Check if list is strictly increasing in Python
- How to get almost increasing sequence of integers in JavaScript ?
- Print all n-digit strictly increasing numbers in C++
