Task
Input - integer number (year)
Print one number - the next year, in which all digits are pairwise different, and there are no digits 2 and 0. If there will never be such a year, print -1
.
My solution (correct)
Could you please recommend time / space complexity optimizations?
vector<char> toVec(int n) {
vector<char> res;
while (n > 0) {
res.push_back((n % 10) + '0');
n /= 10;
}
return res;
}
int main(){
int year;
cin >> year;
for (int i = year + 1; i < pow(10, 8); ++i) {
vector<char> v = toVec(i);
if (find(begin(v), end(v), '2') == v.end() and
find(begin(v), end(v), '0') == v.end()) {
set<char> temp(v.begin(), v.end());
if (v.size() == temp.size()) {
cout << i;
return 0;
}
}
}
cout << -1;
}
i < pow(10, 8)
to the equivalent ofi < 100000000
, but I agree you shouldn't rely on that. \$\endgroup\$