Day 8 | Nearest Lucky Number | C++
- Ayush Mahajan
- Aug 28, 2022
- 2 min read
Today we are solving into question which is a very good example of problem-solving. We will take the first step into breaking problems into parts and then solve them. The problem is Nearly Lucky Number - https://codeforces.com/contest/110/problem/A… Don't forget to try it yourself first.
Remember, the best way to solve a problem is to
Read it
Take a breathe
Confirm you understood it correctly by looking at the test case (or discussing samples with the interviewer in case of an interview)
Plan how the solution in the smallest possible steps.
Implement the solution.
What is the problem asking? - If Input is a Nearly Lucky Number.
What is Nearly Lucky Number - A number in which the count of lucky digits is a lucky number. What are lucky digits - 4 and 7
What is a lucky number - A number with only 4 and 7
Now, the solution is simple
Count Number of Lucky Digits in Input
Check if that count is a Lucky Number or not.
To count, we need to split the number into digits and count the 4 and 7 frequencies. To check the lucky number, we do the same and check if all numbers are 4 and 7
If you are following my threads, then you must remember this problem we solved, Beautiful Year We will use the same concept again in splitting the integers.
Today before directly going into correct implementation, let me guide you through how you can basically debug if you are not getting answers. For example, here we are getting a WA (Wrong Answer) https://codeforces.com/contest/110/submission/169702431…
First, we will start to add cout statements (I call these debug logs) like here https://paste.ubuntu.com/p/hvcmBWpMH5/ Then you can try different inputs to check if it is going through as expected. When I put in 7th TC (We are practicing only, it's okay) I noticed, that my N is not the same.
Since I had enough experience already dealing with it, I can tell you the mistake was not reading the constraints(limits) of the input. N could be up to 10^18 And int on code forces is 32 bits. 2^32 is only about 2*(10^9) Thus I had to use 64-bit integer - long long, int64_t
When I used that, it failed on TC 3. I must have passed earlier because for some reason but using logs I could see a count of 0 lucky digits passes by code, hence I needed to add it as an edge case and got an AC - https://codeforces.com/contest/110/submission/169704263
[Serious Advice Time] Did you notice how today we used what we learned earlier? This is how you improve in competitive programming. You keep solving and keep picking up stuff and then use it as tricks or skills. This small skill by skill you will become better and that's how it mostly is.
![Day 44 | Binary Search Practice [Leetcode] | C++](https://static.wixstatic.com/media/fbea2a_167f1b66234f4d30b91bcf5b517f83f3~mv2.jpg/v1/fill/w_980,h_490,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/fbea2a_167f1b66234f4d30b91bcf5b517f83f3~mv2.jpg)
![Day 43 | Graph and Tree Practice [Leetcode] II | C++](https://static.wixstatic.com/media/fbea2a_361d6f7cd90c431b9f25518e1503da94~mv2.jpg/v1/fill/w_980,h_490,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/fbea2a_361d6f7cd90c431b9f25518e1503da94~mv2.jpg)
![Day 42 | Graph Practice [LeetCode] | C++](https://static.wixstatic.com/media/fbea2a_6570bb28577149f19db06704d542789a~mv2.jpg/v1/fill/w_980,h_490,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/fbea2a_6570bb28577149f19db06704d542789a~mv2.jpg)
Comments