- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get almost increasing sequence of integers in JavaScript ?
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
- Related Articles
- JavaScript: Check if array has an almost increasing sequence
- Strictly increasing sequence JavaScript
- Converting array into increasing sequence in JavaScript
- Finding nth element of an increasing sequence using JavaScript
- How to get sequence number in loops with JavaScript?
- How to create an alternately increasing sequence in R?
- Building a lexicographically increasing sequence of first n natural numbers in JavaScript
- Removing least number of elements to convert array into increasing sequence using JavaScript
- How to get the product of two integers without using * JavaScript
- Longest path in 2-D that contains increasing sequence in JavaScript
- How to create a sequence increasing by 1 in R?
- How to create a random vector of integers with increasing values only in R?
- How to create an array of integers in JavaScript?
- C++ Program to Find the Longest Increasing Subsequence of a Given Sequence
- How to sort an array of integers correctly in JavaScript?

Advertisements