VOOZH about

URL: https://www.geeksforgeeks.org/dsa/finding-largest-palindromic-number-divisible-by-smallest-prime/

⇱ Finding largest palindromic number divisible by smallest prime - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Finding largest palindromic number divisible by smallest prime

Last Updated : 26 Sep, 2023

Given a range [start, end], find the largest palindromic number in the range and check if it is divisible by the smallest prime number in the range.

Examples:

Input: start = 1, end = 500
Output: true
Explanation: The largest palindromic number in the range is 484, which is divisible by the smallest prime number 2 in the range.

Input: start = 50, end = 1000
Output: false
Explanation: The largest palindromic number in the range is 999, which is not divisible by the smallest prime number 53 in the range.

Input: start = 1, end = 10
Output: false
Explanation: The largest palindromic number in the range is 9, which is not divisible by the smallest prime number 2 in the range.

Approach: This can be solved with the following idea:

  • Start iterating from the end toward the start of the given range.
  • For each number, check if it is a palindrome. If yes, store it as the largest palindrome number found so far and break the loop.
  • Find the smallest prime number in the range.
  •  Check if the largest palindrome number found in step 2 is divisible by the smallest prime number found in step 3. 
  •  Return the result.

Below are the steps for the above approach :

  • Define a function isPalindrome to check if a given number is a palindrome or not.
  • Define a function smallestPrimeInRange to find the smallest prime number in a given range.
  • Define a function largestPalindromicDivisibleBySmallestPrime to implement the above approach.

Below is the code for the above approach :


Output
True
False

Time Complexity: O(N2)
Auxiliary Space: O(1)

Comment
Article Tags: