Total possible ways of making sum of odd even indices elements equal in array in nJavaScript

We need to write a JavaScript function that counts how many elements can be removed from an array such that after removal, the sum of elements at odd indices equals the sum of elements at even indices.

The approach involves calculating cumulative sums for odd and even positioned elements, then checking each removal scenario mathematically without actually removing elements.

Problem Understanding

Given an array, we want to find how many single element removals result in equal odd and even index sums in the remaining array.

For example, with array [2, 6, 4, 2]:

  • Remove index 1 (value 6): [2, 4, 2] ? odd sum = 4, even sum = 4 ?
  • Remove index 3 (value 2): [2, 6, 4] ? odd sum = 6, even sum = 6 ?

Algorithm Approach

Instead of removing elements and recalculating sums, we use cumulative sums and mathematical formulas to check each removal scenario efficiently.

Implementation

const arr = [2, 6, 4, 2];

const possibleWays = (arr = []) => {
    const sum = new Array(arr.length);
    let res = 0;
    let oddSum = 0;
    let evenSum = 0;
    
    // Calculate cumulative sums for odd and even indices
    for (let i = 0; i < arr.length; ++i) {
        if (i % 2 === 0) {
            sum[i] = (evenSum += arr[i]);
        } else {
            sum[i] = (oddSum += arr[i]);
        }
    }
    
    // Check each possible removal
    for (let i = 0; i < arr.length; ++i) {
        if (i % 2 === 0) {
            // Removing even index element
            if (2 * sum[i] - arr[i] + oddSum === 2 * (sum[i - 1] || 0) + evenSum) {
                ++res;
            }
        } else {
            // Removing odd index element  
            if (2 * sum[i] - arr[i] + evenSum === 2 * (sum[i - 1] || 0) + oddSum) {
                ++res;
            }
        }
    }
    
    return res;
};

console.log(possibleWays(arr));
2

How It Works

The algorithm works in two phases:

  1. Calculate cumulative sums: Track running totals for elements at even and odd indices separately
  2. Check removal scenarios: For each element, calculate what the sums would be after removal using mathematical formulas instead of array manipulation

The mathematical check avoids the need to create new arrays for each removal scenario, making the solution efficient with O(n) time complexity.

Step-by-Step Example

const arr = [2, 6, 4, 2];

// Step 1: Calculate what happens when we remove each element
console.log("Original array:", arr);

// Remove element at index 0 (value 2): [6, 4, 2]
console.log("Remove index 0:", "oddSum =", 6, "evenSum =", 6, "Equal?", 6 === 6);

// Remove element at index 1 (value 6): [2, 4, 2] 
console.log("Remove index 1:", "oddSum =", 4, "evenSum =", 4, "Equal?", 4 === 4);

// Remove element at index 2 (value 4): [2, 6, 2]
console.log("Remove index 2:", "oddSum =", 6, "evenSum =", 4, "Equal?", 6 === 4);

// Remove element at index 3 (value 2): [2, 6, 4]
console.log("Remove index 3:", "oddSum =", 6, "evenSum =", 6, "Equal?", 6 === 6);
Original array: [ 2, 6, 4, 2 ]
Remove index 0: oddSum = 6 evenSum = 6 Equal? true
Remove index 1: oddSum = 4 evenSum = 4 Equal? true
Remove index 2: oddSum = 6 evenSum = 4 Equal? false
Remove index 3: oddSum = 6 evenSum = 6 Equal? true

Conclusion

This solution efficiently counts possible ways to achieve equal odd/even sums by using cumulative sum arrays and mathematical formulas. The approach avoids costly array manipulations while maintaining O(n) time complexity.

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

337 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements