#C1704. Partition to K Equal Sum Subsets
Partition to K Equal Sum Subsets
Partition to K Equal Sum Subsets
You are given an array of n integers and an integer k. Your task is to determine whether it is possible to partition the array into exactly k non-empty subsets such that the sum of the elements in each subset is equal.
Let \( S = \sum_{i=1}^{n} a_i \) be the total sum of the array. In order for a valid partition to exist, it is necessary that \( S \) is divisible by \( k \), and each subset must sum to \( \frac{S}{k} \). If these conditions are met, output YES
; otherwise, output NO
.
Note: You must read the input from standard input (stdin) and print the result to standard output (stdout).
inputFormat
The first line contains two integers n and k representing the number of elements in the array and the number of subsets respectively.
The second line contains n space-separated integers representing the elements of the array.
outputFormat
Output a single line: YES
if it is possible to partition the array into k subsets with equal sum, and NO
otherwise.
4 2
1 1 2 2
YES