Join Regular Classroom : Visit ClassroomTech

Infytq Final Round Coding Question 2021

Problem:
Consider an integer innum greater than 1.

Identify and print, outnum, such that –

  • The sum of num1 and num2 is a palindrome where
    1. num1 is the largest palindrome smaller than innum
    2. num2 is the smallest palindrome greater then innum
  • If the sum is not a palindrome, repeat the above step by decrementing innum by 1 until innum is not zero

Input format:
Read innum from the standard input stream

Output format:
Print the identified palindrome to the standard output stream

Sample Input:

4

Sample Output:

8

Explanation:
num1- largest palindrome number smaller than innum (4) is 3
num2- largest palindrome number smaller than innum (4) is 5

Solution: We strongly recommend you, try the problem first before moving to the solution.

Python

#code to understand the palindrome problem of INFYTQ Final round 2021
#codewindow.in

def checkpal(n):
    if (str(n)==str(n)[::-1]):
        return True
    else:
        return False

def closestpal(n):
    low=n-1
    while(not checkpal(low)):
        low=low-1
    high=n+1
    while(not checkpal(high)):
        high=high+1

    return low,high

n=int(input())
for i in range(n,-1,-1):
    low,high=closestpal(i)
    if(checkpal(low+high)):
        print(low+high)
        break


Follow us

Also checkout