- Related Questions & Answers
- Check three consecutive numbers - JavaScript
- Finding three desired consecutive numbers in JavaScript
- Strictly increasing or decreasing array - JavaScript
- Finding the first non-consecutive number in an array in JavaScript
- JavaScript to check consecutive numbers in array?
- Check if three consecutive elements in an array is identical in JavaScript
- JavaScript Find the first non-consecutive number in Array
- Maximum consecutive numbers present in an array in C++
- N consecutive odd numbers JavaScript
- Sum of consecutive numbers in JavaScript
- Count of pairs in an array that have consecutive numbers using JavaScript
- Consecutive elements sum array in JavaScript
- Strictly increasing sequence JavaScript
- Make Array Strictly Increasing in C++
- Consecutive Numbers Sum in C++

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

Suppose, we have an array of numbers like this −

const arr = [4, 7, 4, 8, 9, 3];

We are required to write a JavaScript function that takes in one such array of numbers. The function should search for such three numbers from the array whose indices as well as their magnitude is in the strictly increasing order (consecutive or non-consecutive).

For example, in the above array, the numbers 7, 8 and 9 have index 1, 3 and 4. These numbers satisfy both the conditions, so our function should return true for this array.

The code for this will be −

const arr = [4, 7, 4, 8, 9, 3]; const findMatch = (arr) => { let stack = []; let s3 = −Infinity for (let i = arr.length − 1; i >= 0; i−−) { if (arr[i] < s3) return true while (stack.length > 0 && stack[stack.length − 1] < arr[i]) { s3 = stack.pop() }; stack.push(arr[i]) }; return false }; console.log(findMatch(arr));

And the output in the console will be −

false

Advertisements