Count numbers from a range whose cube is a Palindrome
Last Updated :
04 Jan, 2023
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 11
Input: 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++
#include <bits/stdc++.h>
using namespace std;
int arr[10005];
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;
}
void precompute()
{
for ( int i = 1; i <= 10000; i++) {
if (isPalindrome(i * i * i))
arr[i] = 1;
else
arr[i] = 0;
}
for ( int i = 1; i <= 10000; i++) {
arr[i] = arr[i] + arr[i - 1];
}
}
int main()
{
vector<pair< int , int > > Q = { { 2, 7 }, { 10, 25 } };
precompute();
for ( auto it : Q) {
cout << arr[it.second] - arr[it.first - 1] << "\n" ;
}
return 0;
}
|
Java
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 ]);
}
}
}
|
Python3
arr = [ 0 ] * 10005
def isPalindrome(n):
temp = n
res = 0
while temp ! = 0 :
rem = temp % 10
res = res * 10 + rem
temp = temp / / 10
if res = = n:
return 1
else :
return 0
def precompute():
for i in range ( 1 , 10001 ):
if isPalindrome(i * i * i):
arr[i] = 1
else :
arr[i] = 0
for i in range ( 1 , 10001 ):
arr[i] = arr[i] + arr[i - 1 ]
Q = [[ 2 , 7 ], [ 10 , 25 ]]
precompute()
for it in Q:
print (arr[it[ 1 ]] - arr[it[ 0 ] - 1 ])
|
C#
using System;
using System.Collections.Generic;
public class Pair {
private readonly int key;
private readonly 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];
}
}
static void Main() {
var Q = new List<Pair> { new Pair(2, 7), new Pair(10, 25) };
Precompute();
for ( int i = 0; i < Q.Count; i++)
{
Console.WriteLine(arr[Q[i].Value()] - arr[Q[i].Key() - 1]);
}
}
}
|
Javascript
<script>
let arr = new Array(10005).fill(0)
function isPalindrome(n) {
let temp = n;
let res = 0;
while (temp != 0) {
let rem = temp % 10;
res = res * 10 + rem;
temp = Math.floor(temp / 10);
}
if (res == n) {
return 1;
}
else
return 0;
}
function precompute() {
for (let i = 1; i <= 10000; i++) {
if (isPalindrome(i * i * i))
arr[i] = 1;
else
arr[i] = 0;
}
for (let i = 1; i <= 10000; i++) {
arr[i] = arr[i] + arr[i - 1];
}
}
let Q = [[2, 7], [10, 25]];
precompute();
for (let it of Q) {
document.write(arr[it[1]] - arr[it[0] - 1] + "<br>" );
}
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(maxm), where maxm denotes the maximum value of R in a query
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...