You are given a non-decreasing array of positive integers, ‘NUMS’, and provided with an integer ‘K’. Your take is to find out whether it is possible to divide ‘NUMS’ into increasing subsequences such that each of their lengths is at least ‘K’.
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the ‘T’ test cases follow.
The first line of each test case contains two positive integers, ‘N’ denoting the size of the array ‘NUMS’ and ‘K’.
The second line of each test case contains ‘N’ space-separated positive integers denoting the array elements.
Output Format:
For each test case, print ‘YES’ if it is possible to create the required subsequences, otherwise print ‘NO’.
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10^6
1 <= ‘K’ <= ‘N’
1 <= ‘NUMS[i]’ <= 10^6
Where ‘NUMS[i]’ is the i-th element of the array ‘NUMS’.
Time Limit: 1 sec
1
7 3
1 2 2 3 3 4 4
YES
We can divide the original array into [1,2,3,4] and [2,3,4] and we can see both of the new arrays have length >= 3.
2
5 3
4 4 5 6 7
4 4
5 7 8 9
NO
YES
For test case 1, It can be proved that no combination of subsequences fulfills the required conditions. Hence ‘NO’.
For test case 2, The original array itself is a valid answer, Hence ‘YES’.
How does the frequency of numbers affect the answer?
O(N*log(N)), where N is the size of the array ‘NUMS’.
We need to traverse the array while inserting it into the map, which has time complexity O(N), and insertion into the map takes O(log(N)) time. Hence net time complexity: O(Nlog(N)).
O(N), where N is the size of the array ‘NUMS’.
In the worst case, when all the elements of NUMS are different, then there are N elements in the map, hence space complexity is O(N).