Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
If the array is already sorted, be careful: every works because you do zero swaps, so there is no largest answer. That is exactly why the answer is .
For a fixed , think of each occurrence as a token. Two tokens can be swapped directly if their values differ by at least . Direct swaps along edges generate arbitrary permutations inside each connected component.
The value graph has a very special shape: an edge means “far apart.” For , the minimum and maximum values can swap with each other, and they are the useful bridge values.
Any value satisfying can swap with the minimum. Any value satisfying can swap with the maximum. So is movable through the big connected component if at least one of those is true.
A value is stuck outside the movable component only when both and . Therefore every index where differs from the sorted array must have a value in the movable component. The largest valid is the minimum of and over misplaced values, plus the global range cap.
Let be the sorted version of , and let
If is already sorted, the answer is . This is not a typo or sample weirdness: with zero operations, the array is sorted for every integer , so there is no largest piggy integer. Infinite answers, no maximum. Annoying, but fair.
Now assume is not sorted.
For a fixed , model each occurrence of a value as a token. Two tokens can be swapped directly if their values differ by at least . Since swaps are arbitrary by position, the only thing that matters is the graph on values/occurrences where edges connect pairs with difference at least .
A standard fact: transpositions along edges of a connected graph generate every permutation inside that connected component. So any value occurrence inside the same connected component can be rearranged however we want. Values outside that component are stuck with only whatever their own component can do.
The key is that this graph is very structured. Edges connect values that are far apart. If
then and are connected by an edge. They form the “main movable component.” A value belongs to this component if it can connect to either endpoint:
Equivalently, is not in the movable component exactly when
Those middle-ish values are too close to both extremes, so they cannot swap with anything useful. They are basically glued in place. Tough luck.
Now compare with . If , index is already fine; we do not need to move that occurrence. If , then the value currently at index must leave this position during sorting. That is possible only if is in the movable component.
So for every misplaced value , we need:
That is the same as:
Also, because the array is not already sorted, we need at least one real swap, so cannot exceed ; otherwise no two different values can ever be swapped. This is already covered by the same formula for extremes, but keeping it explicit makes the logic clean.
Therefore the largest valid is:
Why is this sufficient? For that , every misplaced value belongs to the main component containing and . Since all positions that need changes contain movable tokens, we can permute those movable occurrences arbitrarily. Fixed positions where can simply be left alone. Thus we can place the needed sorted values into all mismatched positions and obtain .
Edge cases:
int technically works for differences, but long long is harmless and avoids dumb overflow drama.Complexity per test case is dominated by sorting:
with extra memory.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<ll> a(n), b;
for (ll &x : a) cin >> x;
b = a;
sort(b.begin(), b.end());
if (a == b) {
cout << -1 << '\n';
continue;
}
ll mn = b.front(), mx = b.back();
ll ans = mx - mn;
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
ans = min(ans, max(a[i] - mn, mx - a[i]));
}
}
cout << ans << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<ll> a(n), b;
for (ll &x : a) cin >> x;
b = a;
sort(b.begin(), b.end());
if (a == b) {
cout << -1 << '\n';
continue;
}
ll mn = b.front(), mx = b.back();
ll ans = mx - mn;
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
ans = min(ans, max(a[i] - mn, mx - a[i]));
}
}
cout << ans << '\n';
}
return 0;
}