Find the minimum of maximum length of a jump required to reach the last island in exactly k jumps
Last Updated :
07 Mar, 2022
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++
#include <bits/stdc++.h>
using namespace std;
bool isPossible( int arr[], int n, int dist, int k)
{
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 (req <= k)
return true ;
return false ;
}
int minDistance( int arr[], int n, int k)
{
int l = 0;
int h = arr[n - 1];
int ans = 0;
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;
}
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
class GFG
{
static boolean isPossible( int arr[], int n, int dist, int k)
{
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 (req <= k)
{
return true ;
}
return false ;
}
static int minDistance( int arr[], int n, int k)
{
int l = 0 ;
int h = arr[n - 1 ];
int ans = 0 ;
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;
}
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));
}
}
|
Python3
def isPossible(arr, n, dist, k) :
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 (req < = k):
return True
return False
def minDistance(arr, n, k):
l = 0
h = arr[n - 1 ]
ans = 0
while (l < = h):
m = (l + h) / / 2 ;
if (isPossible(arr, n, m, k)):
ans = m
h = m - 1
else :
l = m + 1
return ans
arr = [ 2 , 15 , 36 , 43 ]
n = len (arr)
k = 2
print (minDistance(arr, n, k))
|
C#
using System;
class GFG
{
static bool isPossible( int []arr, int n, int dist, int k)
{
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 (req <= k)
{
return true ;
}
return false ;
}
static int minDistance( int []arr, int n, int k)
{
int l = 0;
int h = arr[n - 1];
int ans = 0;
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;
}
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));
}
}
|
PHP
<?php
function isPossible( $arr , $n , $dist , $k )
{
$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 ( $req <= $k )
return true;
return false;
}
function minDistance( $arr , $n , $k )
{
$l = 0;
$h = $arr [ $n - 1];
$ans = 0;
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 ;
}
$arr = array ( 2, 15, 36, 43 );
$n = count ( $arr );
$k = 2;
echo minDistance( $arr , $n , $k );
?>
|
Javascript
<script>
function isPossible(arr, n, dist, k)
{
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 (req <= k)
{
return true ;
}
return false ;
}
function minDistance(arr, n, k)
{
let l = 0;
let h = arr[n - 1];
let ans = 0;
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;
}
let arr = [2, 15, 36, 43];
let n = arr.length;
let k = 2;
document.write(minDistance(arr, n, k));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...