Determining whether numbers form additive sequence in JavaScript

This problem asks us to determine if the given numbers in an array form an additive sequence. An additive sequence is one where each number (starting from the third) equals the sum of the two preceding numbers.

Understanding Additive Sequences

An additive sequence must have at least three numbers. Each consecutive number in the series, except the first two, must equal the sum of the two numbers before it.

// Example: 112358 as a string of digits
// 1 + 1 = 2
// 1 + 2 = 3  
// 2 + 3 = 5
// 3 + 5 = 8
// Result: true (forms additive sequence)

Algorithm

Step 1 ? Create a function existAdditiveSequence() that accepts an integer array and returns true if it forms an additive sequence.

Step 2 ? Loop through the array starting from index 2 (third element).

Step 3 ? For each element, check if it equals the sum of the two previous elements. If not, return false.

Step 4 ? If all elements pass the test, return true.

Implementation

// Function to check if array forms additive sequence
function existAdditiveSequence(arr) {
    // Need at least 3 elements for additive sequence
    if (arr.length < 3) {
        return false;
    }
    
    // Check each element from index 2 onwards
    for (let i = 2; i < arr.length; i++) {
        if (arr[i] !== arr[i-1] + arr[i-2]) {
            return false;
        }
    }
    return true;
}

// Test cases
const sequence1 = [1, 1, 2, 3, 5, 8];        // Fibonacci sequence
const sequence2 = [1, 3, 4, 7, 11, 18];      // Another additive sequence
const sequence3 = [1, 2, 4, 7, 11, 18];      // Not additive (1+2?4)
const sequence4 = [1, 1, 2, 5, 7, 12];       // Not additive (1+2?5)

console.log("Sequence 1:", existAdditiveSequence(sequence1));
console.log("Sequence 2:", existAdditiveSequence(sequence2)); 
console.log("Sequence 3:", existAdditiveSequence(sequence3)); 
console.log("Sequence 4:", existAdditiveSequence(sequence4));
Sequence 1: true
Sequence 2: true
Sequence 3: false
Sequence 4: false

Example Analysis

Sequence 1 [1, 1, 2, 3, 5, 8]: This is the Fibonacci sequence where 1+1=2, 1+2=3, 2+3=5, 3+5=8. Result: true.

Sequence 2 [1, 3, 4, 7, 11, 18]: Checking: 1+3=4, 3+4=7, 4+7=11, 7+11=18. Result: true.

Sequence 3 [1, 2, 4, 7, 11, 18]: The sum 1+2=3, but the third element is 4, not 3. Result: false.

Sequence 4 [1, 1, 2, 5, 7, 12]: The sum 1+2=3, but the fourth element is 5, not 3. Result: false.

Enhanced Version with Detailed Checking

function checkAdditiveSequence(arr) {
    if (arr.length < 3) {
        console.log("Array too short for additive sequence");
        return false;
    }
    
    console.log(`Checking array: [${arr.join(', ')}]`);
    
    for (let i = 2; i < arr.length; i++) {
        const sum = arr[i-2] + arr[i-1];
        const current = arr[i];
        
        console.log(`Position ${i}: ${arr[i-2]} + ${arr[i-1]} = ${sum}, actual: ${current}`);
        
        if (sum !== current) {
            console.log("Not an additive sequence");
            return false;
        }
    }
    
    console.log("Forms an additive sequence!");
    return true;
}

// Test with detailed output
checkAdditiveSequence([2, 3, 5, 8, 13]);
Checking array: [2, 3, 5, 8, 13]
Position 2: 2 + 3 = 5, actual: 5
Position 3: 3 + 5 = 8, actual: 8
Position 4: 5 + 8 = 13, actual: 13
Forms an additive sequence!

Time and Space Complexity

Time Complexity: O(n) where n is the length of the input array. The function iterates through the array once.

Space Complexity: O(1) as we only use a constant amount of extra space for variables.

Conclusion

The additive sequence checker provides an efficient way to validate whether numbers follow the pattern where each element equals the sum of its two predecessors. This concept is fundamental in mathematical sequences like Fibonacci numbers.

Updated on: 2026-03-15T23:19:00+05:30

379 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements