
You are given an array A of length N, now is it possible to split the array into different subsequences of equal length such that all elements in a subsequence are equal. If it is possible to divide the array into such subsequences of length greater than 1 print “YES” if is not possible print “NO”.
Example: [ 1 1 2 2 3 3 3 3] can be divided into [1 1] [2 2] [3 3] [3 3]
The first line of the input contains ‘T’ denoting the number of test cases.
The first line of each test case contains the three integers N, length of the array.
The second line of each test case contains N space-separated integers of the array A.
Output Format :
Return the result of each test case in a new line.
1 <= T <= 5
1 <= N <= 3000
1 <= A[i] <= 5000
Where ‘T’ denotes the number of test cases.
2
8
1 1 2 2 3 3 4 4
8
1 1 1 2 2 2 3 3
YES
NO
For the first test case,
We can make subsequences of length 2: [1 1] [2 2] [3 3] [4 4]
For the second test case,
We can make only subsequences of length 1, such that all subsequences are of equal length and all elements of the subsequence are the same.
3
9
26 49 26 23 49 23 23 26 49
10
21 47 24 47 27 21 27 32 32 24
18
1 1 31 31 1 31 1 31 1 31 1 1 31 31 1 1 31 31
YES
YES
YES
Find gcd of the frequencies
Explanation:
Algorithm:
O( N*log(N) )
where N is the length of the array
Traversing array is O(N) and find gcd of two elements is O( log(N) ) therefore the time complexity is O(N)
O(N)
where N is the length of the array
We are creating a new frequency array that will take O(N) space.