# Program to split a set into equal sum sets where elements in first set are smaller than second 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 divide the list into two groups A and B such that: The sum of A and the sum of B are same. Here every number in A is strictly smaller than every number in B.

So, if the input is like nums = [3, 4, 5, 12], then the output will be True, as we can have A = [3,4,5] and B = [12] and both have sum 12.

To solve this, we will follow these steps −

• sort the list nums

• total := sum of all elements in nums

• s := 0, i := 0

• while i < size of nums, do

• n := nums[i]

• while i < size of nums and nums[i] is same as n, do

• s := s + nums[i]

• i := i + 1

• if s is same as total − s, then

• return True

• return False

Let us see the following implementation to get better understanding −

## Example

Live Demo

class Solution:
def solve(self, nums):
nums.sort()
total = sum(nums)
s = 0
i = 0
while i < len(nums):
n = nums[i]
while i < len(nums) and nums[i] == n:
s += nums[i]
i += 1
if s == total - s:
return True
return False
ob = Solution()
nums = [3, 4, 5, 12]
print(ob.solve(nums))

## Input

[3, 4, 5, 12]

## Output

True
Updated on 21-Oct-2020 11:00:47