Three strictly increasing numbers (consecutive or non-consecutive) in an array in JavaScript

The Given problem is trying to find three strictly increasing numbers with consecutive or non−consecutive numbers in an array with the help of javascript.

Understanding the Problem

Before starting to write algorithms and programmes for the given problem first we will understand the logic behind the problem.

Consider a set of array with some random numbers in it. So JavaScript allows us to loop through an array and maintain track of the smallest and second−smallest values in order to find three strictly rising numbers.

In this algorithm, We will discover three strictly growing integers if a third value is found that is higher than both of these.


Step 1 − In the first step, construct a function called find3IncreasingNum. This function will check whether the three consecutive numbers are in increasing order or not.

Step 2 − The second step will define two variables with global scope named Infinity. Infinity will define the property of global scope.

Step 3 − The third step defines a for loop to iterate through all the items or numbers present in the array.

Step 4 − The next step specifies the current object which is initialized as the first element of the array. Then this current value will be checked with the next coming numbers and check that it is greater than the current number or smaller.

Step 5 − Now we have all checked all the numbers one by one. And reached the solution.

Step 6 − In the last step show the output after calling the function.


// sample array to store numbers
function find3IncreasingNum(arr) {
  let first = Infinity;
  let second = Infinity;
  for (let i = 0; i < arr.length; i++) { 
    const current = arr[i];
    if (current <= first) {
      first = current;
    } else if (current <= second) {
      second = current;
    } else {
      return true; // found a triplet
  return false; // no triplet found
const arr = [10, 20, 30, 40, 50];
const arr2 = [50, 40, 30, 20, 10];
const arr3 = [10, 50, 20, 40, 30];



This code defines three arrays of integer numbers with different values in it. And their names are arr, arr2, and arr3'. There are three kinds of outputs: true, false and true. First true is stating that the given input array has consecutive integer numbers, second output is stating that there are no consecutive numbers present in the array, and the third output is showing that there is a sequence of numbers present in the array.


Step 1 − Create a numbers array of n digits.

Step 2 − Define a new array and initialize as empty. This array will store groups of all consecutive elements of the array with sub array.

Step 3 − Create a sub array to store the consecutive numbers of the array.

Step 4 − Initialize a for loop for numArray till the length of the array.

Step 5 − Now push all the consecutive items in subArray from numArray.

Step 6 − Check the condition, If the difference between consecutive items is not equal to 1. Then push it to the newArray.

Step 7 − Now show the group of all consecutive digits as output.


const numArray = [40,39,38,50,49,48,70,20]; 
// define a new array
const newArray = [];  
// subArray for consecutive group
var subArray = []; 

for(var i=0; i<numArray.length; i++) { 
  // Push new item to subArray
  // If next item is not consecutive, push subArray to newArray
  if(numArray[i] - numArray[i+1] != 1) { 
    subArray = [];

console.log("Group of consecutive numbers")


Group of consecutive numbers
[ [ 40, 39, 38 ], [ 50, 49, 48 ], [ 70 ], [ 20 ] ]

Time and Space Complexity

The length of the input array, n, determines how long it takes to execute the function "find3IncreasingNum()" in terms of time. This is because the for loop it uses to once cycle through the entire array. For each element in the array, a fixed number of operations are also carried out.

The function occupies O(1) space. This complexity is brought on by the fact that, regardless of the size of the input array, the function consumes a fixed amount of extra space. To maintain track of the array's components, it merely constructs the first, second, and current variables.

As a result, the code's space complexity is constant while its time complexity is linear.


We could see how straightforward this issue was. We will have to think logically and follow certain steps in order to handle any difficulty. With various input arrays, we have seen three different outputs. Two metrics used to assess an algorithm's effectiveness are time complexity and space complexity.

Updated on: 23-Aug-2023


Kickstart Your Career

Get certified by completing the course

Get Started