Given an input 'n' from the user, you need to find all lucky numbers from 1 to n. Lucky numbers are those which have either 4 or 7 or both as their digits. What is the most efficient method to solve this problem?
-
Is there any better method other than iterating the range and checking each digit of every number and classifying them as lucky or not? This method will have the time complexity O(nk) where n is upper limit of the range. k is the number of digits of largest number.
-
Answer:
Here is the editorial of this problem http://www.codechef.com/MARCH12/problems/LUCKY2/ Note : In above case l can be replace by 1 and r = n. http://discuss.codechef.com/questions/3865/lucky2-editorial
Shravan Kumar at Quora Visit the source
Other answers
Theoretically, you could create a compile time table if upper bound of n is known. Each item of the table holds lucky numbers seen till then. Index in to the table with input number.
Pratik Ashar
The other solution can be: Step 1- count the number of digits of 'n'. Say it is 'd'. Step 2- now for i=1 to d, generate the numbers from 0 to (2^i) -1 in binary and replace 0 with 4 and 1 with 7. Also if(i>=1 and i<= d-1) then print the numbers. and when i=d, check whether the number is smaller than n, then print it else ignore.
Shefali Bansal
Let X be any lucky number, then you can generate another two lucky numbers from X. i.e. "10*X+4" and "10*X+7"..!! You can do it anyway, but here is the recursive solution..!! I hope this will help... Link To The Solution: http://paste.ubuntu.com/7632294/ (Solution by kakajan on codeforces) Codeforces Blog: http://212.193.33.25/blog/entry/12659
Swapnil Walke
Related Q & A:
- How do I permanently block a user forever? I mean, find or enter the username, when the user isnt on my list?Best solution by Yahoo! Answers
- I need to find the exact location of the factory amplifier in a 2004 Porsche Cayenne.Best solution by Yahoo! Answers
- How many tickets can I get selecting 6 numbers from 1 to 40 for each play?Best solution by Yahoo! Answers
- I need to find the name of a commercial.Best solution by Yahoo! Answers
- Need to find An Online advertisement?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.