#K40692. Equal Subset Partition for K Subsets
Equal Subset Partition for K Subsets
Equal Subset Partition for K Subsets
Given an array of integers and an integer k, determine whether it is possible to partition the array into k subsets such that the sum of the elements in each subset is equal. In mathematical terms, let the total sum of elements be \(S\) and the target for each subset be \(T = S/k\). The partition is only possible if \(S\) is divisible by \(k\), and there exists a way to form k subsets each summing to \(T\).
Note: The input is read from stdin
and the answer should be printed to stdout
as either True
or False
.
inputFormat
The input is given via stdin
over three lines:
- The first line contains an integer
k
, the number of subsets. - The second line contains an integer
n
, which is the number of elements in the array. - The third line contains
n
space-separated integers representing the elements of the array.
outputFormat
Output a single line to stdout
: True
if the array can be partitioned into k
subsets with equal sum, otherwise False
.
4
7
4 3 2 3 5 2 1
True
</p>