- 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 ways to make a fair array in Python

Suppose we have an array called nums. We can select exactly one index and remove the element from that index. (The index of the elements may change after the removal). We can say an array is fair when the sum of the odd-indexed values equals the sum of the even-indexed values. We have to find the number of indices that we could select such that after the removal, nums is fair.

So, if the input is like nums = [5,3,7,2], then the output will be

Remove from index 0, array is [3,7,2], even position sum: 3+2 = 5, odd position sum 7 (Not fair)

Remove from index 1, array is [5,7,2], even position sum: 5+2 = 7, odd position sum 7 (fair)

Remove from index 2, array is [5,3,2], even position sum: 5+2 = 7, odd position sum 3 (Not fair)

Remove from index 3, array is [5,3,7], even position sum: 5+7 = 12, odd position sum 3 (Not fair)

To solve this, we will follow these steps −

- res := 0, sm1 := 0, sm2 := 0
- for i in range 1 to size of nums - 1, do
- if i is even, then
- sm1 := sm1 + nums[i]

- otherwise,
- sm2 := sm2 + nums[i]

- if i is even, then
- if sm1 is same as sm2, then
- res := res + 1

- for i in range 1 to size of nums - 1, do
- if i is odd, then
- sm1 := sm1 - nums[i] + nums[i-1]

- otherwise,
- sm2 := sm2 - nums[i] + nums[i-1]

- if sm1 is same as sm2, then
- res := res + 1

- if i is odd, then
- return res

## Example

Let us see the following implementation to get better understanding −

def solve(nums): res, sm1, sm2 = 0, 0, 0 for i in range(1, len(nums)): if i%2 == 1: sm1 += nums[i] else: sm2 += nums[i] if sm1 == sm2: res += 1 for i in range(1, len(nums)): if i % 2 == 1: sm1 = sm1 - nums[i] + nums[i-1] else: sm2 = sm2 - nums[i] + nums[i-1] if sm1 == sm2: res += 1 return res nums = [5,3,7,2] print(solve(nums))

## Input

[5,3,7,2]

## Output

1

- Related Questions & Answers
- Program to find minimum moves to make array complementary in Python
- Program to find max chunks to make array sorted in Python
- Program to find number of ways we can concatenate words to make palindromes in Python
- Program to find minimum operations to make array equal using Python
- Program to find number of ways to split array into three subarrays in Python
- Program to find minimum operations to make the array increasing using Python
- Program to find number of ways to split a string in Python
- Program to find shortest subarray to be removed to make array sorted in Python
- Program to find number of good ways to split a string using Python
- Program to find minimum numbers of function calls to make target array using Python
- Fair Candy Swap in Python
- Program to find number of ways we can decode a message in Python
- Program to find number of ways we can split a palindrome in python
- Program to find minimum deletions to make strings strings in Python
- Program to find minimum deletions to make string balanced in Python