Count numbers from a range whose cube is a Palindrome
Given an array Q[][] consisting of N queries of the form {L, R}, the task for each query is to find the total count of numbers from the range [L, R], whose cube is a palindrome.
Examples:
Input: Q[][] = {{2, 10}, {10, 20}}
Output:
2
1
Explanation:
Query 1: The numbers from the range [2, 10], whose cube is a palindrome are 2, 7
Query 2: The only number from the range [10, 20], whose cube is a palindrome is 11Input: Q[][] = {{1, 50}, {13, 15}}
Output:
4
0
Explanation:
Query 1: The numbers from the range [1, 50], whose cube is a palindrome are {1, 2, 7, 11}.
Query 2: No such number exists in the range [13, 15].
Approach: The simplest idea to solve the problem is to use Inclusion-Exclusion Principle and Prefix Sum Array technique to solve this problem. Follow the steps below to solve the given problem:
- Initialize an array arr[], to store at every ith index, whether cube of i is a palindrome or not.
- Traverse the array arr[] and for every ith index, check if the cube of i is a palindrome or not.
- If found to be true, then set arr[i] = 1.
- Otherwise, set arr[i] = 0.
- Convert the array arr[] to a prefix sum array.
- Traverse the array Q[][], and count the number from the range [L, R] whose cube is a palindrome by calculating arr[R] – arr[L-1].
Below is the implementation of the above approach.
C++
// C++ program of the above approach#include <bits/stdc++.h>using namespace std;int arr[10005];// Function to check if n is// a pallindrome number or notint isPalindrome(int n){ // Temporarily store n int temp = n; // Stores reverse of n int res = 0; // Iterate until temp reduces to 0 while (temp != 0) { // Extract the last digit int rem = temp % 10; // Add to the start res = res * 10 + rem; // Remove the last digit temp /= 10; } // If the number and its // reverse are equal if (res == n) { return 1; } // Otherwise else return 0;}// Function to precompute and store// the count of numbers whose cube// is a palindrome numbervoid precompute(){ // Iterate upto 10^4 for (int i = 1; i <= 10000; i++) { // Check if i*i*i is a // pallindrome or not if (isPalindrome(i * i * i)) arr[i] = 1; else arr[i] = 0; } // Convert arr[] to prefix sum array for (int i = 1; i <= 10000; i++) { arr[i] = arr[i] + arr[i - 1]; }}// Driver Codeint main(){ // Given queries vector<pair<int, int> > Q = { { 2, 7 }, { 10, 25 } }; precompute(); for (auto it : Q) { // Using inclusion-exclusion // principle, count required numbers cout << arr[it.second] - arr[it.first - 1] << "\n"; } return 0;} |
Java
// Java program of the above approachimport 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]; // Function to check if n is // a pallindrome number or not static int isPalindrome(int n) { // Temporarily store n int temp = n; // Stores reverse of n int res = 0; // Iterate until temp reduces to 0 while (temp != 0) { // Extract the last digit int rem = temp % 10; // Add to the start res = res * 10 + rem; // Remove the last digit temp /= 10; } // If the number and its // reverse are equal if (res == n) { return 1; } // Otherwise else return 0; } // Function to precompute and store // the count of numbers whose cube // is a palindrome number static void precompute() { // Iterate upto 10^4 for (int i = 1; i <= 10000; i++) { // Check if i*i*i is a // pallindrome or not if (isPalindrome(i * i * i)!= 0) arr[i] = 1; else arr[i] = 0; } // Convert arr[] to prefix sum array for (int i = 1; i <= 10000; i++) { arr[i] = arr[i] + arr[i - 1]; } } // Driver Code public static void main(String[] args) { // Given queries 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++) { // Using inclusion-exclusion // principle, count required numbers System.out.println(arr[Q.get(i).value()] - arr[Q.get(i).key()-1]); } }}// This code is contributed by Dharanendra L V |
2 1
Time Complexity: O(N)
Auxiliary Space: O(maxm), where maxm denotes the maximum value of R in a query
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


