forked from SRIJONDEY/ProblemSolve_cf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path271ABeautifulYear.cpp
More file actions
66 lines (53 loc) · 1.19 KB
/
Copy path271ABeautifulYear.cpp
File metadata and controls
66 lines (53 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using namespace std;
// Function to check if all digits are distinct
bool hasDistinctDigits(int year)
{
bool seen[10] = {false}; // For digits 0-9
while (year > 0)
{
int digit = year % 10;
if (seen[digit])
return false; // Digit already seen
seen[digit] = true;
year /= 10;
}
return true;
}
int main()
{
int y;
cin >> y;
while (true)
{
y++; // Go to next year
if (hasDistinctDigits(y))
{
cout << y << endl;
break;
}
}
return 0;
}
/*📦 Full Example with year = 2013
year = 2013
→ last digit: 3
→ seen[3] = false → mark seen[3] = true
→ year = 201
year = 201
→ last digit: 1
→ seen[1] = false → mark seen[1] = true
→ year = 20
year = 20
→ last digit: 0
→ seen[0] = false → mark seen[0] = true
→ year = 2
year = 2
→ last digit: 2
→ seen[2] = false → mark seen[2] = true
→ year = 0
✅ All digits are unique → function returns true.
🛑 If year = 1990
digit = 0 → mark seen
digit = 9 → mark seen
digit = 9 → already seen → return false*/