Equal Subset Sums (Small)
Time limit5sMemory limit512 MB
For each set of 20 distinct numbers, print two different non-empty subsets with equal sum in code order, or Impossible.
- Level
Medium5 of 10
- Topics
- Brute force, Hash map
- Solved
- No attempts yet
Problem
You are given a set of distinct positive integers. Find two non-empty subsets of that are different and whose elements add up to the same sum.
A subset is a set built only from elements of . Two subsets are different when they do not hold exactly the same elements. The two subsets may share elements.
Input
The first line holds the number of test cases . Each of the next lines holds one test case. A line starts with , the number of elements of , followed by the distinct positive integers of on the same line.
Limits
- is exactly .
- Every element of is a positive integer smaller than .
- No two elements of are equal.
Output
For each test case, first print one line holding "Case #x:", where x is the case number starting from 1.
If two different subsets with the same sum exist, print them on the next two lines, one subset per line. On each line print the elements of that subset in increasing order, separated by single spaces. If no such pair exists, print "Impossible" on a single line.
Several pairs can satisfy the condition, so exactly one pair counts as correct. Write the elements of in increasing order as , and give a non-empty subset the code . Among all pairs of different subsets with the same sum, take the pair whose larger code is smallest; if several pairs remain, take the one whose smaller code is smallest. Print the subset with the smaller code first.