Open In App

Find the minimum of maximum length of a jump required to reach the last island in exactly k jumps

Last Updated : 07 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] of integers, where the ith integer represents the position where an island is present, and an integer k (1 ? k < N). A person is standing on the 0th island and has to reach the last island, by jumping from one island to another in exactly k jumps, the task is to find the minimum of the maximum length of a jump a person will make in his journey. Note that the position of all the islands are given in ascending order.
Examples: 
 

Input: arr[] = {2, 15, 36, 43}, k = 1 
Output: 41 
There is only way to reach the end 
2 -> 43 
Input: arr[] = {2, 15, 36, 43}, k = 2 
Output: 28 
There are two ways to reach the last island 
2 -> 15 -> 43 
Here maximum distance between any two consecutive islands is between 43 and 15 that is 28. 
2 -> 36 -> 43 
Here maximum distance between any two consecutive islands is between 36 and 2 that is 34. 
Thus minimum of 28 and 34 is 28. 
 

 

Approach: The idea is to use binary search, and for a distance mid, compute whether it is possible to reach the end of the array in exactly k jumps where the maximum distance between any two islands chosen for jumping is less than or equal to the distance mid, then check if some distance less than mid exists for which it is possible to reach the end in exactly k jumps.
Below is the implementation of the above approach: 
 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function that returns true if it is possible
// to reach end of the array in exactly k jumps
bool isPossible(int arr[], int n, int dist, int k)
{
 
    // Variable to store the number of
    // steps required to reach the end
    int req = 0;
 
    int curr = 0;
    int prev = 0;
 
    for (int i = 0; i < n; i++) {
        while (curr != n && arr[curr] - arr[prev] <= dist)
            curr++;
        req++;
 
        if (curr == n)
            break;
        prev = curr - 1;
    }
 
    if (curr != n)
        return false;
 
    // If it is possible to reach the
    // end in exactly k jumps
    if (req <= k)
        return true;
 
    return false;
}
 
// Returns the minimum maximum distance required
// to reach the end of the array in exactly k jumps
int minDistance(int arr[], int n, int k)
{
    int l = 0;
    int h = arr[n - 1];
 
    // Stores the answer
    int ans = 0;
 
    // Binary search to calculate the result
    while (l <= h) {
        int m = (l + h) / 2;
        if (isPossible(arr, n, m, k)) {
            ans = m;
            h = m - 1;
        }
        else
            l = m + 1;
    }
 
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 15, 36, 43 };
    int n = sizeof(arr) / sizeof(int);
    int k = 2;
 
    cout << minDistance(arr, n, k);
 
    return 0;
}


Java




// Java implementation of the approach
 
class GFG
{
 
    // Function that returns true if it is possible
    // to reach end of the array in exactly k jumps
    static boolean isPossible(int arr[], int n, int dist, int k)
    {
 
        // Variable to store the number of
        // steps required to reach the end
        int req = 0;
 
        int curr = 0;
        int prev = 0;
 
        for (int i = 0; i < n; i++)
        {
            while (curr != n && arr[curr] - arr[prev] <= dist)
            {
                curr++;
            }
            req++;
 
            if (curr == n)
            {
                break;
            }
            prev = curr - 1;
        }
 
        if (curr != n)
        {
            return false;
        }
 
        // If it is possible to reach the
        // end in exactly k jumps
        if (req <= k)
        {
            return true;
        }
 
        return false;
    }
 
    // Returns the minimum maximum distance required
    // to reach the end of the array in exactly k jumps
    static int minDistance(int arr[], int n, int k)
    {
        int l = 0;
        int h = arr[n - 1];
 
        // Stores the answer
        int ans = 0;
 
        // Binary search to calculate the result
        while (l <= h)
        {
            int m = (l + h) / 2;
            if (isPossible(arr, n, m, k))
            {
                ans = m;
                h = m - 1;
            }
            else
            {
                l = m + 1;
            }
        }
 
        return ans;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = {2, 15, 36, 43};
        int n = arr.length;
        int k = 2;
 
        System.out.println(minDistance(arr, n, k));
    }
}
 
/* This code contributed by PrinciRaj1992 */


Python3




# Python3 implementation of the approach
 
# Function that returns true if it is possible
# to reach end of the array in exactly k jumps
def isPossible(arr, n, dist, k) :
 
    # Variable to store the number of
    # steps required to reach the end
    req = 0
 
    curr = 0
    prev = 0
 
    for i in range(0, n):
        while (curr != n and (arr[curr] - arr[prev]) <= dist):
            curr = curr + 1
        req = req + 1
 
        if (curr == n):
            break
        prev = curr - 1
     
 
    if (curr != n):
        return False
 
    # If it is possible to reach the
    # end in exactly k jumps
    if (req <= k):
        return True
 
    return False
 
 
# Returns the minimum maximum distance required
# to reach the end of the array in exactly k jumps
def minDistance(arr, n, k):
 
    l = 0
    h = arr[n - 1]
 
    # Stores the answer
    ans = 0
 
    # Binary search to calculate the result
    while (l <= h):
        m = (l + h) // 2;
        if (isPossible(arr, n, m, k)):
            ans = m
            h = m - 1
         
        else:
            l = m + 1
     
 
    return ans
 
# Driver code
 
arr = [ 2, 15, 36, 43 ]
n =  len(arr)
k = 2
 
print(minDistance(arr, n, k))
 
# This code is contributed by ihritik


C#




// C# program to implement
// the above approach
using System;
 
class GFG
{
 
    // Function that returns true if it is possible
    // to reach end of the array in exactly k jumps
    static bool isPossible(int []arr, int n, int dist, int k)
    {
 
        // Variable to store the number of
        // steps required to reach the end
        int req = 0;
 
        int curr = 0;
        int prev = 0;
 
        for (int i = 0; i < n; i++)
        {
            while (curr != n && arr[curr] - arr[prev] <= dist)
            {
                curr++;
            }
            req++;
 
            if (curr == n)
            {
                break;
            }
            prev = curr - 1;
        }
 
        if (curr != n)
        {
            return false;
        }
 
        // If it is possible to reach the
        // end in exactly k jumps
        if (req <= k)
        {
            return true;
        }
 
        return false;
    }
 
    // Returns the minimum maximum distance required
    // to reach the end of the array in exactly k jumps
    static int minDistance(int []arr, int n, int k)
    {
        int l = 0;
        int h = arr[n - 1];
 
        // Stores the answer
        int ans = 0;
 
        // Binary search to calculate the result
        while (l <= h)
        {
            int m = (l + h) / 2;
            if (isPossible(arr, n, m, k))
            {
                ans = m;
                h = m - 1;
            }
            else
            {
                l = m + 1;
            }
        }
 
        return ans;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int []arr = {2, 15, 36, 43};
        int n = arr.Length;
        int k = 2;
 
        Console.WriteLine(minDistance(arr, n, k));
    }
}
 
/* This code contributed by PrinciRaj1992 */


PHP




<?php
// Php implementation of the approach
 
// Function that returns true if it is possible
// to reach end of the array in exactly k jumps
function isPossible($arr, $n, $dist, $k)
{
 
    // Variable to store the number of
    // steps required to reach the end
    $req = 0;
 
    $curr = 0;
    $prev = 0;
 
    for ($i = 0; $i < $n; $i++)
    {
        while ($curr != $n && $arr[$curr] - $arr[$prev] <= $dist)
            $curr++;
        $req++;
 
        if ($curr == $n)
            break;
        $prev = $curr - 1;
    }
 
    if ($curr != $n)
        return false;
 
    // If it is possible to reach the
    // end in exactly k jumps
    if ($req <= $k)
        return true;
 
    return false;
}
 
// Returns the minimum maximum distance required
// to reach the end of the array in exactly k jumps
function minDistance($arr, $n, $k)
{
    $l = 0;
    $h = $arr[$n - 1];
 
    // Stores the answer
    $ans = 0;
 
    // Binary search to calculate the result
    while ($l <= $h)
    {
        $m = floor(($l + $h) / 2);
        if (isPossible($arr, $n, $m, $k))
        {
            $ans = $m;
            $h = $m - 1;
        }
        else
            $l = $m + 1;
    }
 
    return $ans;
}
 
    // Driver code
    $arr = array( 2, 15, 36, 43 );
    $n = count($arr);
    $k = 2;
 
    echo minDistance($arr, $n, $k);
 
    // This code is contributed by Ryuga
?>


Javascript




<script>
// Javascript implementation of the approach
 
  
    // Function that returns true if it is possible
    // to reach end of the array in exactly k jumps
    function isPossible(arr, n, dist, k)
    {
   
        // Variable to store the number of
        // steps required to reach the end
        let req = 0;
   
        let curr = 0;
        let prev = 0;
   
        for (let i = 0; i < n; i++)
        {
            while (curr != n && arr[curr] - arr[prev] <= dist)
            {
                curr++;
            }
            req++;
   
            if (curr == n)
            {
                break;
            }
            prev = curr - 1;
        }
   
        if (curr != n)
        {
            return false;
        }
   
        // If it is possible to reach the
        // end in exactly k jumps
        if (req <= k)
        {
            return true;
        }
   
        return false;
    }
   
    // Returns the minimum maximum distance required
    // to reach the end of the array in exactly k jumps
    function minDistance(arr, n, k)
    {
        let l = 0;
        let h = arr[n - 1];
   
        // Stores the answer
        let ans = 0;
   
        // Binary search to calculate the result
        while (l <= h)
        {
            let m = Math.floor((l + h) / 2);
            if (isPossible(arr, n, m, k))
            {
                ans = m;
                h = m - 1;
            }
            else
            {
                l = m + 1;
            }
        }
   
        return ans;
    }
       
 
// Driver Code
 
        let arr = [2, 15, 36, 43];
        let n = arr.length;
        let k = 2;
   
         document.write(minDistance(arr, n, k));
           
</script>


Output: 

28

 

Time Complexity: O(N2)

Auxiliary Space: O(1)



Similar Reads

Minimum number of jumps to reach end (Jump Game)
Given an array arr[] where each element represents the max number of steps that can be made forward from that index. The task is to find the minimum number of jumps to reach the end of the array starting from index 0. Examples: Input: arr[] = {1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9}Output: 3 (1-&gt; 3 -&gt; 9 -&gt; 9)Explanation: Jump from 1st element to
22 min read
Maximum jumps to reach end of Array with condition that index i can make arr[i] jumps
Given an integer N and an array arr[ ] of size N, the task is to find the maximum jumps to reach the end of the array given the constraint that from index i can make arr[i] jumps and reach the position i+arr[i]. Examples: Input: N = 5, arr[] = {2, 3, 5, 7, 9}Output: 12 Explanation:At index 0 make 2 jumps and move to index 2 and make 5 jumps after t
6 min read
Minimize cost to reach end of an array by two forward jumps or one backward jump in each move
Given an array arr[] consisting of N positive integers, the task is to find the minimum cost required to either cross the array or reach the end of the array by only moving to indices (i + 2) and (i - 1) from the ith index. Examples: Input: arr[] = {5, 1, 2, 10, 100}Output: 18Explanation:Optimal cost path (0 based indexing): 0 ? 2 ? 1 ? 3 ? 5Theref
11 min read
Number of jump required of given length to reach a point of form (d, 0) from origin in 2D plane
Given three positive integers a, b and d. You are currently at origin (0, 0) on infinite 2D coordinate plane. You are allowed to jump on any point in the 2D plane at euclidean distance either equal to a or b from your current position. The task is to find the minimum number of jump required to reach (d, 0) from (0, 0).Examples: Input : a = 2, b = 3
7 min read
Python Program for Number of jump required of given length to reach a point of form (d, 0) from origin in 2D plane
Given three positive integers a, b and d. You are currently at origin (0, 0) on infinite 2D coordinate plane. You are allowed to jump on any point in the 2D plane at euclidean distance either equal to a or b from your current position. The task is to find the minimum number of jump required to reach (d, 0) from (0, 0). Examples: Input : a = 2, b =
2 min read
Maximum power of jump required to reach the end of string
Given a string consisting of 1 and 0, the task is to find out the maximum power of jump required to reach the end of the string. At a time you can jump from one 1 to the next 1 or from one 0 to the next 0. Note: Power of jump is defined as the distance between two consecutive 1's or two consecutive 0's. Examples: Input: 10101 Output: 2 First, make
7 min read
Minimum cost to reach the end of the array with maximum jumps of length K
Given an array arr[] of size N and an integer K, one can move from an index i to any other index j such that j ? i+k. The cost of being at any index 'i', is arr[i]. The task is to find the minimum cost to reach the end of the array starting from index 0. Examples: Input : arr[] = {2, 4, 1, 6, 3}, K = 2Output: 6Explanation: Path can be taken as 2 -
11 min read
Minimum jumps to reach last building in a matrix
Given a matrix containing an integer value, In which each cell of the matrix represents the height of the building. Find minimum jumps needed to reach from First building (0, 0) to last (n-1, m-1). Jump from a cell to the next cell is the absolute difference between two building heights. Examples : Input : int height[][] = {{ 5, 4, 2 }, { 9, 2, 1 }
15 min read
Minimum jumps to traverse all integers in range [1, N] such that integer i can jump i steps
Given an integer N, the task is to find the minimum steps to visit all integers in the range [1, N] by selecting any integer and jump i steps at every ith jump. Note: It is possible to revisit an integer more than once. Examples: Input: N = 6Output: 3Explanation: One of the following way is: First start at first number and visit the integers {1, 2,
7 min read
Minimum cost to reach end of array when a maximum jump of K index is allowed
Given an array arr[] of N integers and an integer K, one can move from an index i to any other j if j &lt;= i + k. The cost of moving from one index i to the other index j is abs(arr[i] - arr[j]). Initially, we start from the index 0 and we need to reach the last index i.e. N - 1. The task is to reach the last index in the minimum cost possible.Exa
12 min read