Given a number N, the task is to find the largest number less than or equal to the given number N such that on rearranging its digits it can become prime.
Input : N = 99 Output : 98 Explanation : We can rearrange the digits of 98 to 89 and 89 is a prime number. Input : N = 84896 Output : 84896 Explanation : We can rearrange the digits of 84896 to 46889 which is a prime number.
Below is the algorithm to find such a largest number num <= N such that digits of num can be rearranged to get a prime number:
Preprocessing Step: Generate a list of all the prime numbers less than or equal to given number N. This can be done efficiently using the sieve of Eratosthenes.
Main Steps: The main idea is to check all numbers from N to 1, if any of the number can be reshuffled to form a prime. The first such number found will be the answer.
To do this, run a loop from N to 1 and for every number:
- Extract the digits of the given number and store it in a vector.
- Sort this vector to get the smallest number which can be formed using these digits.
- For each permutation of this vector, we would form a number and check whether the formed number is prime or not. Here we make use of the Preprocessing step.
- If it is prime then we stop the loop and this is our answer.
Below is the implementation of the above approach:
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.
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Largest number not greater than N all the digits of which are odd
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Find the element before which all the elements are smaller than it, and after which all are greater
- Largest number less than N with digit sum greater than the digit sum of N
- Smallest number by rearranging digits of a given number
- Count of Array elements greater than all elements on its left and next K elements on its right
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Smallest subarray of size greater than K with sum greater than a given value
- Smallest Special Prime which is greater than or equal to a given number
- Sum of the digits of square of the given number which has only 1's as its digits
- Largest even digit number not greater than N
- Smallest number greater than or equal to N having sum of digits not exceeding S
- Count of array elements which are greater than all elements on its left
- Reduce the array by deleting elements which are greater than all elements to its left
- Delete array elements which are smaller than next or become smaller
- Maximize number of groups formed with size not smaller than its largest element
- Find Largest Special Prime which is less than or equal to a given number
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
Improved By : SURENDRA_GANGWAR