Partition an array into two sub-arrays with equal sum.
MediumGiven an array of integers, determine if it can be partitioned into two subsets such that the sum of the elements in each subset is equal.
A partition is considered valid if it divides the array into two subsets where both subsets have the same sum.
Examples
Input: [2, 4, 11, 5] Output: True Explanation: The array can be partitioned into two subsets [2, 4, 5] and [11] with equal sum. Input: [2, 7, 3, 1] Output: False Explanation: The array cannot be partitioned into two subsets with equal sums.
Our solution processes the input array by first calculating the total sum of the array elements and then determining if it can be partitioned into two subsets with equal sums. The core idea is based on dynamic programming.
-
Calculate Total Sum: We start by computing the total sum of the array elements. If the total sum is odd, it is immediately impossible to partition it into two subsets with equal sums, so we return
false. -
Define Target Sum: If the total sum is even, we calculate the target sum for each subset, which is half of the total sum.
-
Dynamic Programming Array: We use a boolean array
dpwheredp[i]indicates whether a subset sum ofiis achievable. We initializedp[0]totruebecause a subset sum of0is always achievable (by taking no elements). -
Iterate Through the Array: For each number in the array, we update the
dparray in reverse order (fromtargetdown to the current number). This reverse iteration ensures that each number is only used once in the current update.- If a subset sum
i - numwas achievable, then adding the current numbernummakesdp[i]achievable. We updatedp[i]totrueaccordingly.
- If a subset sum
-
Check Final Result: After processing all numbers, if
dp[target]istrue, it means there is a subset with the required sum equal totarget, indicating that the array can be partitioned into two subsets with equal sums. Otherwise, we returnfalse.
The use of a boolean array ensures that we efficiently keep track of achievable subset sums.
Time Complexity: The solution has a time complexity of O(n * target), where n is the number of elements in the array and target is half of the total sum. This is because we iterate through the array and update the dp array for each element.
Space Complexity: The solution uses O(target) space for the dp array, where target is half of the total sum. This space is needed to store the achievable subset sums.
Interview experiences
2 sharedRelated questions
Split an array into equal sum subarraysFind a triplet in an array with a given sum.Print all sub-arrays of an array with distinct elements.Related courses






