- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given, an array of non-negative integers denoting the maximum number of steps that can be made forward from that element. The pointer is initially positioned at the first index [0 index] of the array. Your goal is to reach the last index of the array in the minimum number of steps. If it is not possible to reach the end of the array then print the maximum integer.

**naive approach** is to begin from initial{the primary} component and recursively call for all the components accessible from the first element. The minimum range of jumps to reach the end from first is calculated using the minimum range of jumps required to achieve end from the elements accessible from first.

minJumps(start, end) = Min ( minJumps(k, end) ) for all k accessible from the start

Here we’ll use the top-down approach of dynamic programming. We’ll use Hashmap to store the subproblem results and whenever we create a solution, first check if the subproblem is already resolved, if yes then use it.

Input: { 1, 2, 4, 1, 2, 2, 1, 1, 3, 8 } Output: Minimum number of steps = 6 {1-->2-->4-->1-->3-->8}

The first element is 1, so it can only go to 2. The second element is 2, so can make at most 2 steps eg to 4 or 1. It goes to 4 from where it reaches 1 and so on.

The complexity of dynamic programming approach to find the minimum number of jumps to reach the end of an array is O(n^2) with space complexity of O(n)

#include<stdio.h> #include<limits.h> int min_steps (int arr[], int n){ int steps[n]; int i, j; if (n == 0 || arr[0] == 0) return INT_MAX; steps[0] = 0; for (i = 1; i < n; i++){ steps[i] = INT_MAX; for (j = 0; j < i; j++){ if (i <= j + arr[j] && steps[j] != INT_MAX){ steps[i] = (steps[i] < (steps[j] + 1)) ? steps[i] : steps[j] + 1; break; } } } return steps[n - 1]; } int main (){ int arr[100]; int n; printf ("Enter size of the array:"); scanf ("%d", &n); printf ("Enter elements in the array:"); for (int i = 0; i < n; i++){ scanf ("%d", &arr[i]); } printf ("Minimum number of steps : %d", min_steps (arr, n)); return 0; }

Enter size of array : 7 Enter elements in the array :2 1 1 5 2 1 1 Minimum number of steps : 3

- Related Questions & Answers
- How to find the minimum number of jumps required to reach the end of the array using C#?
- Program to find minimum jumps to reach home in Python
- Minimum Number of Jumps Problem
- Program to find minimum number of hops required to reach end position in Python
- Find the number of jumps to reach X in the number line from zero in C++
- Find minimum steps required to reach the end of a matrix in C++
- Program to Find Minimum Jumps Required to Reach a Value with Different Parity in Python
- Count number of ways to jump to reach end in C++
- Find the minimum number of steps to reach M from N in C++
- Program to find number of minimum steps to reach last index in Python
- Program to find minimum number of vertices to reach all nodes using Python
- Program to find out the minimum number of moves for a chess piece to reach every position in Python
- Find the minimum of maximum length of a jump required to reach the last island in exactly k jumps in Python
- Program to find minimum number of buses required to reach final target in python
- Maximum power of jump required to reach the end of string in C++

Advertisements