

You can perform multiple operations on a single element also.
The first line of the input contains a single integer T, representing the number of test cases.
The first line of each test case consists of two space-separated integers, representing the values of N (size of 'ARR') and K(maximum number of updates possible).
The second line of each test case contains N space-separated integers, denoting the elements of the 'ARR'.
For each test case, print a single integer, representing the maximum number of elements that can be made equal after performing K or fewer increments.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10^2
1 <= N <= 10^3
1 <= ARR[i], K <= 10^9
Time Limit: 1sec
We will have to sort the input array in order for this algorithm to work. After that, we run two loops- outer loop(for i) from N - 1 to 0th index which will be used for fixing the array element to which we will try to increment all other elements that are currently less than it; and an inner loop which will go from (i - 1) to 0th index to keep track of remaining updates(stored in ‘remaining’) and current number of equal elements(stored in ‘curr’). We update our final ‘ans’ variable whenever curr>ans.
Algorithm:
Approach: An efficient way to solve this problem is to first sort the array and then use binary search on the sorted array. In binary search, we will try to find a range of ‘X’ elements in the given array for which all elements in the range ’X’ can be made equal in at most K operations. For doing this, we maintain an auxiliary array ‘PRESUM’ size ‘N’ + 1. ‘PRESUM[i]’ will indicate the prefix sum from 0 to ith index.
In binary search, we use a helper function ‘helper’ to determine whether any ‘X’ elements can be made equal to each other or not. The value of ‘X’ will be the midpoint of low and high points in the binary search at any point. Initially, low = 1(the least possible answer) and high = N(the maximum possible answer). In the helper function, we use a sliding window to determine whether there are x number of elements present which when incremented will become equal. Now, while checking for different windows using a for loop, the condition to be checked will be:-
(X * ARR[j - 1] - (PRESUM[j] - PRESUM[i] )) <= K, where i and j are the starting and ending indices of the sliding window.
We update our ‘ANS’ variable based on the output of the helper function and also update the starting and ending points of binary search accordingly.