Program to find number of elements can be removed to make odd and even indexed elements sum equal in Python

Given a list of numbers, we need to find how many elements can be removed such that the sum of even-indexed elements equals the sum of odd-indexed elements in the resulting array. This problem uses prefix sums to efficiently calculate sums after element removal.

Problem Understanding

For each element at index i, we simulate its removal and check if the remaining elements have equal even and odd index sums. After removing an element, all subsequent elements shift left by one position, changing their index parity.

Example

Given nums = [6, 8, 5, 2, 3] ?

  • Remove 8 (index 1): [6, 5, 2, 3] ? even sum = 6 + 2 = 8, odd sum = 5 + 3 = 8 ?
  • Remove 2 (index 3): [6, 8, 5, 3] ? even sum = 6 + 5 = 11, odd sum = 8 + 3 = 11 ?

Algorithm Steps

  • Create prefix sum arrays for even and odd indexed elements
  • For each possible removal position, calculate the new even and odd sums
  • Count positions where even sum equals odd sum after removal

Solution

def solve(nums):
    n = len(nums)
    # Create 2D prefix sum array: a[0] for even indices, a[1] for odd indices
    a = [[0] * (n + 1), [0] * (n + 1)]
    
    # Build prefix sums
    for i, x in enumerate(nums):
        a[0][i + 1] = a[0][i]
        a[1][i + 1] = a[1][i]
        a[i % 2][i + 1] += x

    count = 0
    total_sum = sum(nums)
    
    # Check each possible removal
    for i in range(n):
        # Calculate new even sum after removing element at index i
        even_sum = a[0][i] - a[0][0] + a[1][n] - a[1][i + 1]
        
        # Check if even sum * 2 equals remaining total
        if even_sum * 2 == total_sum - nums[i]:
            count += 1
    
    return count

# Test the function
nums = [6, 8, 5, 2, 3]
result = solve(nums)
print(f"Number of removable elements: {result}")
Number of removable elements: 2

How It Works

The key insight is that when we remove an element at index i, all elements after position i shift left. This means:

  • Elements at even indices before i remain at even indices
  • Elements at odd indices after i become even-indexed
  • We use prefix sums to efficiently calculate these new sums

Step-by-Step Trace

For nums = [6, 8, 5, 2, 3] ?

# Prefix sums:
# a[0] (even): [0, 6, 6, 11, 11, 11]  # 6 at index 0, 5 at index 2
# a[1] (odd):  [0, 0, 8, 8, 10, 13]   # 8 at index 1, 2 at index 3, 3 at index 4

# Remove element at index 1 (value 8):
# New even sum = 6 + (13 - 8) = 6 + 5 - 3 = 8
# New odd sum = 0 + (11 - 11) = 8
# Equal sums! Count++

Time Complexity

The algorithm runs in O(n) time with O(n) space, making it efficient for processing the prefix sums and checking each removal position once.

Conclusion

This solution uses prefix sums to efficiently simulate element removal without actually modifying the array. The key insight is calculating how index shifts affect even/odd positioning after removal.

Updated on: 2026-03-26T17:09:24+05:30

744 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements