Smaller palindromic number closest to N
Given an integer N, the task is to find the closest palindromic number which is smaller than N.
Examples:
Input: N = 4000
Output: 3993
Explanation:
3993 is the closest palindromic number to N(= 4000) which is also smaller than N(= 4000). Therefore, 3993 is the required answer.Input: N = 2889
Output: 2882
Approach: Follow the steps below to solve the problem:
- Decrement the value of N while N is not a palindromic number
- Finally, print the updated value of N which was found to be a Palindrome Number.
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to check if // a number is palindrome or not bool checkPalindrome( int N) { // Stores reverse of N int rev = 0; // Stores the value of N int temp = N; // Calculate reverse of N while (N != 0) { // Update rev rev = rev * 10 + N % 10; // Update N N = N / 10; } // Update N N = temp; // if N is equal to // rev of N if (N == rev) { return true ; } return false ; } // Function to find the closest // smaller palindromic number to N int closestSmallerPalindrome( int N) { // Calculate closest smaller // palindromic number to N do { // Update N N--; } while (N >= 0 && !checkPalindrome(N)); return N; } // Driver Code int main() { int N = 4000; cout << closestSmallerPalindrome(N); return 0; } |
Java
// Java program to implement // the above approach import java.util.*; import java.lang.Math; class GFG { // Function to check whether // a number is palindrome or not static boolean checkPalindrome( int N) { // Stores reverse of N int rev = 0 ; // Stores the value of N int temp = N; // Calculate reverse of N while (N != 0 ) { // Update rev rev = rev * 10 + N % 10 ; // Update N N = N / 10 ; } // Update N N = temp; // if N is equal to // rev of N if (N == rev) { return true ; } return false ; } // Function to find the closest // smaller palindromic number to N static int closestSmallerPalindrome( int N) { // Calculate closest smaller // palindromic number to N do { // Update N N--; } while (N >= 0 && !checkPalindrome(N)); return N; } // Driver Code public static void main(String[] args) { int N = 4000 ; System.out.println( closestSmallerPalindrome(N)); } } |
Python3
# Python3 program to implement # the above approach # Function to check if # a number is palindrome or not def checkPalindrome(N): # Stores reverse of N rev = 0 # Stores the value of N temp = N # Calculate reverse of N while (N ! = 0 ): # Update rev rev = rev * 10 + N % 10 # Update N N = N / / 10 # Update N N = temp # If N is equal to # rev of N if (N = = rev): return True return False # Function to find the closest # smaller palindromic number to N def closestSmallerPalindrome(N): # Calculate closest smaller # palindromic number to N while N > = 0 and not checkPalindrome(N): # Update N N - = 1 return N # Driver Code if __name__ = = '__main__' : N = 4000 print (closestSmallerPalindrome(N)) # This code is contributed by mohit kumar 29 |
C#
// C# program to implement // the above approach using System; class GFG{ // Function to check whether // a number is palindrome or not static bool checkPalindrome( int N) { // Stores reverse of N int rev = 0; // Stores the value of N int temp = N; // Calculate reverse of N while (N != 0) { // Update rev rev = rev * 10 + N % 10; // Update N N = N / 10; } // Update N N = temp; // If N is equal to // rev of N if (N == rev) { return true ; } return false ; } // Function to find the closest // smaller palindromic number to N static int closestSmallerPalindrome( int N) { // Calculate closest smaller // palindromic number to N do { // Update N N--; } while (N >= 0 && !checkPalindrome(N)); return N; } // Driver Code public static void Main() { int N = 4000; Console.WriteLine(closestSmallerPalindrome(N)); } } // This code is contributed by bgangwar59 |
Javascript
<script> // Javascript program to implement // the above approach // Function to check whether // a number is palindrome or not function checkPalindrome(N) { // Stores reverse of N let rev = 0; // Stores the value of N let temp = N; // Calculate reverse of N while (N != 0) { // Update rev rev = Math.floor(rev * 10 + N % 10); // Update N N = Math.floor( N / 10); } // Update N N = temp; // if N is equal to // rev of N if (N == rev) { return true ; } return false ; } // Function to find the closest // smaller palindromic number to N function closestSmallerPalindrome(N) { // Calculate closest smaller // palindromic number to N do { // Update N N--; } while (N >= 0 && !checkPalindrome(N)); return N; } // Driver Code let N = 4000; document.write(closestSmallerPalindrome(N)); </script> |
Output:
3993
Time Complexity: O(N * log10N)
Auxiliary Space: O(log10N)