#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Function to check if it is feasible to have a distance `dist` between chosen integers
bool is_feasible(const vector<int>& starts, int d, int n, int dist) {
int current = starts[0];
for (int i = 1; i < n; ++i) {
int next_start = current + dist;
// Check if there is a valid number in the next range
if (next_start > starts[i] + d) {
return false;
}
// Update the current chosen number to the valid next_start
current = max(next_start, starts[i]);
}
return true;
}
// Function to find the maximum possible value of `dist`
int max_dist(vector<int>& starts, int d) {
int n = starts.size();
// Sort the starts array
sort(starts.begin(), starts.end());
// Initialize binary search bounds
int low = 0, high = *max_element(starts.begin(), starts.end()) + d - *min_element(starts.begin(), starts.end());
// Binary search to find the maximum dist
while (low < high) {
int mid = (low + high + 1) / 2;
if (is_feasible(starts, d, n, mid)) {
low = mid;
} else {
high = mid - 1;
}
}
return low;
}
// Main function
int main() {
int n = 3;
int d = 2;
vector<int> starts = {3, 2, 3};
cout << max_dist(starts, d) << endl; // Output: 1
return 0;
}