Lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7.
What minimum lucky number has the sum of digits equal to n.
Input : sum = 11 Output : 47 Sum of digits in 47 is 11 and 47 is the smallest number with given sum. Input : sum = 10 Output : -1
The approach is based on below facts :
- Since digits are 4 and 7 only, given digit sum can be written as a*4 + b*7 = sum where a and b are some positive integers (greater than or equal to 0) representing number of 4s and 7s respectively.
- Since we need to find minimum number, the result would always be in the form which has all 4s first, then all 7s, i.e., 44…477…7.
We basically need to find values of ‘a’ and ‘b’. We find these values using below facts:
- If sum is multiple of 4, then result has all 4s.
- If sum is multiple of 7, then result has all 7s.
- If sum is not multiple of 4 or 7, then we can subtract one of them till sum becomes multiple of other.
Time Complexity: O(sum).
This article is contributed by Amit. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Sum of the digits of square of the given number which has only 1's as its digits
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Minimum digits to be removed to make either all digits or alternating digits same
- Find smallest number with given number of digits and sum of digits under given constraints
- Smallest number with given sum of digits and sum of square of digits
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Minimum number closest to N made up of odd digits only
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Smallest multiple of a given number made of digits 0 and 9 only
- Check if the given decimal number has 0 and 1 digits only
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Minimum number of digits to be removed so that no two consecutive digits are same
- Number of digits in the nth number made of given four digits
- Print numbers with digits 0 and 1 only such that their sum is N
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count of integers of length N and value less than K such that they contain digits only from the given set
Improved By : nitin mittal