Queries to count Palindrome Numbers from a range whose sum of digits is a Prime Number
Given an array Q[][] consisting of N queries of the form {L, R}, the task for each query is to find the count of the numbers in the range [L, R] that are palindrome and the sum of their digits is a prime number.
Examples:
Input: Q[][] = {{5, 9}, {5, 22}}
Output:
2
3
Explanation:
Query 1: All palindrome numbers from the range [5, 9] having sum of their digits equal to a prime number are {5, 7}. Therefore, the count of elements is 2.
Query 2: All palindrome numbers from the range [5, 20] having sum of their digits equal to a prime number are {5, 7, 11}. Therefore, the count of elements is 2.
Input: Q[] = {{1, 101}, {13, 15}}
Output:
6
0
Naive Approach: The simplest approach to solve the given problem is to iterate over the range [L, R] for each query and print the count of those numbers that are palindrome, and the sum of their digits is a prime number.
Time Complexity: O(N*(R – L))
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by using the Inclusion-Exclusion principle and the concepts of prefix sum. Follow the steps below to solve the given problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int arr[100005];
bool 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 true ;
}
else {
return false ;
}
}
int sumOfDigits( int N)
{
int sum = 0;
while (N != 0) {
sum += N % 10;
N /= 10;
}
return sum;
}
bool isPrime( int n)
{
if (n <= 1) {
return false ;
}
for ( int i = 2; i <= n / 2; ++i) {
if (n % i == 0)
return false ;
}
return true ;
}
void precompute()
{
for ( int i = 1; i <= 100000; i++) {
if (isPalindrome(i)) {
int sum = sumOfDigits(i);
if (isPrime(sum))
arr[i] = 1;
else
arr[i] = 0;
}
else
arr[i] = 0;
}
for ( int i = 1; i <= 100000; i++) {
arr[i] = arr[i] + arr[i - 1];
}
}
void countNumbers( int Q[][2], int N)
{
precompute();
for ( int i = 0; i < N; i++) {
cout << (arr[Q[i][1]]
- arr[Q[i][0] - 1]);
cout << endl;
}
}
int main()
{
int Q[][2] = { { 5, 9 }, { 1, 101 } };
int N = sizeof (Q) / sizeof (Q[0]);
countNumbers(Q, N);
}
|
Java
class GFG{
static int [] arr = new int [ 100005 ];
static boolean 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 true ;
}
else
{
return false ;
}
}
static int sumOfDigits( int N)
{
int sum = 0 ;
while (N != 0 )
{
sum += N % 10 ;
N /= 10 ;
}
return sum;
}
static boolean isPrime( int n)
{
if (n <= 1 )
{
return false ;
}
for ( int i = 2 ; i <= n / 2 ; ++i)
{
if (n % i == 0 )
return false ;
}
return true ;
}
static void precompute()
{
for ( int i = 1 ; i <= 100000 ; i++)
{
if (isPalindrome(i))
{
int sum = sumOfDigits(i);
if (isPrime(sum))
arr[i] = 1 ;
else
arr[i] = 0 ;
}
else
arr[i] = 0 ;
}
for ( int i = 1 ; i <= 100000 ; i++)
{
arr[i] = arr[i] + arr[i - 1 ];
}
}
static void countNumbers( int [][] Q, int N)
{
precompute();
for ( int i = 0 ; i < N; i++)
{
System.out.println((arr[Q[i][ 1 ]] -
arr[Q[i][ 0 ] - 1 ]));
}
}
public static void main(String[] args)
{
int [][] Q = { { 5 , 9 }, { 1 , 101 } };
int N = Q.length;
countNumbers(Q, N);
}
}
|
Python3
arr = [ 0 for i in range ( 100005 )]
def isPalindrome(N):
temp = N
res = 0
while (temp ! = 0 ):
rem = temp % 10
res = res * 10 + rem
temp / / = 10
if (res = = N):
return True
else :
return False
def sumOfDigits(N):
sum = 0
while (N ! = 0 ):
sum + = N % 10
N / / = 10
return sum
def isPrime(n):
if (n < = 1 ):
return False
for i in range ( 2 , (n / / 2 ) + 1 , 1 ):
if (n % i = = 0 ):
return False
return True
def precompute():
for i in range ( 1 , 100001 , 1 ):
if (isPalindrome(i)):
sum = sumOfDigits(i)
if (isPrime( sum )):
arr[i] = 1
else :
arr[i] = 0
else :
arr[i] = 0
for i in range ( 1 , 100001 , 1 ):
arr[i] = arr[i] + arr[i - 1 ]
def countNumbers(Q, N):
precompute()
for i in range (N):
print (arr[Q[i][ 1 ]] - arr[Q[i][ 0 ] - 1 ])
if __name__ = = '__main__' :
Q = [[ 5 , 9 ], [ 1 , 101 ]]
N = len (Q)
countNumbers(Q, N)
|
C#
using System;
class GFG{
static int [] arr = new int [100005];
static bool 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 true ;
}
else
{
return false ;
}
}
static int sumOfDigits( int N)
{
int sum = 0;
while (N != 0)
{
sum += N % 10;
N /= 10;
}
return sum;
}
static bool isPrime( int n)
{
if (n <= 1)
{
return false ;
}
for ( int i = 2; i <= n / 2; ++i)
{
if (n % i == 0)
return false ;
}
return true ;
}
static void precompute()
{
for ( int i = 1; i <= 100000; i++)
{
if (isPalindrome(i))
{
int sum = sumOfDigits(i);
if (isPrime(sum))
arr[i] = 1;
else
arr[i] = 0;
}
else
arr[i] = 0;
}
for ( int i = 1; i <= 100000; i++)
{
arr[i] = arr[i] + arr[i - 1];
}
}
static void countNumbers( int [, ] Q, int N)
{
precompute();
for ( int i = 0; i < N; i++)
{
Console.WriteLine((arr[Q[i, 1]] -
arr[Q[i, 0] - 1]));
}
}
static public void Main()
{
int [,] Q = { { 5, 9 }, { 1, 101 } };
int N = Q.GetLength(0);
countNumbers(Q, N);
}
}
|
Javascript
<script>
let arr = [];
for (let m = 0; m < 100005; m++)
{
arr[m] = 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 true ;
}
else
{
return false ;
}
}
function sumOfDigits(N)
{
let sum = 0;
while (N != 0)
{
sum += N % 10;
N = Math.floor( N / 10);
}
return sum;
}
function isPrime(n)
{
if (n <= 1)
{
return false ;
}
for (let i = 2; i <= Math.floor(n / 2); ++i)
{
if (n % i == 0)
return false ;
}
return true ;
}
function precompute()
{
for (let i = 1; i <= 100000; i++)
{
if (isPalindrome(i))
{
let sum = sumOfDigits(i);
if (isPrime(sum))
arr[i] = 1;
else
arr[i] = 0;
}
else
arr[i] = 0;
}
for (let i = 1; i <= 100000; i++)
{
arr[i] = arr[i] + arr[i - 1];
}
}
function countNumbers( Q, N)
{
precompute();
for (let i = 0; i < N; i++)
{
document.write((arr[Q[i][1]] -
arr[Q[i][0] - 1]) + "<br/>" );
}
}
let Q = [[ 5, 9 ], [ 1, 101 ]];
let N = Q.length;
countNumbers(Q, N);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(M), where M is the maximum element among each query.
Last Updated :
09 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...