BackmediumArraysFlipkart

Even Odd Sum Equilibrium Solution

Problem Statement

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.

Example 1
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).

Example 2
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"}
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Even Odd Sum Equilibrium — Problem Statement & Solution Guide

ArraysMediumPrefix sum and observation
TimeO(n)
|
SpaceO(n)

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

Example 1

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).

Example 2

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

JavaScript Solution
Time: O(n)
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; }

Asked in Top Tech Interviews

Flipkart

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.