Given two positive integers L and R (represented as strings) where . The task is to find the total number of super-palindromes in the inclusive range [L, R].
A palindrome is called super-palindrome if it is a palindrome and also square of a palindrome.
Input: L = "4", R = "1000" Output: 4 Explanation: 4, 9, 121, and 484 are super-palindromes. Input : L = "100000", R = "10000000000" Output : 11
Lets say is a super-palindrome.
Now since R is a palindrome, the first half of the digits of R can be used to determine R up-to two possibilities. Let i be the first half of the digits in R. For eg. if i = 123, then R = 12321 or R = 123321. Thus we can iterate through these all these digits. Also each possibility can have either odd or even number of digits in R.
Thus we iterate through each i upto 105 and create the associated palindrome R, and check whether R2 is a palindrome.
Also we will handle the odd and even palindromes separately, and break whenever out palindrome goes beyond R.
Now since , and and (on Concatenation), where i‘ is reverse of i (in both ways), so our LIMIT will not be greater than .
Below is the implementation of above approach:
Time Complexity: O(N*log(N)) where N is upper limit and the log(N) term comes from checking if a candidate is palindrome.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count square and non-square numbers before n
- Check if a number is a perfect square having all its digits as a perfect square
- Count numbers upto N which are both perfect square and perfect cube
- Count of elements which is product of a pair or an element square
- Count of three non-overlapping sub-strings which on concatenation forms a palindrome
- Check if a number is perfect square without finding square root
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Find smallest perfect square number A such that N + A is also a perfect square number
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- N-th number which is both a square and a cube
- Area of a square inscribed in a circle which is inscribed in an equilateral triangle
- Area of a square inscribed in a circle which is inscribed in a hexagon
- Largest factor of a given number which is a perfect square
- Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
- Biggest Reuleaux Triangle within a Square which is inscribed within a Circle
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within a hexagon
- Find the side of the squares which are lined in a row, and distance between the centers of first and last square is given
- Sum of the digits of square of the given number which has only 1's as its digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Harshit Saini