Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmSmaller palindromic number closest to N

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:

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)
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments