Sum of all palindromic numbers lying in the range [ L , R ] for Q queries | pref [ ] array to precompute the sum of all palindromic number ; Function that return number num if num is palindromic else return 0 ; Convert num to string ; Function to precompute the sum of all palindrome numbers upto 100000 ; checkPalindrome ( ) return the number i if i is palindromic else return 0 ; Function to print the sum for each query ; Function to print sum of all palindromic numbers between [ L , R ] ; Function that pre computes the sum of all palindromic numbers ; Iterate over all Queries to print the sum ; Queries ; Function that print the the sum of all palindromic number in Range [ L , R ]
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 ] + " " ) ; } 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 ) ;