Counting possible APs within an array in JavaScript

Arithmetic Progression (AP) is a sequence of numbers where the difference between any two consecutive numbers is constant (called the common difference).

For instance, 1, 2, 3, 4, 5, 6,? is an AP with a common difference of 1 (2 - 1 = 1).

Problem Statement

We need to write a JavaScript function that takes a sorted array of integers and returns the count of arithmetic progressions of size 3 that can be formed from the array elements.

In each progression, the differences between consecutive elements must be the same. The input array is guaranteed to be sorted in increasing order.

Example Input and Output

Input

const arr = [1, 2, 3, 5, 7, 9];

Expected Output

5

Explanation: The five arithmetic progressions of size 3 are:

[1, 2, 3], [1, 3, 5], [1, 5, 9], [3, 5, 7], [5, 7, 9]

Algorithm Approach

For three numbers to form an AP, the middle element must be the average of the first and third elements. We use this property to check if three elements form an AP.

Implementation

const arr = [1, 2, 3, 5, 7, 9];

const countAP = (arr = []) => {
    let i, j, k;
    let { length: len } = arr;
    let count = 0;
    
    for (i = 0; i < len - 2; i++) {
        for (k = i + 2; k < len; k++) {
            let temp = arr[i] + arr[k];
            let div = temp / 2;
            
            // Check if middle element exists and is integer
            if ((div * 2) == temp) {
                for (j = i + 1; j < k; j++) {
                    if (arr[j] == div) {
                        count += 1;
                    }
                }
            }
        }
    }
    return count;
};

console.log(countAP(arr));
5

How It Works

1. We use three nested loops to check all possible combinations of three elements

2. For elements at positions i and k, we calculate the required middle element as (arr[i] + arr[k]) / 2

3. We search for this middle element between positions i and k

4. If found, we increment our count

Alternative Optimized Approach

const countAPOptimized = (arr = []) => {
    let count = 0;
    const len = arr.length;
    
    for (let j = 1; j < len - 1; j++) {
        let i = j - 1;
        let k = j + 1;
        
        while (i >= 0 && k < len) {
            if (arr[i] + arr[k] === 2 * arr[j]) {
                count++;
                i--;
                k++;
            } else if (arr[i] + arr[k] < 2 * arr[j]) {
                k++;
            } else {
                i--;
            }
        }
    }
    return count;
};

const testArr = [1, 2, 3, 5, 7, 9];
console.log(countAPOptimized(testArr));
5

Comparison

Approach Time Complexity Space Complexity
Triple Loop O(n³) O(1)
Two Pointer O(n²) O(1)

Conclusion

Both approaches solve the problem of counting arithmetic progressions of size 3. The optimized two-pointer method is more efficient with O(n²) time complexity compared to the O(n³) triple loop approach.

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

212 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements