Find Palindromes
You are given an integer ‘N’. Your task is to find all palindromic numbers from 1 to ‘N’.
Palindromic integers are those integers that read the same backward or forwards.
Note:
Order of numbers should be in the non-decreasing matter.
For example:
You are given ‘N’ as 12, so the output should be [1, 2, 3, 4, 5, 6, 7, 8, 9, 11], as all single-digit numbers are palindromic, and 11 is also a palindromic number.
Input Format :
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case consists of a single integer ‘N’, representing the given integer.
Output Format:
For each test case, print all the palindromic integers space-separated from 1 to ‘N’ in increasing order.
Print a separate line for each test case.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 10^9
Time Limit: 1 sec
CodingNinjas
author
2y
Brute Force
In this approach, iterate through the integers 1 to N, then check for each integer if it is a palindrome or not.
To check if an integer is a palindrome, convert the integer into a string and...read more
CodingNinjas
author
2y
Optimised Approach
In this approach, we try to create all the palindrome integers till N. To create the palindromes of odd length, we remove the last digit from the number and add the remaining digits ...read more
Help your peers!
Add answer anonymously...
Popular interview questions of SDE-2
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app