Given Q queries in the form of 2D array arr[][] whose every row consists of two numbers L and R which denotes the range [L, R], the task is to find the sum of all Palindrome Numbers lying in range [L, R].
Input: Q = 2, arr[][] = { {10, 13}, {12, 21} }
Output:
11
0
Explanation:
From 10 to 13 only 11 is the palindrome number
From 12 to 21 there is no palindromic number
Input: Q = 4, arr[][] = { { 10, 10 }, { 258, 785 }, {45, 245 }, { 1, 1000} }
Output:
0
27024
2955
50040
Approach:
The idea is to use the prefix sum array. The sum of all palindromic number till that particular index is precomputed and stored in an array pref[] so that every query can be answered in O(1) time.
- Initialise the prefix array pref[].
- Iterate from 1 to N and check if the number is palindromic or not:
- If the number is palindromic then, the current index of pref[] will store the sum of the number and the number at previous index of pref[].
- Else the current index of pref[] is same as the value at previous index of pref[].
- For Q queries the sum of all palindromic numbers for range [L, R] can be found as follows:
sum = pref[R] - pref[L - 1]
-
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
long long pref[100001];
int checkPalindrome(int num)
{
string str = to_string(num);
int l = 0, r = str.length() - 1;
while (l < r) {
if (str[l] != str[r]) {
return 0;
}
l++;
r--;
}
return num;
}
void preCompute()
{
for (int i = 1; i <= 100000; ++i) {
pref[i] = pref[i - 1]
+ checkPalindrome(i);
}
}
void printSum(int L, int R)
{
cout << pref[R] - pref[L - 1]
<< endl;
}
void printSumPalindromic(int arr[][2],
int Q)
{
preCompute();
for (int i = 0; i < Q; i++) {
printSum(arr[i][0], arr[i][1]);
}
}
int main()
{
int Q = 2;
int arr[][2] = { { 10, 13 },
{ 12, 21 } };
printSumPalindromic(arr, Q);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int []pref = new int[100001];
static int checkPalindrome(int num)
{
String str = String.valueOf(num);
int l = 0, r = str.length() - 1;
while (l < r) {
if (str.charAt(l) != str.charAt(r)) {
return 0;
}
l++;
r--;
}
return num;
}
static void preCompute()
{
for (int i = 1; i <= 100000; ++i) {
pref[i] = pref[i - 1]
+ checkPalindrome(i);
}
}
static void printSum(int L, int R)
{
System.out.print(pref[R] - pref[L - 1]
+"\n");
}
static void printSumPalindromic(int arr[][],
int Q)
{
preCompute();
for (int i = 0; i < Q; i++) {
printSum(arr[i][0], arr[i][1]);
}
}
public static void main(String[] args)
{
int Q = 2;
int arr[][] = { { 10, 13 },
{ 12, 21 } };
printSumPalindromic(arr, Q);
}
}
|
Python3
pref=[0]*100001
def checkPalindrome(num):
strr = str(num)
l = 0
r = len(strr)- 1
while (l < r):
if (strr[l] != strr[r]):
return 0
l+=1
r-=1
return num
def preCompute():
for i in range(1,100001):
pref[i] = pref[i - 1]+ checkPalindrome(i)
def printSum(L, R):
print(pref[R] - pref[L - 1])
def printSumPalindromic(arr,Q):
preCompute()
for i in range(Q):
printSum(arr[i][0], arr[i][1])
Q = 2
arr= [[10, 13 ],[ 12, 21 ]]
printSumPalindromic(arr, Q)
|
C#
using System;
class GFG{
static int []pref = new int[100001];
static int checkPalindrome(int num)
{
String str = String.Join("",num);
int l = 0, r = str.Length - 1;
while (l < r) {
if (str[l] != str[r]) {
return 0;
}
l++;
r--;
}
return num;
}
static void preCompute()
{
for (int i = 1; i <= 100000; ++i) {
pref[i] = pref[i - 1]
+ checkPalindrome(i);
}
}
static void printSum(int L, int R)
{
Console.Write(pref[R] - pref[L - 1]
+"\n");
}
static void printSumPalindromic(int [,]arr,
int Q)
{
preCompute();
for (int i = 0; i < Q; i++) {
printSum(arr[i,0], arr[i,1]);
}
}
public static void Main(String[] args)
{
int Q = 2;
int [,]arr = { { 10, 13 },
{ 12, 21 } };
printSumPalindromic(arr, Q);
}
}
|
Javascript
<script>
var pref=Array(100001).fill(0);
function checkPalindrome(num)
{
var str = num.toString();
var l = 0, r = str.length - 1;
while (l < r) {
if (str[l] != str[r]) {
return 0;
}
l++;
r--;
}
return num;
}
function preCompute()
{
for (var i = 1; i <= 100000; ++i) {
pref[i] = pref[i - 1]
+ checkPalindrome(i);
}
}
function printSum(L, R)
{
document.write( pref[R] - pref[L - 1]+"<br>");
}
function printSumPalindromic(arr, Q)
{
preCompute();
for (var i = 0; i < Q; i++) {
printSum(arr[i][0], arr[i][1]);
}
}
var Q = 2;
var arr = [ [ 10, 13 ],
[ 12, 21 ] ];
printSumPalindromic(arr, Q);
</script>
|
Time Complexity: O(Q+105)
Auxiliary Space: O(105)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!