BackmediumArrays Infosys Swiggy

Balance Array Partition Solution

Problem Statement

Given an array of integers weights, determine the maximum weight that can be allocated such that the sum of weights at even indices equals the sum of weights at odd indices.

Example 1
Input
[1, 2, 3, 4]
Output
0

Explanation: Step-by-step: Given array [1, 2, 3, 4], the sum of weights at even indices (2 + 4 = 6) is greater than the sum of weights at odd indices (1 + 3 = 4). Since we cannot allocate any weight to even indices, the maximum weight that can be allocated is 0.

Example 2
Input
[20, 10, 30, 40, 50]
Output
60

Explanation: Step-by-step: Given array [20, 10, 30, 40, 50], the sum of weights at even indices (10 + 30 + 50 = 90) is greater than the sum of weights at odd indices (20 + 40 = 60). However, we can only allocate the sum of weights at odd indices to even indices. Therefore, the maximum weight that can be allocated is 60.

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 Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free