Last Updated: 3 Nov, 2025

Minimize Maximum Adjacent Distance

Moderate
Asked in company
Razorpay

Problem statement

You are given the positions of N cubes on a 1D line, represented by an array X. You are allowed to remove at most K cubes from this line.


Your goal is to find a configuration of remaining cubes that minimizes the maximum distance between any two adjacent cubes.


For example, if the remaining cubes are at positions [1, 5, 12], the adjacent distances are (5-1)=4 and (12-5)=7. The maximum adjacent distance is 7.


You need to find the minimum possible value for this maximum adjacent distance.


Input Format:
The first line contains two space-separated integers, N (the number of cubes) and K (the maximum number of cubes you can remove).

The second line contains N space-separated integers, representing the positions of the cubes in array X.


Output Format:
Print a single integer representing the minimum possible maximum distance between any two adjacent remaining cubes.


Note:
The problem asks for the "minimum of a maximum," which is a strong indicator for Binary Search.