

The first line contains ‘T’, denoting the number of test cases.
The first line of each test case contains two space-separated integers, ‘N’ and ‘K’, denoting the number of photos and the minimum size of each batch.
The second line of each test case contains an array ‘photos’ of ‘N’ space-separated integers, denoting the resolution of the ith photo.
For each test case, print an integer denoting the minimum ‘maximum error’.
You are not required to print the expected output. It has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= K <= N <= 10^4
1 <= photos[i] <= 10^9
Time Limit: 1 sec
The first thing is that we can show that it is always beneficial to sort the array and then divide the array into subarrays. Then if we fix the error, we can use dynamic programming to divide the array into subarrays.
Let us define a function ‘check’, which when passed a parameter ‘error’, returns whether the array can be divided such that its maximum error is less or equal to ‘error’.
Now, we can check for which ‘error’, there are some valid breaks. We can find this using linear search, as we need the minimum such ‘error’. Note that the minimum error we can achieve is 0, and the maximum is the difference between the highest resolution and the lowest resolution in the entire array.
The steps are as follows:
Instead of linear searching the ‘error’, we can use binary search to find the optimal error and check if it is valid using the same dynamic programming approach as discussed in approach 1.
The steps are as follows:
Mirrored Difference Update
K-Balanced Linked List
Sorted Doubly Linked List to Balanced BST
Longest Unique Subarray
Minimized Maximum of Products Distributed to Any Store