Even Odd Sum Equilibrium — Problem Statement & Solution Guide
Problem Description
Given an array of integers values, determine the maximum possible sum of elements at even indices such that it equals the sum of elements at odd indices by selecting a subset of elements.
Examples
Input
[1, 2, 3, 4, 5]
Output
9
Explanation: Step-by-step: Given the array [1, 2, 3, 4, 5], we first separate the elements at even and odd indices. The elements at even indices are [2, 4] and the elements at odd indices are [1, 3, 5]. The maximum possible sum of elements at even indices that equals the sum of elements at odd indices is 9, which is the sum of elements at odd indices (1, 3, 5).
Input
[10, 20, 30, 40, 50]
Output
90
Explanation: Step-by-step: Given the array [10, 20, 30, 40, 50], we first separate the elements at even and odd indices. The elements at even indices are [10, 30, 50] and the elements at odd indices are [20, 40]. The maximum possible sum of elements at even indices that equals the sum of elements at odd indices is 90, which is the sum of elements at odd indices (20, 40, 30).
Constraints
- {"name":"weightRange","type":"numeric","min":1,"max":10000,"description":"Range of possible weights"}
- {"name":"arrayLength","type":"numeric","min":1,"max":100,"description":"Length of the input array"}
Optimal Approach & Strategy
The optimized approach involves using a prefix sum array to calculate the cumulative sums of weights on even and odd compartments, allowing for a more efficient calculation of the maximum allocatable weight with a time complexity of O(n).
Brute Force Approach
A brute-force approach involves trying all possible combinations of items on even and odd compartments and calculating the difference in weights, resulting in a time complexity of O(2^n). This approach is inefficient for large inputs.
Verified Code Solutions
function evenOddSumEquilibrium(values) { if (values.length === 0) return 0; let evenSum = 0, oddSum = 0; for (let i = 0; i < values.length; i++) { if (i % 2 === 0) evenSum += values[i]; else oddSum += values[i]; } let maxEvenSum = 0; let dp = new Array(values.length / 2 + 1).fill(0); dp[0] = 0; for (let i = 0; i < values.length; i += 2) { for (let j = values.length / 2; j >= 0; j--) { if (i + j <= values.length / 2) { dp[j] = Math.max(dp[j], dp[j - 1] + values[i]); } } } for (let i = 0; i <= values.length / 2; i++) { if (dp[i] === oddSum && dp[i] > maxEvenSum) maxEvenSum = dp[i]; } return maxEvenSum; }class Solution {
public int solution(int[] nums) {
int even_sum = 0;
int odd_sum = 0;
for (int i = 0; i < nums.length; i++) {
if (i % 2 == 0) {
even_sum += nums[i];
} else {
odd_sum += nums[i];
}
}
int max_even_sum = 0;
for (int i = 1; i < (1 << nums.length); i++) {
int current_sum = 0;
for (int j = 0; j < nums.length; j++) {
if ((i & (1 << j)) != 0) {
current_sum += nums[j];
}
}
if ((j % 2 == 0 && current_sum <= odd_sum) || (j % 2 != 0 && current_sum <= even_sum)) {
max_even_sum = Math.max(max_even_sum, current_sum);
}
}
return max_even_sum;
}
}def solution(nums):
even_sum = 0
odd_sum = 0
for i in range(len(nums)):
if i % 2 == 0:
even_sum += nums[i]
else:
odd_sum += nums[i]
max_even_sum = 0
for i in range(1 << len(nums)):
current_sum = 0
for j in range(len(nums)):
if (i & (1 << j)) != 0:
current_sum += nums[j]
if (j % 2 == 0 and current_sum <= odd_sum) or (j % 2 != 0 and current_sum <= even_sum):
max_even_sum = max(max_even_sum, current_sum)
return max_even_sumfunction evenOddSumEquilibrium(values) { if (values.length === 0) return 0; let evenSum = 0, oddSum = 0; for (let i = 0; i < values.length; i++) { if (i % 2 === 0) evenSum += values[i]; else oddSum += values[i]; } let maxEvenSum = 0; let dp = new Array(values.length / 2 + 1).fill(0); dp[0] = 0; for (let i = 0; i < values.length; i += 2) { for (let j = values.length / 2; j >= 0; j--) { if (i + j <= values.length / 2) { dp[j] = Math.max(dp[j], dp[j - 1] + values[i]); } } } for (let i = 0; i <= values.length / 2; i++) { if (dp[i] === oddSum && dp[i] > maxEvenSum) maxEvenSum = dp[i]; } return maxEvenSum; }Asked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.