# Program to check whether we can make group of two partitions with equal sum or not in Python?

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Suppose we have a list of numbers called nums, we have to check whether we can partition nums into two groups where the sum of the elements in both groups are same.

So, if the input is like nums = [2, 3, 6, 5], then the output will be True, as we can make groups like: [2, 6] and [3, 5].

To solve this, we will follow these steps

• total := sum of all elements in nums

• if total is odd, then

• return False

• half := integer part of total / 2

• dp := a list of size half + 1 and fill with false

• dp := true

• for each num in nums, do

• for i in range half to 0, decrease by 1, do

• if i >= num, then

• dp[i] := dp[i] OR dp[i - num]

• return dp[half]

## Example

Live Demo

class Solution:
def solve(self, nums):

total = sum(nums)

if total & 1:
return False

half = total // 2

dp = [True] + [False] * half
for num in nums:
for i in range(half, 0, -1):
if i >= num:
dp[i] |= dp[i - num]

return dp[half]

ob = Solution()
nums = [2, 3, 6, 5]
print(ob.solve(nums))

## Input

[2, 3, 6, 5]

## Output

True