Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to find the minimum number of jumps required to reach the end of the array using C#?
The minimum number of jumps problem involves finding the least number of jumps required to reach the end of an array, where each element represents the maximum number of positions you can jump forward from that index.
Given an array where each element represents the maximum jump length from that position, we need to find the minimum jumps to reach the last index. For example, in array {1, 3, 6, 3, 2, 3, 6, 8, 9, 5}, we can reach the end in 4 jumps.
Recursive Approach
The recursive approach explores all possible jumps from each position and returns the minimum jumps needed. At each position, we try all possible jumps and take the minimum result.
Example
using System;
namespace ConsoleApplication {
public class Arrays {
public int MinJumps(int[] arr, int l, int h) {
if (h == l)
return 0;
if (arr[l] == 0)
return int.MaxValue;
int min = int.MaxValue;
for (int i = l + 1; i <= h && i <= l + arr[l]; i++) {
int jumps = MinJumps(arr, i, h);
if (jumps != int.MaxValue && jumps + 1 < min)
min = jumps + 1;
}
return min;
}
}
class Program {
static void Main(string[] args) {
Arrays a = new Arrays();
int[] arrm = { 1, 3, 6, 3, 2, 3, 6, 8, 9, 5 };
int n = arrm.Length;
Console.Write("Minimum number of jumps to reach end is " + a.MinJumps(arrm, 0, n - 1));
}
}
}
The output of the above code is −
Minimum number of jumps to reach end is 4
Dynamic Programming Approach
A more efficient solution uses dynamic programming to avoid redundant calculations. We build up the solution from the bottom, storing minimum jumps for each position.
Example
using System;
class Program {
static int MinJumpsDP(int[] arr) {
int n = arr.Length;
if (n <= 1) return 0;
if (arr[0] == 0) return -1;
int[] dp = new int[n];
for (int i = 0; i < n; i++) {
dp[i] = int.MaxValue;
}
dp[0] = 0;
for (int i = 0; i < n - 1; i++) {
if (dp[i] == int.MaxValue) continue;
for (int j = i + 1; j <= Math.Min(i + arr[i], n - 1); j++) {
dp[j] = Math.Min(dp[j], dp[i] + 1);
}
}
return dp[n - 1] == int.MaxValue ? -1 : dp[n - 1];
}
static void Main(string[] args) {
int[] arr = { 1, 3, 6, 3, 2, 3, 6, 8, 9, 5 };
Console.WriteLine("Minimum jumps (DP): " + MinJumpsDP(arr));
int[] arr2 = { 2, 3, 1, 1, 4 };
Console.WriteLine("Minimum jumps (DP): " + MinJumpsDP(arr2));
}
}
The output of the above code is −
Minimum jumps (DP): 4 Minimum jumps (DP): 2
Greedy Approach (Most Efficient)
The most efficient solution uses a greedy approach that tracks the farthest reachable position and counts jumps only when necessary.
Example
using System;
class Program {
static int MinJumpsGreedy(int[] arr) {
int n = arr.Length;
if (n <= 1) return 0;
if (arr[0] == 0) return -1;
int jumps = 0;
int currentEnd = 0;
int farthest = 0;
for (int i = 0; i < n - 1; i++) {
farthest = Math.Max(farthest, i + arr[i]);
if (i == currentEnd) {
jumps++;
currentEnd = farthest;
if (currentEnd >= n - 1) break;
}
}
return currentEnd >= n - 1 ? jumps : -1;
}
static void Main(string[] args) {
int[] arr1 = { 1, 3, 6, 3, 2, 3, 6, 8, 9, 5 };
Console.WriteLine("Array: [1, 3, 6, 3, 2, 3, 6, 8, 9, 5]");
Console.WriteLine("Minimum jumps (Greedy): " + MinJumpsGreedy(arr1));
int[] arr2 = { 2, 3, 1, 1, 4 };
Console.WriteLine("Array: [2, 3, 1, 1, 4]");
Console.WriteLine("Minimum jumps (Greedy): " + MinJumpsGreedy(arr2));
int[] arr3 = { 3, 2, 1, 0, 4 };
Console.WriteLine("Array: [3, 2, 1, 0, 4]");
Console.WriteLine("Minimum jumps (Greedy): " + MinJumpsGreedy(arr3));
}
}
The output of the above code is −
Array: [1, 3, 6, 3, 2, 3, 6, 8, 9, 5] Minimum jumps (Greedy): 4 Array: [2, 3, 1, 1, 4] Minimum jumps (Greedy): 2 Array: [3, 2, 1, 0, 4] Minimum jumps (Greedy): -1
Comparison of Approaches
| Approach | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Recursive | O(n^n) | O(n) | Understanding the problem |
| Dynamic Programming | O(n^2) | O(n) | Medium-sized arrays |
| Greedy | O(n) | O(1) | Large arrays, optimal solution |
Conclusion
The minimum jumps problem can be solved using recursive, dynamic programming, or greedy approaches. The greedy solution with O(n) time complexity is most efficient, tracking the farthest reachable position and incrementing jumps only when reaching the boundary of the current jump range.
