C++ Program to Print the Largest Possible Prime Number From a Given Number
Given an integer, the task is to find the largest prime number that can be made out of that. If we consider the integer as a string of digits, then the prime number can be a substring of any length. The examples given below will clarify the idea of the problem.
Explanation: The three numbers in the number 12691 are 269,691. The greater number is 691 and hence we print this.
- Create a string of the given number
- Compute all the substrings of the string
- Check whether any substring is prime or not
- If a substring is prime, maximize its value by comparing it with the other prime substrings
- Return the maximum value of the prime substring
Time Complexity: O(N3)
Auxiliary Space: O(1)
My Personal Notes arrow_drop_up
Please Login to comment...