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.
Let be the smallest array value. Since modulo never increases a number, any final common value is at most .
For every , the answer is automatically possible: choose for each element and send everything to .
If , the smallest element cannot change at all, because every allowed modulus is larger than it. Therefore the only possible final value is exactly .
For another value to become , the final operation must use some modulus with . Then , so .
Only the second smallest value can be the bottleneck. After sorting, the answer is .
Let the smallest value be
Modulo operations never increase a number. So whatever common value we finish with, it cannot be bigger than .
That gives us the first important baseline.
For any
we can make every element equal to : for each , choose
This is allowed because , and
So the answer is at least . Free points. Take them.
What changes when ?
If , then the smallest element is stuck forever. Every allowed modulus satisfies , so
Therefore, if , the final common value must be exactly .
Now every other value must be reducible to .
Look at the final operation that turns this element into . Suppose the value right before that operation is . Since values only decrease,
For some modulus , we need
That means
for some positive integer , so
and in particular
Since , we get the necessary condition
This has to hold for every non-minimum element. The tightest one is the second smallest element.
After sorting:
So any answer bigger than must satisfy
Can we achieve that upper bound? Yes, if it is actually bigger than .
Set
For every other value , we have
Choose
Because , this modulus is larger than , and
So every element becomes , while the minimum stays .
If , then we cannot beat the baseline , but still works by sending everything to .
Therefore the answer is
Sort the array, read the two smallest numbers, print that value. Number theory showed up, made one divisibility observation, and left before things got annoying.
Complexity: sorting costs per test case, and the total is bounded by , so this easily fits.
#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);
for (ll &x : a) cin >> x;
sort(a.begin(), a.end());
cout << max(a[0], a[1] - a[0]) << '\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);
for (ll &x : a) cin >> x;
sort(a.begin(), a.end());
cout << max(a[0], a[1] - a[0]) << '\n';
}
return 0;
}