Permute the Array

3.1

27 votes
Arrays, Data Structures, Easy, Hash Maps, Implementation, One-dimensional
Problem

You are given an array consisting of N integers. You need to determine whether any permutation of given array exist such that the sum of all subarrays of length K are equal.

Input format :

  • The first line consist of T denoting the number of testcases.
  • The first line of each testcase consist of two space seperated integers N and K.
  • The second line of each testcase consist of N space seperated integers denoting array elements.

Output format :

  • Print "YES" or "NO" for each testcase in a new line.

Constraints :

  • 1T10
  • 1KN106
  • 1Ai105
  • N is divisible by K  i.e NmodK=0

 

Sample Input
2
4 2
3 5 5 2
5 5
1 2 3 4 5
Sample Output
NO
YES
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In the first test case there is no permutation for which the array satisifes the conditon.

In the second test since whole array is a subarray of length 5 so the answer is YES.

Editor Image

?