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.
Try tiny values first. If , you do not need to build some fancy number. A one-digit answer can already share a digit.
Any decimal digit appearing in can be used directly as . For example, if contains digit , then works.
Since every digit is between and , any valid one-digit answer is automatically smaller than every multi-digit answer. So the best answer will be a single digit.
Among all digits that appear in , choosing the smallest digit gives the smallest possible .
The answer is just the minimum digit in the decimal representation of . Remember: is non-negative, so if contains digit , the answer is .
We need the smallest non-negative integer such that and share at least one digit.
The important thing is to not overcomplicate this. Since can be any non-negative integer, it can be a single digit: .
If a digit appears in , then choosing immediately works, because 's representation contains exactly that digit .
For example:
Key observation: the answer is always one digit.
Why? Because has at least one digit. Pick any digit from , call it . Then is valid and . So there is always a valid answer at most .
Any number with two or more digits is at least , so it can never beat a valid one-digit answer. Therefore, we only care about digits already inside .
So the smallest valid is simply:
Implementation is straightforward:
ans to something large, like 9 or 10.x % 10.ans with the minimum digit seen.x /= 10.ans.Because , this is basically instant. The complexity per test case is , which is at most here. So yeah, the computer is not even waking up for this one.
#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 x;
cin >> x;
int ans = 9;
while (x > 0) {
ans = min(ans, x % 10);
x /= 10;
}
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 x;
cin >> x;
int ans = 9;
while (x > 0) {
ans = min(ans, x % 10);
x /= 10;
}
cout << ans << '\n';
}
return 0;
}