
Let 'N' = 5, 'M' = 7, and 'A' = [0, 6, 1, 3, 2]. In the first operation, Alice will choose elements at indices 1 and 4, 'A[1]' = 6 and 'A[4]' = 2. The array becomes [0, 0, 1, 3, 3]. As it is non-decreasing after a single operation. Hence, the answer is 1.
The first line contains an integer 'T', which denotes the number of test cases.
For every test case:-
The first line contains two integers, 'N', and 'M' denoting the size of the array 'A' and the above-mentioned variable respectively.
The next line contains 'N' space-separated integers denoting the elements of the array 'A'.
For each test case, Return a number 'C' denoting the minimum number of such operations required to make the array non-decreasing.
You don’t need to print anything. Just implement the given function.
1 <= 'T' <= 10^5
1 <= 'N', 'M' <= 10^5
0 <= 'A[i]' < 'M'
Time Limit: 1 sec
Approach:-
Let's check that the answer to the problem is ≤ 'x'. Then, for each element, you have some interval (interval on the "circle" of remainders modulo m) of values that it can be equal to. For example, 'm' = 6 and array 'A' = [1, 2, 4] and for 'x' = 3 the range for 'A[0]' is (1,4), for 'A[1]' is (2,5), and for 'A[2]' is (4,1).
So we need to check, if we can pick in each interval some point, to make all these values non-decreasing.
We can do it greedily! Each time, let's take the smallest element from the interval, that is at least the previously chosen value.
If the array 'A' can be made non-decreasing using 'x' number of operations, then it can also be done in 'x+1' operations. As we can choose none of the indices of the array for the 'x+1'th operation. So, make a binary search on 'x' for the minimum number of operations.
Algorithm:-
Element Count in Ranges
First Digit One
Minimize Maximum Adjacent Distance
Sorted Doubly Linked List to Balanced BST
Minimized Maximum of Products Distributed to Any Store