top of page
Search

Day 5 | Beautiful Year | C++

  • Writer: Ayush Mahajan
    Ayush Mahajan
  • Aug 28, 2022
  • 2 min read

5th question is halfway to entering double digits Today we are solving - Beautiful Year: https://codeforces.com/contest/271/problem/A


In my opinion, this question is simple, because even the largest input year can be 9000, and for that 9012 can be the answer. Thus I can loop over every year above the input year and check if have unique digits. As this question is easy, let me teach you here different implementations.


Method 1 We run an infinite loop checking one by one if digits are different or not in each case. What we can do is create a function to check if a number has unique digits or not. If you don't know about functions, then you can learn about them here: https://w3schools.com/cpp/cpp_functions.asp


In this method, we use a frequency array to see if this digit occurred earlier or not. And if this digit occurred earlier then of course the digits are not unique. You can be confused about how to split numbers into digits. You can refer here:


I implemented my approach here: https://codeforces.com/contest/271/submission/169438830… If you have any issues with the code, do reply here and I will try my best to answer your question.


Method 2 This is just to write code in a cooler manner using a bitmask. You really don't need to understand it right now. It does not give any benefit in this question and we won't be discussing bitmasking right now as well. It's just in case you want to explore more stuff.


Thinking about using bitmask is that we don't need frequency as integer but only that it appeared before or not. And also there are only 9 digits, thus only 9 bits are required. You can find implementations: https://codeforces.com/contest/271/submission/169439709


There was a compilation error as well.

 
 
 

Comments


bottom of page