- 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

Suppose we have an array A of numbers, in A the i-th number is the position where an island is present, and another integer k is given (1 ≤ k < N). Now, a person is standing on the 0-th island and has to reach the last island, by jumping from one island to another in exactly k jumps, we have to find the minimum of the maximum length of a jump a person will make in his/her journey. We have to keep in mind that the position of all the islands are given in ascending order.

So, if the input is like A = [7, 20, 41, 48], k = 2, then the output will be 28, as there are two ways to reach the last island 7 to 20 to 48, here the maximum distance between any two consecutive islands is between 48 and 20 that is 28. And 7 to 41 to 48 here the maximum distance between any two consecutive islands is between 41 and 7 that is 34. So, the minimum of 28 and 34 is 28.

To solve this, we will follow these steps −

Define a function isPossible() . This will take arr,dist, k

n := size of arr

req := 0

current := 0

previous := 0

for i in range 0 to n, do

while current is not same as n and (arr[current] - arr[previous]) <= dist, do

current := current + 1

req := req + 1

if current is same as n, then

come out from the loop

previous := current - 1

if current is not same as n, then

return False

if req <= k, then

return True

return False

From the main method, do the following −

n := size of arr

left := 0

right := last element of arr

ans := 0

while left −= right, do

mid := (left + right) / 2;

if isPossible(arr, mid, k) is non-zero, then

ans := mid

right := mid - 1

otherwise,

left := mid + 1

return ans

Let us see the following implementation to get better understanding −

def isPossible(arr,dist, k) : n = len(arr) req = 0 current = 0 previous = 0 for i in range(0, n): while (current != n and (arr[current] - arr[previous]) <= dist): current += 1 req += 1 if (current == n): break previous = current - 1 if (current != n): return False if (req <= k): return True return False def minimum_distance(arr, k): n = len(arr) left = 0 right = arr[-1] ans = 0 while (left <= right): mid = (left + right) // 2; if (isPossible(arr, mid, k)): ans = mid right = mid - 1 else: left = mid + 1 return ans arr = [7, 20, 41, 48] k = 2 print(minimum_distance(arr, k))

[7, 20, 41, 48] , 2

28

- Related Questions & Answers
- Maximum power of jump required to reach the end of string in C++
- How to find the minimum number of jumps required to reach the end of the array using C#?
- Program to Find Minimum Jumps Required to Reach a Value with Different Parity in Python
- Program to find minimum jumps to reach home in Python
- Find minimum steps required to reach the end of a matrix in C++
- C Program for Minimum number of jumps to reach the end
- Program to find maximum length of k ribbons of same length in Python
- Program to find number of minimum steps to reach last index in Python
- Program to find minimum number of hops required to reach end position in Python
- Program to find minimum number of buses required to reach final target in python
- Python - Find the length of the last word in a string
- C++ code to find minimum jump to reach home by frog
- Find the number of jumps to reach X in the number line from zero in C++
- Find maximum average subarray of k length in C++
- Check if it is possible to reach a number by making jumps of two given length in Python

Advertisements