Longest set of Palindrome Numbers from the range [ L , R ] with at most K difference between its maximum and minimum | Function to search the rightmost index of given number ; Store the rightmost index ; Calculate the mid ; If given number <= num ; Assign ans = mid ; Update low ; Update high ; return ans ; Function to check if the given number is palindrome or not ; Generate reverse of the given number ; If n is a palindrome ; Function to find the maximum size of group of palindrome numbers having difference between maximum and minimum element at most K ; Stores the all the palindromic numbers in the range [ L , R ] ; Traverse over the range [ L , R ] ; If i is a palindrome ; Append the number in the list ; Stores count of maximum palindromic numbers ; Iterate each element in the list ; Calculate rightmost index in the list < current element + K ; Check if there is rightmost index from the current index ; Return the count ; Driver Code
function search ( list , num ) { var low = 0 , high = list . length - 1 ; var ans = - 1 ; while ( low <= high ) { var mid = low + ( high - low ) / 2 ; if ( list [ mid ] <= num ) { ans = mid ; low = mid + 1 ; } else high = mid - 1 ; } return ans ; } function isPalindrome ( n ) { var rev = 0 ; var temp = n ; while ( n > 0 ) { rev = rev * 10 + n % 10 ; n = parseInt ( n / 10 ) ; } return rev == temp ; } function countNumbers ( L , R , K ) { var list = [ ] ; for ( var i = L ; i <= R ; i ++ ) { if ( isPalindrome ( i ) ) { list . push ( i ) ; } } var count = 0 ; for ( var i = 0 ; i < list . length ; i ++ ) { var right_index = search ( list , list [ i ] + K - 1 ) ; if ( right_index != - 1 ) count = Math . max ( count , right_index - i + 1 ) ; } return count ; } var L = 98 , R = 112 ; var K = 13 ; document . write ( countNumbers ( L , R , K ) ) ;