Número palindrómico más pequeño más cercano a N

Dado un número entero N , la tarea es encontrar el número palindrómico más cercano que sea menor que N .

Ejemplos:

Entrada: N = 4000
Salida: 3993
Explicación:
3993 es el número palindrómico más cercano a N(= 4000) que también es más pequeño que N(= 4000). Por lo tanto, 3993 es la respuesta requerida.

Entrada: N = 2889
Salida: 2882

Enfoque: siga los pasos a continuación para resolver el problema:

A continuación se muestra la implementación del enfoque anterior:

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>
Producción: 

3993

 

Complejidad de tiempo: O(N * log 10 N) 
Espacio auxiliar: O(log 10 N)
 

Publicación traducida automáticamente

Artículo escrito por sallagondaavinashreddy7 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *