PHP Program for Minimum Number of Jumps to Reach End

This problem involves finding the minimum number of jumps required to reach the end of an array, where each element represents the maximum number of steps you can jump from that position.

Method 1: Naive Recursive Approach

The naive recursive approach explores all possible paths from each position and chooses the minimum number of jumps

<?php
function minJumpsRecursive($arr, $start, $end) {
    // Base case: If the starting index is the last index, no jumps are needed
    if ($start == $end) {
        return 0;
    }
    
    // If the current element is 0, it is not possible to make any further jumps
    if ($arr[$start] == 0) {
        return PHP_INT_MAX;
    }
    
    // Initialize the minimum number of jumps to a large value
    $minJumps = PHP_INT_MAX;
    
    // Try all possible jumps from the current position
    for ($i = $start + 1; $i <= $end && $i <= $start + $arr[$start]; $i++) {
        $jumps = minJumpsRecursive($arr, $i, $end);
        if ($jumps != PHP_INT_MAX && $jumps + 1 < $minJumps) {
            $minJumps = $jumps + 1;
        }
    }
    
    return $minJumps;
}

// Example usage
$arr = [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9];
$n = count($arr);
$minJumps = minJumpsRecursive($arr, 0, $n - 1);

if ($minJumps != PHP_INT_MAX) {
    echo "Minimum number of jumps required to reach the end: " . $minJumps;
} else {
    echo "It is not possible to reach the end.";
}
?>
Minimum number of jumps required to reach the end: 3

Method 2: Dynamic Programming

Dynamic programming optimizes the solution by storing results of subproblems to avoid redundant calculations

<?php
function minJumpsDynamic($arr, $n) {
    // Create an array to store the minimum number of jumps needed
    $minJumps = array_fill(0, $n, PHP_INT_MAX);
    $minJumps[0] = 0; // Base case: No jumps needed to reach the first element
    
    // Calculate the minimum number of jumps for each position
    for ($i = 1; $i < $n; $i++) {
        for ($j = 0; $j < $i; $j++) {
            // Check if it is possible to reach position $i from position $j
            if ($j + $arr[$j] >= $i) {
                // Update the minimum number of jumps for position $i
                $minJumps[$i] = min($minJumps[$i], $minJumps[$j] + 1);
            }
        }
    }
    
    return $minJumps[$n - 1];
}

// Example usage
$arr = [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9];
$n = count($arr);
$minJumps = minJumpsDynamic($arr, $n);

if ($minJumps != PHP_INT_MAX) {
    echo "Minimum number of jumps required to reach the end: " . $minJumps;
} else {
    echo "It is not possible to reach the end.";
}
?>
Minimum number of jumps required to reach the end: 3

Comparison

Method Time Complexity Space Complexity Best For
Recursive O(n^n) O(n) Small arrays
Dynamic Programming O(n²) O(n) Larger arrays

Conclusion

The dynamic programming approach is significantly more efficient than the naive recursive solution, making it suitable for larger arrays. Both methods correctly solve the minimum jumps problem, but dynamic programming avoids redundant calculations.

Updated on: 2026-03-15T10:34:48+05:30

172 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements