Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
Problem Understanding
Let's understand with an example. If the input is nums = [5,3,7,2], let's check each removal ?
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)
So only removing index 1 makes the array fair, hence the answer is 1.
Algorithm Approach
To solve this efficiently, we will follow these steps ?
- Calculate initial sums for odd and even positions (excluding index 0)
- Check if removing index 0 makes array fair
- For each subsequent index, update sums dynamically and check fairness
- When an element is removed, elements after it shift left, changing their parity
Implementation
def solve(nums):
res, sm1, sm2 = 0, 0, 0
# Calculate initial sums for odd and even positions (starting from index 1)
for i in range(1, len(nums)):
if i % 2 == 1: # odd index
sm1 += nums[i]
else: # even index
sm2 += nums[i]
# Check if removing index 0 makes array fair
if sm1 == sm2:
res += 1
# Check removing each subsequent index
for i in range(1, len(nums)):
if i % 2 == 1: # removing odd index element
sm1 = sm1 - nums[i] + nums[i-1]
else: # removing even index element
sm2 = sm2 - nums[i] + nums[i-1]
if sm1 == sm2:
res += 1
return res
# Test the function
nums = [5, 3, 7, 2]
result = solve(nums)
print(f"Number of ways to make array fair: {result}")
Number of ways to make array fair: 1
How It Works
The algorithm works in two phases ?
-
Initial Setup: Calculate sums of elements at odd positions (
sm1) and even positions (sm2) starting from index 1 - Dynamic Updates: For each removal, update the sums by considering how elements shift positions after removal
When we remove element at index i, the element at i-1 takes its place in the sum calculation, which is why we subtract nums[i] and add nums[i-1].
Example with Different Input
# Test with another example
nums2 = [1, 2, 3, 4, 5]
result2 = solve(nums2)
print(f"Array: {nums2}")
print(f"Number of fair removals: {result2}")
# Let's verify manually for first few removals
print("\nManual verification:")
for i in range(len(nums2)):
temp = nums2[:i] + nums2[i+1:]
even_sum = sum(temp[j] for j in range(0, len(temp), 2))
odd_sum = sum(temp[j] for j in range(1, len(temp), 2))
print(f"Remove index {i}: {temp}, even_sum: {even_sum}, odd_sum: {odd_sum}, fair: {even_sum == odd_sum}")
Array: [1, 2, 3, 4, 5] Number of fair removals: 1 Manual verification: Remove index 0: [2, 3, 4, 5], even_sum: 6, odd_sum: 8, fair: False Remove index 1: [1, 3, 4, 5], even_sum: 5, odd_sum: 8, fair: False Remove index 2: [1, 2, 4, 5], even_sum: 5, odd_sum: 7, fair: False Remove index 3: [1, 2, 3, 5], even_sum: 4, odd_sum: 7, fair: False Remove index 4: [1, 2, 3, 4], even_sum: 4, odd_sum: 6, fair: False
Conclusion
This algorithm efficiently finds the number of indices whose removal makes an array fair by using dynamic sum updates instead of recalculating from scratch. The time complexity is O(n) and space complexity is O(1).
