5 Best Ways to Check if an Array of 1s and 2s Can Be Divided into Two Parts with Equal Sum in Python

Rate this post

πŸ’‘ Problem Formulation: We encounter situations in programming where we need to determine whether a given array of integers can be split into two subarrays such that the sum of the elements in both subarrays is equal. Specifically, when the array contains only 1s and 2s, we need to check the possibility of partitioning the array into two parts with equal sum. For example, given an array [1, 1, 2, 2], it should return True since it can be partitioned into [1, 2] and [1, 2] with equal sums of 3.

Method 1: Recursive Backtracking

This method employs a recurive backtracking approach that tries to find two subarrays with equal sums by exploring different combinations. The recursion considers each element and decides whether to include it in the first or second subarray, ensuring their sums are tracked and compared for equality.

Here’s an example:

def can_partition(arr, sum1=0, sum2=0, index=0):
    if index == len(arr):
        return sum1 == sum2
    return (can_partition(arr, sum1 + arr[index], sum2, index + 1) or
            can_partition(arr, sum1, sum2 + arr[index], index + 1))
print(can_partition([1, 1, 2, 2]))


This recursive code snippet checks whether the two halves of the array can have an equal sum starting from the first element. It takes each element and recursively places it in either of the two subarrays until all elements are placed. If at any point the sums are equal, it returns True; otherwise, if the end of the array is reached without equal sums, it returns False.

Method 2: Dynamic Programming

Dynamic Programming is an optimization technique used to solve recursive problems more efficiently by storing the results of subproblems. The idea is to use a boolean DP array to check whether a certain sum can be achieved with subsets of elements in the array.

Here’s an example:

def can_partition_dp(arr):
    total_sum = sum(arr)
    if total_sum % 2 != 0:
        return False
    half_sum = total_sum // 2
    dp = [False] * (half_sum + 1)
    dp[0] = True
    for num in arr:
        for i in range(half_sum, num - 1, -1):
            dp[i] |= dp[i - num]
    return dp[half_sum]

print(can_partition_dp([1, 1, 2, 2]))


In this snippet, we first check if the total sum is even; if not, we cannot possibly divide the array into two equal parts. We then create a DP array to store whether certain sums are achievable up to half of the total sum. If the half sum is achievable upon completion, the array can be partitioned into two parts with equal sum.

Method 3: Greedy Approach

The greedy method sorts the array and tries to balance two subarrays by always putting the next biggest number in the subarray with the smaller sum. While this approach might work on this specific problem, it may not always yield the correct result for a generalized version of equal sum partitioning problems.

Here’s an example:

def can_partition_greedy(arr):
    sum1, sum2 = 0, 0
    for num in arr:
        if sum1 < sum2:
            sum1 += num
            sum2 += num
    return sum1 == sum2

print(can_partition_greedy([1, 2, 2, 1]))


This greedy algorithm codes for the simplest logicβ€”sort the array and keep adding the next largest number to the subarray which has the lesser sum. Once all elements are chosen, we check if the sums are equal.

Method 4: Bit Manipulation

Bit manipulation provides low-level, high-performing operations. By expressing the sum in binary, we check if the number of 1s and 2s can form a sum such that each bit position has an even count, which inherently splits the numbers into two equal sums.

Here’s an example:

def can_partition_bitwise(arr):
    bits = 0
    for num in arr:
        bits ^= 1 << num
    return bits == 0 or bits == 1 << max(arr)

print(can_partition_bitwise([1, 1, 2, 2]))


This snippet utilizes bitwise operations to maintain a running exclusive OR (XOR) of bits. Essentially, for an array with equal sums of 1s and 2s, we will either get 0 or a value where the bit for the maximum number (2) is set.

Bonus One-Liner Method 5: Pythonic Approach

Python provides extensive built-in functions and concise syntax, enabling the creation of a one-liner solution using list comprehensions along with language-specific utilities like the any() function.

Here’s an example:

can_partition_one_liner = lambda arr, s=sum(arr): all(s % 2) or any(s/2 == sum(arr[:i]) for i in range(len(arr)))

print(can_partition_one_liner([1, 1, 2, 2]))


This elegant pythonic one-liner first checks if the sum of the array is odd; if so, it returns False since it cannot be divided into two equal sums. If the sum is even, it utilizes any() to determine if there exists any index where the sum of elements up to that index equals half of the total sum.


  • Method 1: Recursive Backtracking. This method is robust and straightforward but can be inefficient for large arrays due to its exponential time complexity.
  • Method 2: Dynamic Programming. An optimized bottom-up method that uses a DP table to solve the problem in polynomial time. Complex to understand, but very efficient for larger datasets.
  • Method 3: Greedy Approach. It’s a simple and fast method but lacks the guarantee of the correct solution for a broader range of problems beyond this specific case.
  • Method 4: Bit Manipulation. Very fast and uses low-level operations, but can be less intuitive and requires understanding of bitwise operations.
  • Method 5: Pythonic Approach. Elegant and compact, leverages Python’s rich features for a clean one-liner code. However, lacks the explicit clarity which may be necessary for understanding underlying mechanics, especially for beginners.