# Minimum jumps required using Dynamic programming (DP)

Given an array of integers of length N in which value of each index represent maximum jump u can take from that index. You have to **find minimum jumps required** to reach from 0th index to the end of the array(last index).

Submitted by Ritik Aggarwal, on December 17, 2018

**Problem:** You are given an array of integers of length N in which value of each index represents maximum jump u can take from that index. You have to find minimum jumps required to reach from 0th index to the end of the array(last index).

**Constraints:**

1 <= N <= 30,000 0 <= A[i] <= 100

Sample Input 1: 6 2 2 3 1 0 3 Sample Output 1: minimum jumps required is 2 Sample Input 2: 11 1 3 5 8 9 2 6 7 6 8 9 Sample Output 2: minimum jumps required is 3

**Explanation of the problem:**

**For the sample Input1,**

We can jump from **0 ^{th}** index to

**2**index then to last index. So,

^{nd}**2**jumps required.

**For the sample input2,**

We can jump from **0 ^{th}** index to

**1**index then

^{st}**3**index and then last index. So,

^{rd}**3**jumps required.

**Solution: For sample input – 1**

**STATUS OF DP MATRIX AFTER i**

^{th}ITERATION**Note:** Since, we have to take minimum of all the possible jumps so if there are no jumps possible that is arr[i] = 0 then cell will get initialize to non- updated minimum value that is 1000000(denotes infinite steps). Due to presence of 1000001 at 4th index 3rd index also gets this value though arr[3] is non-zero.

**Algorithm:**

**STEP-1:**Create a 1D dp matrix in which**i**cell represent minimum jumps required from^{th}**i**index to the last.^{th}**STEP-2:**Initialize the last index of dp matrix to be zero as it is the end of the array.**STEP-3:**Start filling the dp matrix from second last index.**STEP-4:**From**i**index we can jump maximum of arr[i] value so taking minimum of dp[i + j] where^{th}**j**is ranging from 1 to arr[i] and i + j must be less than length of dp matrix.**STEP-5:**Add 1 to the answer got from step 4(1 jump is required from ith to i+j index) and store the answer at**i**index of dp matrix.^{th}**STEP-6:**return the answer of**0**index of dp matrix.^{th}

**The time complexity of the above code is O(N^2).**

**C++ Implementation **

#include <iostream> using namespace std; int minJumps(int arr[], int n){ int dp[n] = {0}; // if already at last index the number of steps is 0 dp[n-1] = 0; // filling from second last index for(int i = n - 2;i>=0;i--){ int min = 1000000; // value at an index of arr donates maximum_possible jump int jumps_range = arr[i]; // i + j < n is used to avoid jumping out of array length // && also jump can vary from 1 to value of arr at that index for(int j = 1;j<=jumps_range && i + j < n;j++){ // taking minimum of all possible jumps as ith index of // dp matrix represent minmum jumps required to reach from ith // index to the end if(min > dp[i+j]){ min = dp[i+j]; } } // also have to add 1 as 1 jump is required to jump on any // index in jump_range and storing that value dp[i] = 1 + min; } // return the minimum jumps from 0th index to the end return dp[0]; } // driver function to check the code int main() { int n; cout<<"Enter size of the array: "; cin >> n; int arr[n] = {0}; cout<<"Enter array elements: "; for(int j = 0;j<n;j++){ cin >> arr[j]; } for(int val : arr){ cout << val << " "; } cout <<"\nminimum jumps required is "<< minJumps(arr, n) << endl; return 0; }

**Output**

Enter size of the array: 6 Enter array elements: 2 2 3 1 0 3 2 2 3 1 0 3 minimum jumps required is 2

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.