
Input: ‘N’ = 5, ‘A’ = [1, 2, 3, 4, 5]
Output: 4
Initially, array ‘B = [0, 0, 0, 0, 0], we will perform the following sequence of moves:
‘B[0] = B[0] - A[0]’
‘B[2] = B[2] + A[2]’
‘B[3] = B[3] + A[3]’
‘B[4] = B[4] + A[4]’
After these moves, the updated array will be [-1, 0, 3, 4, 5], and this is the minimum possible number of moves to make this array strictly increase.
The first line contains a single integer ‘T’ denoting the number of test cases, then the test case follows.
For each test case, the first line will contain the integer ‘N’, the number of elements in the arrays ‘A’.
The second line will contain ‘N’ spaced integers i.e., the elements of the array 'A'.
For each test case, return the minimum number of the moves needed to make the array ‘B’ strictly increasing.
You are not required to print anything; it has already been taken care of. Just implement the function.
1 ≤ T ≤ 10
2 ≤ N ≤ 10^3
1 ≤ A[i] ≤ 10^9
It is guaranteed that the sum of 'N' is ≤ 10^3 over all test cases.
Time limit: 1 sec
Approach:
Initially, the array 'B' has all elements set to 0, and it's better to have 0 in the final array. As the final array is strictly increasing, there will be only one 0 in this array.
Now we know that there will be 0 in the final array. If we fix the position of the 0, we will set the other values greedily: find the smallest value for each element, which is bigger than the previous one, and similarly before that element.
We will check this for all positions; we can calculate the answer in O(n) time for each. The minimum of all these answers will be the final answer. This will be done in O(n^2) complexity.
Algorithm: