Determining whether numbers form additive sequence in JavaScript


This problem says to determine if the given numbers in the array are in the form of additive sequence or not. We will check, If any two adjacent numbers in a sequence add up to the next number in the sequence, we can confirm this using a straightforward procedure.

Understanding the problem

To understand the above mentioned problem we need to first understand the problem in depth. If the question is broken down into subproblems, it will be easy to solve it. Consider any important procedures or stages that must be followed in order to solve the issue.

So first we need to identify what are the additive numbers or sequences? An additive sequence must have three numbers or more. Each consecutive number in the series, with the exception of the first two numbers, must equal the sum of the two numbers before it.

Num = 112358

Since 2 = 1+1, 3 = 1 + 2, 5 = 2 + 3, and 8 = 3 + 5 —------> 112358

The result is true

To solve this problem, a sequence of numbers forms a compound sequence in JavaScript. We can use a simple algorithm to check whether the sum of any two adjacent numbers in the sequence is equal to the next number in the sequence. To test this problem we will use the existAdditiveSequence() function, you can call it with different input arrays and check the output.

Algorithm

Step 1 − Declare a function and give it a name 'existAdditiveSequence()', that accepts an integer array of elements as an argument. And It will return true if the number will form an additive sequence, and false if it is not.

Step 2 − Now forwarding, starting from the second index, which is the third number in the sequence, the first step will loop over all of the numbers in the sequence.

Step 3 − In following the second step, we shall verify that the sum of the previous consecutive numbers is identical to the current number for each number in the sequence. After that, the function returns false if it is not equal. Thus, it will show that there is no additive sequence of numbers. The function returns true, indicating that integers do form an additive sequence, if the loop ends without discovering any inconsistencies.

Step 4 − After the third step, If the above conditions are not satisfied then the result will be false.

Example 

//create a function with name and argument
function existAdditiveSequence(n) {
    for (let i = 2; i < n.length; i++) {
      if (n[i] !== n[i-1] + n[i-2]) {
        return false;
      }
    }
    return true;
  }

// Define sequence of array
const arr1 = [1, 1, 2, 3, 5, 8];
const arr2 = [1, 3, 4, 7, 11, 18];
const arr3 = [1, 2, 4, 7, 11, 18];
const arr4 = [1, 1, 2, 5, 7, 12];

// show result
console.log(existAdditiveSequence(arr1), "---> An additive sequence")
console.log(existAdditiveSequence(arr2), "---> An additive sequence"); 
console.log(existAdditiveSequence(arr3), "---> Not an additive sequence"); 
console.log(existAdditiveSequence(arr4), "---> Not an additive sequence");

Output

true ---> An additive sequence
true ---> An additive sequence
false ---> Not an additive sequence
false ---> Not an additive sequence

Array [1, 1, 2, 3, 5, 8] and array [1, 3, 4, 7, 11, 18], creates the additive Fibonacci sequence. So, the result is true.

Array [1, 2, 4, 7, 11, 18], does not create an additive sequence because the first two numbers' sum, 3, does not equal the third number, 4, as it should. The output will be false as a result.

Array [1, 1, 2, 5, 7, 12], It does not form an additive sequence since the 2nd and 3rd numbers added together equal 3, which is not the same as the fourth number 5. So, the output will be false as a result.

Time and Space Complexity

The existAdditiveSequence() function has an O(n) time complexity, where n is the length of the input array. This is due to the function iterating over the input array once and determining whether each number is the sum of the two before it.

The function has a constant additional memory requirement to keep the loop index as well as the current, previous, and previous− previous values, hence its space complexity is O(1).

Conclusion

The existAdditiveSequence() method is a useful tool for figuring out whether a group of numbers is an additive sequence.

Updated on: 23-Aug-2023

186 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements