Alternating sum of elements of a two-dimensional array using JavaScript

Problem

We are required to write a JavaScript function that takes in a 2-Dimensional array of m X n order of numbers. For this array, our function should calculate the alternating sum where each element is multiplied by (-1)^(i+j), where i and j are the row and column indices respectively.

The mathematical formula is: $\sum_{i=1}^m \sum_{j=1}^n (-1)^{i+j}a_{ij}$

How It Works

The alternating sum works by applying a positive or negative sign to each element based on its position. When the sum of row index (i) and column index (j) is even, the element gets a positive sign. When the sum is odd, the element gets a negative sign.

Example

Let's see how this works with a 3x3 array:

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

const alternateSum = (arr = []) => {
    let sum = 0;
    for(let i = 0; i < arr.length; i++){
        for(let j = 0; j < arr[i].length; j++){
            const multiplier = (i + j) % 2 === 0 ? 1 : -1;
            sum += (multiplier * arr[i][j]);
        };
    };
    return sum;
};

console.log("Array:");
console.log(arr);
console.log("Alternating sum:", alternateSum(arr));

Output

Array:
[ [ 4, 6, 3 ], [ 1, 8, 7 ], [ 2, 5, 9 ] ]
Alternating sum: 7

Step-by-Step Calculation

For the above array, here's how the calculation works:

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

console.log("Position signs pattern:");
console.log("+ - +");
console.log("- + -");
console.log("+ - +");
console.log();

// Calculate step by step
let sum = 0;
for(let i = 0; i < arr.length; i++){
    for(let j = 0; j < arr[i].length; j++){
        const multiplier = (i + j) % 2 === 0 ? 1 : -1;
        const contribution = multiplier * arr[i][j];
        sum += contribution;
        console.log(`Position (${i},${j}): ${arr[i][j]} × ${multiplier} = ${contribution}`);
    }
}
console.log(`Total sum: ${sum}`);

Output

Position signs pattern:
+ - +
- + -
+ - +

Position (0,0): 4 × 1 = 4
Position (0,1): 6 × -1 = -6
Position (0,2): 3 × 1 = 3
Position (1,0): 1 × -1 = -1
Position (1,1): 8 × 1 = 8
Position (1,2): 7 × -1 = -7
Position (2,0): 2 × 1 = 2
Position (2,1): 5 × -1 = -5
Position (2,2): 9 × 1 = 9
Total sum: 7

Alternative Implementation

Here's a more concise version using array methods:

const alternateSum = (arr = []) => {
    return arr.reduce((total, row, i) => {
        return total + row.reduce((rowSum, element, j) => {
            const multiplier = (i + j) % 2 === 0 ? 1 : -1;
            return rowSum + (multiplier * element);
        }, 0);
    }, 0);
};

const testArray = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

console.log("Test array:", testArray);
console.log("Alternating sum:", alternateSum(testArray));

Output

Test array: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
Alternating sum: 5

Conclusion

The alternating sum of a 2D array applies a checkerboard pattern of positive and negative signs to each element. This technique is useful in mathematical computations and matrix operations where alternating signs are required.

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

410 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements