Count numbers from a range whose cube is a Palindrome | Java program of the above approach ; Function to check if n is a pallindrome number or not ; Temporarily store n ; Stores reverse of n ; Iterate until temp reduces to 0 ; Extract the last digit ; Add to the start ; Remove the last digit ; If the number and its reverse are equal ; Otherwise ; Function to precompute and store the count of numbers whose cube is a palindrome number ; Iterate upto 10 ^ 4 ; Check if i * i * i is a pallindrome or not ; Convert arr [ ] to prefix sum array ; Driver Code ; Given queries ; Using inclusion - exclusion principle , count required numbers
import java . io . * ; import java . util . * ; public class Pair { private final int key ; private final int value ; public Pair ( int aKey , int aValue ) { key = aKey ; value = aValue ; } public int key ( ) { return key ; } public int value ( ) { return value ; } } class GFG { static int [ ] arr = new int [ 10005 ] ; static int isPalindrome ( int n ) { int temp = n ; int res = 0 ; while ( temp != 0 ) { int rem = temp % 10 ; res = res * 10 + rem ; temp /= 10 ; } if ( res == n ) { return 1 ; } else return 0 ; } static void precompute ( ) { for ( int i = 1 ; i <= 10000 ; i ++ ) { if ( isPalindrome ( i * i * i ) != 0 ) arr [ i ] = 1 ; else arr [ i ] = 0 ; } for ( int i = 1 ; i <= 10000 ; i ++ ) { arr [ i ] = arr [ i ] + arr [ i - 1 ] ; } } public static void main ( String [ ] args ) { ArrayList < Pair > Q = new ArrayList < Pair > ( ) ; Pair pair = new Pair ( 2 , 7 ) ; Q . add ( pair ) ; Pair pair2 = new Pair ( 10 , 25 ) ; Q . add ( pair2 ) ; precompute ( ) ; for ( int i = 0 ; i < Q . size ( ) ; i ++ ) { System . out . println ( arr [ Q . get ( i ) . value ( ) ] - arr [ Q . get ( i ) . key ( ) - 1 ] ) ; } } }