Given a very large number N (1 <= number of digit in N <= 105). The task is find the largest number X such that X < N and each digit of X is prime number.
Input : N = 1000 Output : 777 777 is the largest number less than 1000 which have each digit as prime. Input : N = 11 Output : 7
The idea is to traverse from leftmost digit of the number N to rightmost digit of N. Check if the current digit is prime or not. If it is prime, copy the digit to output number at corresponding digit position. If it is not prime, copy the largest prime number less than current digit.
Now consider if the current digit is ‘0’ or ‘1’. In that case copy ‘7’ to current digit position of output number. Also move to adjacent left digit of current digit and reduce it to largest prime number less than it.
Once we reduce any digit to largest prime number less than the digit, we copy ‘7’ to rest of the right digit in the output number.
Below is C++ implementation of this approach:
This article is contributed by Anuj Chauhan (anuj0503). 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.
- Largest number less than N with digit sum greater than the digit sum of N
- Check if frequency of each digit is less than the digit
- Sum of largest prime factor of each number less than equal to n
- Check if the first and last digit of number N is prime and their sum is less than K
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M
- Number of triplets such that each value is less than N and each pair sum is a multiple of K
- Find Largest Special Prime which is less than or equal to a given number
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Largest number less than or equal to N/2 which is coprime to N
- Largest number less than or equal to Z that leaves a remainder X when divided by Y
- Largest number M less than N such that XOR of M and N is even
- Nearest prime less than given number n
- Count pairs with sum as a prime number and less than n
- Print all Prime Quadruplet of a number less than it
- Number less than equals to N with maximum product of prime factors
- Count of N-digit numbers having digit XOR as single digit