Given an integer array nums, return true if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or false otherwise.
Example 1:
Input: nums = [1,5,11,5] Output: true Explanation: The array can be partitioned as [1, 5, 5] and [11].
Example 2:
Input: nums = [1,2,3,5] Output: false Explanation: The array cannot be partitioned into equal sum subsets.
Constraints:
1 <= nums.length <= 2001 <= nums[i] <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are given a collection of items with different weights and you want to know if you can divide them into two groups that weigh exactly the same. The brute force strategy involves testing every single possible way to split the items into two piles to see if any split results in equal weights.
Here's how the algorithm would work step-by-step:
def can_partition(nums): total_weight = sum(nums) # An odd total weight cannot be split into two equal integer halves if total_weight % 2 != 0: return False target_weight = total_weight // 2 def explore_combinations(current_index, current_sum): # A match means we successfully found a valid subset equaling our target if current_sum == target_weight: return True # Exceeding the target or running out of elements terminates this path if current_sum > target_weight or current_index >= len(nums): return False include_current = explore_combinations(current_index + 1, current_sum + nums[current_index]) exclude_current = explore_combinations(current_index + 1, current_sum) return include_current or exclude_current return explore_combinations(0, 0)Instead of checking every possible combination of numbers, the optimal approach focuses on finding out if we can collect a specific target sum. We figure out this target sum by adding all the numbers together and seeing if the total can be split evenly in half.
Here's how the algorithm would work step-by-step:
def can_partition(number_collection):
grand_total = sum(number_collection)
# Odd totals can never be split into two equal integer subsets.
if grand_total % 2 != 0:
return False
target_sum = grand_total // 2
achievable_scores = {0}
# Iteratively update achievable sums with each number in the collection.
for current_number in number_collection:
new_scores = set()
for existing_score in achievable_scores:
new_score = existing_score + current_number
if new_score <= target_sum:
new_scores.add(new_score)
achievable_scores.update(new_scores)
# Check if the exact half-sum was successfully built.
if target_sum in achievable_scores:
return True
return False| Case | How to Handle |
|---|---|
| Array with total sum that is an odd number | Return false immediately since an odd sum cannot be divided into two equal integer subsets. |
| Array with a single element | Return false because a single element cannot be partitioned into two subsets. |
| Array containing negative numbers | Standard subset sum DP table assumes positive integers and requires value shifting or constraints validation to handle negative bounds. |
| Array containing zeros | Zeros do not change the total sum and are handled naturally by the dynamic programming subset transition logic. |
| Large array with very large maximum values and sum | Use a bitset or boolean array optimization to maintain memory efficiency and prevent memory limit exceed errors. |
| Elements larger than the target subset sum | Filter or skip any element greater than half of the total sum during the dynamic programming iteration. |
| All elements are identical | Check if the count of elements and their values allow an even split of the total sum. |