#include <bits/stdc++.h>
#define int long long int
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define INF 1e18
using namespace std;
int N, T, A[500010], mn = INF, hi = INF, mxd = -INF, mnd = INF;
// multiset<int> ms;
// once someone reaches half of T, flip them 
// use a multiset???
int32_t main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> N >> T;
    for (int i = 0; i < N; i++) {
        cin >> A[i];
        mn = min(mn, A[i]);
    }
    for (int i = 0; i < N; i++) {
        int m = (A[i] - mn) % T;
        int d = min(m, T - m);
    }
    for (int j = 0; j < N; j++) {
        int m = (A[j] - mn) % T;
        int d = m;
        if (abs(m) <= abs(T - m)) d = m;
        else d = T - m;
        mxd = max(mxd, d);
        mnd = min(mnd, d);
        // cout << d << " ";
    }
    cout << (mxd - mnd) / 2 + ((mxd - mnd) % 2 != 0) << "\n";
    // for (int j = 0; j < N; j++) {
    //     int m = (A[j] - i) % T;
    //     int d = min(abs(m), abs(T - m));
    //     mxd = max(mxd, d);
    //     ms.insert(d);
    // }
    // incr by 1 each time
    // until > half then flip
    // for (int i = mn - T / 2; i <= mn + T / 2; i++) {
    //     int mxd = -INF;
    //     for (int j = 0; j < N; j++) {
    //         int m = (A[j] - i) % T;
    //         int d = min(abs(m), abs(T - m));
    //         mxd = max(mxd, d);
    //         cout << d << " ";
    //     }
    //     cout << endl;
    //     hi = min(hi, mxd);
    // }
    // cout << hi << "\n";
    return 0;    
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |