


Input:
‘N’ = 3
‘ARR’ = [ 2, 1, 1 ]
The shortest way to reach index 2 is
Index 0 => Index 2
that requires only 1 jump.
The first line contains ‘T,’ denoting the number of test cases.
The first line of each test case contains ‘N’ denoting the size of the ‘ARR’.
The second line of each test case contains 'N' space-separated Integers denoting the ‘ARR’.
For each test case, Return the answer as described in the problem statement.
Output for each test case is printed on a separate line.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10000
0 <= ARR [ i ] <= N
Time Limit: 1 sec
We can use dynamic programming. We can build an array ‘DP’ of size ‘N’ where
DP [ i ] denotes the minimum number of jumps to reach index ‘i’ from index 0.
We can traverse the array from left to right for every index ‘i’ from 0 to ‘N’ - 1. For every index ‘i’, we can iterate from index ‘i’ + 1 to + ‘i’ + ARR [ i ], updating the values if jumping from index ‘i’ is more optimal.
From any jump point i, we can reach any index from (i + 1) to (i + A[i]), and there will be some value between range ARR [i + 1] to ARR [i + ARR[i]], which can provide the farthest reach from that range.
So we define lower and upper end of the current jump point and calculate the farthest reach in that range. Whenever we reach the upper endpoint of a range, we update the upper end with the value of farthest reach and increment jump count. We continue this process till the value of farthest reachable index is greater than n - 1.