C++20 sections · 1024 units
Open in Course

Beautiful Year - Implementation

Complete solution

We increment the year and check digits until we find one with all distinct:

#include <iostream>
using namespace std;
bool hasDistinctDigits(int year) {
 int d1 = year % 10;
 int d2 = (year / 10) % 10;
 int d3 = (year / 100) % 10;
 int d4 = (year / 1000) % 10;
 return (d1 != d2 && d1 != d3 && d1 != d4 && d2 != d3 && d2 != d4 && d3 != d4);
} int main() {
 int year;
 cin >> year; year++;
 while (!hasDistinctDigits(year)) { year++;
} cout << year << endl;
return 0;
}
``` The brute force search from year+1 upward finds the answer quickly because beautiful years are never too far apart.