- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

Suppose we have a list of numbers called nums. Now consider a function say f(i) which deletes the element at index i and then returns true or false, depending the resulting list's sum of even index values is same as the sum of odd index values or not. So we need the number of indexes for which f would return true.

So, if the input is like nums = [6, 8, 5, 2, 3], then the output will be 2 becuase, if we remove 8, the array will be [6, 5, 2, 3], the odd and even index elements sum is 8, so they are same. Another possible solution is, if we remove 2, the array will be [6, 8, 5, 3], here odd and even indexed elements sum is 11, so they are same.

To solve this, we will follow these steps −

- n := size of nums
- a := a 2d list of order 2 x (n+1) and fill each with 0s
- for each index i and value x nums, do
- a[0, i + 1] := a[0, i]
- a[1, i + 1] := a[1, i]
- a[i mod 2, i + 1] := a[i mod 2, i + 1] + x

- c := 0
- s := sum of all elements present in nums
- for i in range 0 to n - 1, do
- e := a[0, i] - a[0, 0] + a[1, n] - a[1, i + 1]
- if e * 2 is same as s - nums[i], then
- c := c + 1

- return c

## Example

Let us see the following implementation to get better understanding −

def solve(nums): n = len(nums) a = [[0] * (n + 1), [0] * (n + 1)] 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 c = 0 s = sum(nums) for i in range(n): e = a[0][i] - a[0][0] + a[1][n] - a[1][i + 1] if e * 2 == s - nums[i]: c += 1 return c nums = [6, 8, 5, 2, 3] print(solve(nums))

## Input

[6, 8, 5, 2, 3]

## Output

2

- Related Questions & Answers
- JavaScript Sum odd indexed and even indexed elements separately and return their absolute difference
- Program to find only even indexed elements from list in Python
- Absolute Difference of even and odd indexed elements in an Array (C++)?
- Minimum number of elements to be removed to make XOR maximum using C++.
- Absolute Difference of even and odd indexed elements in an Array in C++?
- Program to find sum of odd elements from list in Python
- Swap Even Index Elements And Odd Index Elements in Python
- Minimum number of elements that should be removed to make the array good using C++.
- Total possible ways of making sum of odd even indices elements equal in array in JavaScript
- Number of operations required to make all array elements Equal in Python
- Python program to split the even and odd elements into two different lists.
- Count number of even and odd elements in an array in C++
- Sorting odd and even elements separately JavaScript
- Python program to find sum of elements in list
- Program to find sum of unique elements in Python