Given a huge integer value n, find the largest integer value x such that x <= n and all the digits of x are prime.
Input : n = 45 Output : 37 37 is the largest number smaller than or equal to with all prime digits. Input : n = 1000 Output : 777 Input : n = 7721 Output : 7577 Input : n = 7221 Output : 5777
We know that the prime digits are 2, 3, 5 and 7. Also since we have to manipulate each digit of a very large number it will be easier if we do it as a string. The main idea is to find the first non-prime digit and then
find the first digit greater than 2 in its left. Now we can replace the found digit with the prime digit that is just less than it. If the digit is 2, we have to erase it and replace the next digit with 7. After this we can replace the remaining digits to its right by 7.
Following is the implementation of the above algorithm:
37 777 7577 5777 73777777777777777777777777777777777777777
The time complexity of the above program is O(N) where N is the length of the string.
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.
- Find the Largest number with given number of digits and sum of digits
- Largest number not greater than N which can become prime after rearranging its digits
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum digits to be removed to make either all digits or alternating digits same
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Print prime numbers with prime sum of digits in an array
- Sum of prime numbers without odd prime digits
- Count all prime numbers in a given range whose sum of digits is also prime
- Largest number less than N whose each digit is prime number
- Find smallest number with given number of digits and sum of digits
- Number of digits in the nth number made of given four digits
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Count of prime digits of a Number which divides the number
- Largest number smaller than or equal to n and digits in non-decreasing order
- Largest number with the given set of N digits that is divisible by 2, 3 and 5
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Largest number not greater than N all the digits of which are odd
- Find largest number smaller than N with same set of digits
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.