- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to split a set into equal sum sets where elements in first set are smaller than second in Python

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

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

- Related Questions & Answers
- Program to find maximum sum of two sets where sums are equal in C++
- Program to find minimum length of first split of an array with smaller elements than other list in Python
- Count elements smaller than or equal to x in a sorted matrix in C++
- Python program to convert Set into Tuple and Tuple into Set
- Count numbers (smaller than or equal to N) with given digit sum in C++
- Set the first array elements raised to powers from second array element-wise in Numpy
- Program to count non-empty subsets where sum of min and max element of set is less than k in Python
- How to split a big data frame into smaller ones in R?
- Split the array into equal sum parts according to given conditions in C++
- Python program to find happiness by checking participation of elements into sets
- Python Program to Extract Elements from a List in a Set
- Largest rectangle sum smaller than num in JavaScript
- Convert set into a list in Python
- Split string into equal parts JavaScript
- Minimum numbers which is smaller than or equal to N and with sum S in C++

Advertisements