Topics
You are given distinct points on the number line in a sorted array . You can place at most more points on the line (integer coordinates only). You have to make the maximum separation between any two consecutive points in the final configuration as minimum as possible. Output this minimal value.
Note: You can place the points anywhere you like, but you cannot place more than one point at the same position on the line.
Input Format:
- The first line contains an integer , the number of test cases
- The first line of each test case contains two space-separated integers , , where ,
- The next line contains space-separated distinct integers ( )
- The sum of across all test cases does not exceed
Output Format:
For each test case, output the minimal maximum separation between any two consecutive points possible, in a new line.
Constraints:
- The sum of across all test cases does not exceed
Idea
This problem is a classic application of binary search on the answer space. We want to minimize the maximum difference between consecutive elements. This suggests a monotonic relationship: if a maximum difference of mid is feasible (we can achieve it with at most K added points), then any difference larger than mid is also feasible. Conversely, if mid is infeasible, then any difference smaller than mid is also infeasible. This creates the [F, F, ..., F, T, T, ..., T]
pattern suitable for the binary search boolean array framework, where true represents a feasible maximum difference.
- Answer Space: Search space is the range of possible max differences. The min possible difference is
1
(if all elements can be placed consecutively), and the max is the initial largest gap in the sorted input arrayA
check(mid, arr, k)
Function: This function determines the feasibility of a given maximum differencemid
. It determines if a given maximum difference,mid
, is achievable by adding at mostk
new points:- Iterate through the sorted array
arr
- For each adjacent pair, calculate the difference
d
- If
d > mid
, we need to insert points. Theceil
division:(d + mid - 1) / mid
gives us the total number of segments of length at mostmid
required to cover the distanced
. We know thatn+1
segments are created byn
points, so number of points required is((d + mid - 1) / mid) - 1
- Accumulate the total number of inserted points across all gaps
- Return
true
if the total number of inserted points is less than or equal tok
(feasible); otherwise, returnfalse
- Iterate through the sorted array
Time Complexity: , where is the number of elements in the input array and is the initial maximum difference (the range of our answer space). The check
function takes time, and binary search performs iterations.
Space Complexity:
Code
bool check(ll mid, vector<ll> &arr, ll k) {
ll prev = arr[0];
int n = arr.size();
ll count = 0;
for (int i = 0; i < n; ++i) {
ll d = arr[i] - prev;
if (d > mid) {
count += (d + mid - 1) / mid - 1;
}
prev = arr[i];
}
return count <= k;
}
void solve() {
int n;
ll k;
cin >> n >> k;
vector<ll> arr(n);
ll lo = 1;
ll hi = 0;
ll prev = 0;
for (int i = 0; i < n; ++i) {
cin >> arr[i];
if (i > 0)
hi = max(hi, arr[i] - prev);
prev = arr[i];
}
ll ans = hi;
while (lo <= hi) {
ll mid = lo + (hi - lo) / 2;
if (check(mid, arr, k)) {
ans = mid;
hi = mid - 1;
} else
lo = mid + 1;
}
cout << ans << endl;
}