Longest set of Palindrome Numbers from the range [L, R] with at most K difference between its maximum and minimum
Given three positive integers L, R, and K, the task is to find the largest group of palindromic numbers from the range [L, R] such that the difference between the maximum and the minimum element present in the group is less than K.
Examples:
Input: L = 50, R = 78, K = 12
Output: 2
Explanation:
All palindromic numbers from the range [50, 78] are {55, 66, 77}.
The group of palindromic numbers {55, 66} have the difference between the maximum and minimum element as 11, which is less than K( = 12).
Therefore, the size of the group is 2.
Input: L = 98, R = 112, K = 13
Output: 3
Approach: The given problem can be solved by using Binary Search. Follow the steps below to solve the problem:
- Initialize an auxiliary array, say arr[], and store all the palindrome numbers present in the range [L, R].
- Define a function, say search(arr, X), to find the rightmost index with value less than X:
- Initialize three variables, say low as 0, high as (arr.size() – 1), and ans as -1.
- Iterate until low ≤ high and perform the following operations:
- Calculate mid as low+ (high – low)/2.
- If the value at index mid is at most X, then update the value of ans as mid and low as (mid + 1).
- Otherwise, update the value of high as (mid – 1).
- After completing the above steps, return the value of ans as the result.
- Traverse the array arr[] and perform the following steps:
- Find the rightmost index, which is less than or equal to (arr[i] + K – 1) using function search() and store it in a variable, say rightIndex.
- If the value of rightIndex is not equal to -1, then update the value of count as the maximum of count and (rightIndex – i + 1).
- After completing the above steps, print the value of count as the resultant.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
static int search(vector< int > list, int num)
{
int low = 0, high = list.size() - 1;
int ans = -1;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (list[mid] <= num)
{
ans = mid;
low = mid + 1;
}
else
high = mid - 1;
}
return ans;
}
bool isPalindrome( int n)
{
int rev = 0;
int temp = n;
while (n > 0)
{
rev = rev * 10 + n % 10;
n /= 10;
}
return rev == temp;
}
int countNumbers( int L, int R, int K)
{
vector< int > list;
for ( int i = L; i <= R; i++)
{
if (isPalindrome(i))
{
list.push_back(i);
}
}
int count = 0;
for ( int i = 0; i < list.size(); i++)
{
int right_index = search(list,
list[i] + K - 1);
if (right_index != -1)
count = max(count, right_index - i + 1);
}
return count;
}
int main()
{
int L = 98, R = 112;
int K = 13;
cout << countNumbers(L, R, K);
}
|
Java
import java.util.*;
public class Main {
static int countNumbers( int L, int R, int K)
{
ArrayList<Integer> list
= new ArrayList<>();
for ( int i = L; i <= R; i++) {
if (isPalindrome(i)) {
list.add(i);
}
}
int count = 0 ;
for ( int i = 0 ; i < list.size(); i++) {
int right_index
= search(list, list.get(i) + K - 1 );
if (right_index != - 1 )
count = Math.max(count,
right_index - i + 1 );
}
return count;
}
static int search(
ArrayList<Integer> list, int num)
{
int low = 0 , high = list.size() - 1 ;
int ans = - 1 ;
while (low <= high) {
int mid = low + (high - low) / 2 ;
if (list.get(mid) <= num) {
ans = mid;
low = mid + 1 ;
}
else
high = mid - 1 ;
}
return ans;
}
static boolean isPalindrome( int n)
{
int rev = 0 ;
int temp = n;
while (n > 0 ) {
rev = rev * 10 + n % 10 ;
n /= 10 ;
}
return rev == temp;
}
public static void main(String args[])
{
int L = 98 , R = 112 ;
int K = 13 ;
System.out.print(
countNumbers(L, R, K));
}
}
|
Python3
def countNumbers(L, R, K):
list = []
for i in range (L, R + 1 ):
if (isPalindrome(i)):
list .append(i)
count = 0
for i in range ( len ( list )):
right_index = search( list , list [i] + K - 1 )
if (right_index ! = - 1 ):
count = max (count, right_index - i + 1 )
return count
def search( list , num):
low, high = 0 , len ( list ) - 1
ans = - 1
while (low < = high):
mid = low + (high - low) / / 2
if ( list [mid] < = num):
ans = mid
low = mid + 1
else :
high = mid - 1
return ans
def isPalindrome(n):
rev = 0
temp = n
while (n > 0 ):
rev = rev * 10 + n % 10
n / / = 10
return rev = = temp
if __name__ = = '__main__' :
L, R = 98 , 112
K = 13
print (countNumbers(L, R, K))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int countNumbers( int L, int R, int K)
{
List< int > list = new List< int >();
for ( int i = L; i <= R; i++)
{
if (isPalindrome(i))
{
list.Add(i);
}
}
int count = 0;
for ( int i = 0; i < list.Count; i++)
{
int right_index = search(list,
list[i] + K - 1);
if (right_index != -1)
count = Math.Max(count,
right_index - i + 1);
}
return count;
}
static int search(List< int > list, int num)
{
int low = 0, high = list.Count - 1;
int ans = -1;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (list[mid] <= num)
{
ans = mid;
low = mid + 1;
}
else
high = mid - 1;
}
return ans;
}
static bool isPalindrome( int n)
{
int rev = 0;
int temp = n;
while (n > 0)
{
rev = rev * 10 + n % 10;
n /= 10;
}
return rev == temp;
}
public static void Main( string [] args)
{
int L = 98, R = 112;
int K = 13;
Console.WriteLine(countNumbers(L, R, K));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity: O((R – L) * log(R – L))
Auxiliary Space: O(R – L)
Last Updated :
26 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...