# Dynamic programming – Minimum Jumps to reach to end

Objective:  Given an array of non negative integers, start from the first element and reach the last by jumping. The jump length can be at most the value at the current position in the array. Optimum result is when you reach the goal in minimum number of jumps.

Example:

```Given array A = {2,3,1,1,4}
possible ways to reach the end (index list)
i) 0,2,3,4 (jump 2 to index 2, then jump 1 to index 3 then 1 to index 4)
ii) 0,1,4 (jump 1 to index 1, then jump 3 to index 4)

Since second solution has only 2 jumps it is the optimum result.
```

Approach:

Recursion-

1. Start from the index 0. Try out each option.
2. If the value at current index is ‘k’, then try out all the jumps from 1 to k.
4. Base cases are –
1. Check if you start index and end index are same then no further jumps are required, return 0.
2. If value on the start index is 0, then we can pass through that index, return Integer.Max
3. Calculate the remaining Length in the array which is yet to be travelled. If remaining length is less than the value at the present index then we do not need further recursion, we can reach to the destination in one jump so return 1.

Code:

Dynamic Programming- Top Down

If we look closely the diagram above we are solving many sub problems recursively. Here we will use Top-down approach of dynamic programming. We will use Hash Map to store the sub problems results and whenever we make a recursive call, first check if the sub problem is already solved, if yes then use it.

Code:

Output:

```Minimum Jumps required: 9
Dynamic Programming - Time taken: 0 miliseconds
```

__________________________________________________
Top Companies Interview Questions..-

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________