Prepin
Log in

M

Divide an array into two sub-arrays that have equal sums.

Approach / Explanation

This is an algorithmic challenge where you are required to partition an array into two subarrays such that the sums of the elements in both subarrays are equal. Analyze the Problem: You are provided with an array and must divide it into two subarrays where the sums of subarray1 and subarray2 are identical. Essential considerations to address: Determine a method to split the array into two sections with equal sums. Clearly describe how you plan to implement this in your selected programming language. Verify whether the total sum of the array is odd, as it cannot be evenly divided into two equal sums in that case. Monitor cumulative sums and look for a value that indicates half of the total sum.

Suggested Answer

A potential method to tackle this issue is to employ a straightforward iterative technique. Initially, we will calculate the total sum of the array. Next, we will loop through each element of the array while maintaining a cumulative total. The moment our cumulative total reaches half of the total sum, we identify the partition point for creating two subarrays with equal sums. Below is a Python code example: def equal_sum_partition ( arr ) : if sum ( arr ) % 2 != 0 : return "It's not feasible to split the array into two parts with equal sum since the total sum is odd." total_sum = sum ( arr ) running_sum = 0 for i in range ( len ( arr ) ) : running_sum += arr [ i ] if running_sum == total_sum / 2 : return arr [ : i + 1 ] , arr [ i + 1 : ] equal_sum_partition ( [ 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 ] ) # Output: ([1, 2, 3, 4], [1, 2, 3, 4])

Alternative Answer

Another method utilizes prefix sums and hashing. This technique begins with calculating the prefix sum, which is then stored along with the indices in a dictionary. If the overall sum of the array is even, we look for half of that sum in the dictionary. Here is a Python code example: def equal_sum_partition ( arr ) : total_sum = sum ( arr ) if total_sum % 2 != 0 : return "It's not possible to divide the array into two parts with equal sum as the total sum is odd" prefix_sum = 0 sum_dict = { } for i in range ( len ( arr ) ) : prefix_sum += arr [ i ] sum_dict [ prefix_sum ] = i if total_sum / 2 in sum_dict : partition_index = sum_dict [ total_sum / 2 ] return arr [ : partition_index + 1 ] , arr [ partition_index + 1 : ] equal_sum_partition ( [ 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 ] ) # Output: ([1, 2, 3, 4], [1, 2, 3, 4]) In this code, a dictionary is employed to keep track of the prefix sum at every step along with the corresponding index. We then verify if half of the overall sum is present in the dictionary. If it is, we retrieve the index from the dictionary and divide the array at that point.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Apple
Meta (Facebook)

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.