Skip to content
Related Articles
Minimum number with digits as 4 and 7 only and given sum
• Difficulty Level : Easy
• Last Updated : 22 Apr, 2021

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.

Examples:

```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 :

1. 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.
2. 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:

1. If sum is multiple of 4, then result has all 4s.
2. If sum is multiple of 7, then result has all 7s.
3. If sum is not multiple of 4 or 7, then we can subtract one of them till sum becomes multiple of other.

## C++

 `// C++ program to find smallest number``// with given sum of digits.``#include ``using` `namespace` `std;` `// Prints minimum number with given digit``// sum and only allowed digits as 4 and 7.``void` `findMin(``int` `sum)``{``    ``int` `a = 0, b = 0;``    ``while` `(sum > 0)``    ``{``        ``// Cases where all remaining digits``        ``// are 4 or 7 (Remaining sum of digits``        ``// should be multiple of 4 or 7)``        ``if` `(sum % 7 == 0)``        ``{``            ``b++;``            ``sum -= 7;``        ``}``        ``else` `if` `(sum % 4 == 0)``        ``{``            ``a++;``            ``sum -= 4;``        ``}` `        ``// If both 4s and 7s are there``        ``// in digit sum, we subtract a 4.``        ``else``        ``{``            ``a++;``            ``sum -= 4;``        ``}``    ``}` `    ``if` `(sum < 0)``    ``{``        ``printf``(``"-1n"``);``        ``return``;``    ``}` `    ``for` `(``int` `i=0; i

## Java

 `// Java program to find smallest number``// with given sum of digits.``import` `java.io.*;` `class` `GFG {``    ` `    ``// Prints minimum number with given digit``    ``// sum and only allowed digits as 4 and 7.``    ``static` `void` `findMin(``int` `sum)``    ``{``        ``int` `a = ``0``, b = ``0``;``        ``while` `(sum > ``0``)``        ``{``            ``// Cases where all remaining digits``            ``// are 4 or 7 (Remaining sum of digits``            ``// should be multiple of 4 or 7)``            ``if` `(sum % ``7` `== ``0``)``            ``{``                ``b++;``                ``sum -= ``7``;``            ``}``            ``else` `if` `(sum % ``4` `== ``0``)``            ``{``                ``a++;``                ``sum -= ``4``;``            ``}``    ` `            ``// If both 4s and 7s are there``            ``// in digit sum, we subtract a 4.``            ``else``            ``{``                ``a++;``                ``sum -= ``4``;``            ``}``        ``}``    ` `        ``if` `(sum < ``0``)``        ``{``            ``System.out.print(``"-1n"``);``            ``return``;``        ``}``    ` `        ``for` `(``int` `i = ``0``; i < a; i++)``            ``System.out.print(``"4"``);``            ` `        ``for` `(``int` `i = ``0``; i < b; i++)``            ``System.out.print(``"7"``);``            ` `        ``System.out.println();``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `main(String args[])``                            ``throws` `IOException``    ``{``        ``findMin(``15``);``    ``}``}` `/* This code is contributed by Nikita tiwari.*/`

## Python

 `# Python program to find smallest number``# with given sum of digits.` `# Prints minimum number with given digit``# sum and only allowed digits as 4 and 7.``def` `findMin(s):``    ``a, b ``=` `0``, ``0``    ``while` `(s > ``0``):``        ` `        ``# Cases where all remaining digits``        ``# are 4 or 7 (Remaining sum of digits``        ``# should be multiple of 4 or 7)``        ``if` `(s ``%` `7` `=``=` `0``):``            ``b ``+``=` `1``            ``s ``-``=` `7``        ``elif` `(s ``%` `4` `=``=` `0``):``            ``a ``+``=` `1``            ``s ``-``=` `4` `        ``# If both 4s and 7s are there``        ``# in digit sum, we subtract a 4.``        ``else``:``            ``a ``+``=` `1``            ``s ``-``=` `4` `    ``string ``=` `""``    ``if` `(s < ``0``):``        ``string ``=` `"-1"``        ``return` `string``    ` `    ` `    ``string ``+``=` `"4"` `*` `a``    ``string ``+``=` `"7"` `*` `b``    ` `    ``return` `string` `# Driver code``print` `findMin(``15``)` `# This code is contributed by Sachin Bisht`

## C#

 `// C# program to find smallest number``// with given sum of digits.``using` `System;` `class` `GFG {``    ` `    ``// Prints minimum number with given digit``    ``// sum and only allowed digits as 4 and 7.``    ``static` `void` `findMin(``int` `sum)``    ``{``        ``int` `a = 0, b = 0;``        ``while` `(sum > 0)``        ``{``            ` `            ``// Cases where all remaining digits``            ``// are 4 or 7 (Remaining sum of digits``            ``// should be multiple of 4 or 7)``            ``if` `(sum % 7 == 0)``            ``{``                ``b++;``                ``sum -= 7;``            ``}``            ``else` `if` `(sum % 4 == 0)``            ``{``                ``a++;``                ``sum -= 4;``            ``}``    ` `            ``// If both 4s and 7s are there``            ``// in digit sum, we subtract a 4.``            ``else``            ``{``                ``a++;``                ``sum -= 4;``            ``}``        ``}``    ` `        ``if` `(sum < 0)``        ``{``            ``Console.Write(``"-1n"``);``            ``return``;``        ``}``    ` `        ``for` `(``int` `i = 0; i < a; i++)``            ``Console.Write(``"4"``);``            ` `        ``for` `(``int` `i = 0; i < b; i++)``            ``Console.Write(``"7"``);``            ` `        ``Console.WriteLine();``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``        ``findMin(15);``    ``}``}` `// This code is contributed by Nitin Mittal.`

## PHP

 ` 0)``    ``{``        ` `        ``// Cases where all remaining digits``        ``// are 4 or 7 (Remaining sum of digits``        ``// should be multiple of 4 or 7)``        ``if` `(``\$sum` `% 7 == 0)``        ``{``            ``\$b``++;``            ``\$sum` `-= 7;``        ``}``        ``else` `if` `(``\$sum` `% 4 == 0)``        ``{``            ``\$a``++;``            ``\$sum` `-= 4;``        ``}` `        ``// If both 4s and 7s are there``        ``// in digit sum, we subtract a 4.``        ``else``        ``{``            ``\$a``++;``            ``\$sum` `-= 4;``        ``}``    ``}` `    ``if` `(``\$sum` `< 0)``    ``{``        ``echo``(``"-1n"``);``        ``return``;``    ``}` `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$a``; ``\$i``++)``        ``echo``(``"4"``);` `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$b``; ``\$i``++)``        ``echo``(``"7"``);` `    ``echo``(``"\n"``);``}` `    ``// Driver code``    ``findMin(15);``    ` `// This code is contributed by nitin mittal``?>`

## Javascript

 ``

Output:

`447`

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 contribute@geeksforgeeks.org. 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 mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up