How to get almost increasing sequence of integers in JavaScript ?


In the given problem statement we have to get an almost increasing sequence of integers with the help of Javascript functionalities. So we will use some basic functionalities of mathematics and Javascript.

Understanding the problem

The problem at hand is to generate an almost increasing series of integers in Javascript. So the almost increasing sequence is a sequence in which every item is greater than or equal to the previous item. In other terms we will say it allows for a single item in the sequence where an item is smaller than the previous item. So the resultant array should be stored in an array.

Logic for the given problem

To solve this problem we will define a function with the help of it we will generate an almost increasing sequence of integers. The function will take the desired length of the series as a parameter. In this function we will begin with generating the first number of the series randomly with the help of Math.random and scale this series based on the specified range. After getting the first number we will proceed to generate the remaining numbers in the series. This task will be done by adding the random number between 1 and 10 to the previous number in the series.

Algorithm

Step 1: As we have to generate an almost increasing sequence of integers so first we will define a function called generateSequence. This function will take a parameter of desired length for the sequence as input.

Step 2: After declaring the function we will define a blank array for storing the sequence and name it as sequence.

Step 3: Then we will start generating the first number randomly with the help of Math.random() and scale it if needed.

Step 4: Now in this step we will generate the rest of the sequence by adding the random number between 1 to 10 to the previous number in the sequence.

Step 5: After getting all the numbers we need to push the numbers in the sequence array.

Step 6: And the resulting sequence will be stored in an array and returned by the function.

Example

 //Function for generating the sequence
function generateSequence(len) {
   var sequence = [];

   // Randomly produce first number
   sequence.push(Math.floor(Math.random() * 100));

   // Produce the rest of the series
   for (var i = 1; i < len; i++) {
      var num = sequence[i - 1] + Math.floor(Math.random() * 10) + 1;
      sequence.push(num);
   }

   return sequence;
}

var len = 10; // length of the sequence
var sequence = generateSequence(len);
console.log(sequence);

Output

[
    63, 66, 72, 75, 78,
    84, 86, 87, 95, 99
]

Complexity

The time complexity for generating an almost increasing sequence of integers is O(n), in which n is the size of the sequence. Because the function traverses the numbers n times to generate each item of the sequence. And the space complexity of the function is also O(n) because it creates an array to store the sequence of length n.

Conclusion

The code we have implemented shows a simple approach to get an almost increasing sequence of integers in Javascript. The code produces the sequence by beginning with the random number and then adding an random value within a defined range for next items.

Updated on: 14-Aug-2023

285 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements