 Open in App
Not now

# Longest set of Palindrome Numbers from the range [L, R] with at most K difference between its maximum and minimum

• Last Updated : 26 Jul, 2021

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++

 `// C++ program for the above approach` `#include` `using` `namespace` `std;` `   `  `// Function to search the` `// rightmost index of given number` `static` `int` `search(vector<``int``> list, ``int` `num)` `{` `    ``int` `low = 0, high = list.size() - 1;`   `    ``// Store the rightmost index` `    ``int` `ans = -1;`   `    ``while` `(low <= high)` `    ``{` `        `  `        ``// Calculate the mid` `        ``int` `mid = low + (high - low) / 2;`   `        ``// If given number <= num` `        ``if` `(list[mid] <= num)` `        ``{` `            `  `            ``// Assign ans = mid` `            ``ans = mid;`   `            ``// Update low` `            ``low = mid + 1;` `        ``}` `        ``else`   `            ``// Update high` `            ``high = mid - 1;` `    ``}`   `    ``// return ans` `    ``return` `ans;` `}`   `// Function to check if the given` `// number is palindrome or not` `bool` `isPalindrome(``int` `n)` `{` `    ``int` `rev = 0;` `    ``int` `temp = n;`   `    ``// Generate reverse` `    ``// of the given number` `    ``while` `(n > 0)` `    ``{` `        ``rev = rev * 10 + n % 10;` `        ``n /= 10;` `    ``}`   `    ``// If n is a palindrome` `    ``return` `rev == temp;` `}`   `// Function to find the maximum size` `// of group of palindrome numbers` `// having difference between maximum` `// and minimum element at most K` `int` `countNumbers(``int` `L, ``int` `R, ``int` `K)` `{` `    `  `    ``// Stores the all the palindromic` `    ``// numbers in the range [L, R]` `    ``vector<``int``> list;`   `    ``// Traverse over the range [L, R]` `    ``for``(``int` `i = L; i <= R; i++) ` `    ``{` `        `  `        ``// If i is a palindrome` `        ``if` `(isPalindrome(i)) ` `        ``{` `            `  `            ``// Append the number` `            ``// in the list` `            ``list.push_back(i);` `        ``}` `    ``}`   `    ``// Stores count of maximum` `    ``// palindromic numbers` `    ``int` `count = 0;`   `    ``// Iterate each element in the list` `    ``for``(``int` `i = 0; i < list.size(); i++) ` `    ``{` `        `  `        ``// Calculate rightmost index in` `        ``// the list < current element + K` `        ``int` `right_index = search(list, ` `                                 ``list[i] + K - 1);`   `        ``// Check if there is rightmost` `        ``// index from the current index` `        ``if` `(right_index != -1)` `            ``count = max(count, right_index - i + 1);` `    ``}`   `    ``// Return the count` `    ``return` `count;` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `L = 98, R = 112;` `    ``int` `K = 13;`   `    ``cout << countNumbers(L, R, K);` `}`   `// This code is contributed by ipg2016107`

## Java

 `// Java program for the above approach` `import` `java.util.*;`   `public` `class` `Main {`   `    ``// Function to find the maximum size` `    ``// of group of palindrome numbers` `    ``// having difference between maximum` `    ``// and minimum element at most K` `    ``static` `int` `countNumbers(``int` `L, ``int` `R, ``int` `K)` `    ``{` `        ``// Stores the all the palindromic` `        ``// numbers in the range [L, R]` `        ``ArrayList list` `            ``= ``new` `ArrayList<>();`   `        ``// Traverse over the range [L, R]` `        ``for` `(``int` `i = L; i <= R; i++) {`   `            ``// If i is a palindrome` `            ``if` `(isPalindrome(i)) {`   `                ``// Append the number` `                ``// in the list` `                ``list.add(i);` `            ``}` `        ``}`   `        ``// Stores count of maximum` `        ``// palindromic numbers` `        ``int` `count = ``0``;`   `        ``// Iterate each element in the list` `        ``for` `(``int` `i = ``0``; i < list.size(); i++) {`   `            ``// Calculate rightmost index in` `            ``// the list < current element + K` `            ``int` `right_index` `                ``= search(list, list.get(i) + K - ``1``);`   `            ``// Check if there is rightmost` `            ``// index from the current index` `            ``if` `(right_index != -``1``)` `                ``count = Math.max(count,` `                                 ``right_index - i + ``1``);` `        ``}`   `        ``// Return the count` `        ``return` `count;` `    ``}`   `    ``// Function to search the` `    ``// rightmost index of given number` `    ``static` `int` `search(` `        ``ArrayList list, ``int` `num)` `    ``{` `        ``int` `low = ``0``, high = list.size() - ``1``;`   `        ``// Store the rightmost index` `        ``int` `ans = -``1``;`   `        ``while` `(low <= high) {`   `            ``// Calculate the mid` `            ``int` `mid = low + (high - low) / ``2``;`   `            ``// If given number <= num` `            ``if` `(list.get(mid) <= num) {`   `                ``// Assign ans = mid` `                ``ans = mid;`   `                ``// Update low` `                ``low = mid + ``1``;` `            ``}` `            ``else`   `                ``// Update high` `                ``high = mid - ``1``;` `        ``}`   `        ``// return ans` `        ``return` `ans;` `    ``}`   `    ``// Function to check if the given` `    ``// number is palindrome or not` `    ``static` `boolean` `isPalindrome(``int` `n)` `    ``{` `        ``int` `rev = ``0``;` `        ``int` `temp = n;`   `        ``// Generate reverse` `        ``// of the given number` `        ``while` `(n > ``0``) {` `            ``rev = rev * ``10` `+ n % ``10``;` `            ``n /= ``10``;` `        ``}`   `        ``// If n is a palindrome` `        ``return` `rev == temp;` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `main(String args[])` `    ``{` `        ``int` `L = ``98``, R = ``112``;` `        ``int` `K = ``13``;`   `        ``System.out.print(` `            ``countNumbers(L, R, K));` `    ``}` `}`

## Python3

 `# Python3 program for the above approach`   `# Function to find the maximum size` `# of group of palindrome numbers` `# having difference between maximum` `# and minimum element at most K` `def` `countNumbers(L, R, K):` `  `  `    ``# Stores the all the palindromic` `    ``# numbers in the range [L, R]` `    ``list` `=` `[]`   `    ``# Traverse over the range [L, R]` `    ``for` `i ``in` `range``(L, R ``+` `1``):` `      `  `        ``# If i is a palindrome` `        ``if` `(isPalindrome(i)):` `          `  `            ``# Append the number` `            ``# in the list` `            ``list``.append(i)`   `    ``# Stores count of maximum` `    ``# palindromic numbers` `    ``count ``=` `0`   `    ``# Iterate each element in the list` `    ``for` `i ``in` `range``(``len``(``list``)):` `      `  `        ``# Calculate rightmost index in` `        ``# the list < current element + K` `        ``right_index ``=` `search(``list``, ``list``[i] ``+` `K ``-` `1``)`   `        ``# Check if there is rightmost` `        ``# index from the current index` `        ``if` `(right_index !``=` `-``1``):` `            ``count ``=` `max``(count, right_index ``-` `i ``+` `1``)`   `    ``# Return the count` `    ``return` `count`   `# Function to search the` `# rightmost index of given number` `def` `search(``list``, num):` `    ``low, high ``=` `0``, ``len``(``list``) ``-` `1`   `    ``# Store the rightmost index` `    ``ans ``=` `-``1`   `    ``while` `(low <``=` `high):`   `        ``# Calculate the mid` `        ``mid ``=` `low ``+` `(high ``-` `low) ``/``/` `2`   `        ``# If given number <= num` `        ``if` `(``list``[mid] <``=` `num):`   `            ``# Assign ans = mid` `            ``ans ``=` `mid`   `            ``# Update low` `            ``low ``=` `mid ``+` `1` `        ``else``:` `            ``# Update high` `            ``high ``=` `mid ``-` `1`   `    ``# return ans` `    ``return` `ans`   `# Function to check if the given` `# number is palindrome or not` `def` `isPalindrome(n):` `    ``rev ``=` `0` `    ``temp ``=` `n`   `    ``# Generate reverse` `    ``# of the given number` `    ``while` `(n > ``0``):` `        ``rev ``=` `rev ``*` `10` `+` `n ``%` `10` `        ``n ``/``/``=` `10`   `    ``# If n is a palindrome` `    ``return` `rev ``=``=` `temp`   `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `    ``L, R ``=` `98``, ``112` `    ``K ``=` `13`   `    ``print``(countNumbers(L, R, K))`   `# This code is contributed by mohit kumar 29.`

## C#

 `// C# program for the above approach` `using` `System;` `using` `System.Collections.Generic;`   `class` `GFG{`   `// Function to find the maximum size` `// of group of palindrome numbers` `// having difference between maximum` `// and minimum element at most K` `static` `int` `countNumbers(``int` `L, ``int` `R, ``int` `K)` `{` `    `  `    ``// Stores the all the palindromic` `    ``// numbers in the range [L, R]` `    ``List<``int``> list = ``new` `List<``int``>();`   `    ``// Traverse over the range [L, R]` `    ``for``(``int` `i = L; i <= R; i++)` `    ``{` `        `  `        ``// If i is a palindrome` `        ``if` `(isPalindrome(i)) ` `        ``{` `            `  `            ``// Append the number` `            ``// in the list` `            ``list.Add(i);` `        ``}` `    ``}`   `    ``// Stores count of maximum` `    ``// palindromic numbers` `    ``int` `count = 0;`   `    ``// Iterate each element in the list` `    ``for``(``int` `i = 0; i < list.Count; i++) ` `    ``{` `        `  `        ``// Calculate rightmost index in` `        ``// the list < current element + K` `        ``int` `right_index = search(list, ` `                                 ``list[i] + K - 1);`   `        ``// Check if there is rightmost` `        ``// index from the current index` `        ``if` `(right_index != -1)` `            ``count = Math.Max(count,` `                             ``right_index - i + 1);` `    ``}`   `    ``// Return the count` `    ``return` `count;` `}`   `// Function to search the` `// rightmost index of given number` `static` `int` `search(List<``int``> list, ``int` `num)` `{` `    ``int` `low = 0, high = list.Count - 1;`   `    ``// Store the rightmost index` `    ``int` `ans = -1;`   `    ``while` `(low <= high) ` `    ``{` `        `  `        ``// Calculate the mid` `        ``int` `mid = low + (high - low) / 2;`   `        ``// If given number <= num` `        ``if` `(list[mid] <= num)` `        ``{` `            `  `            ``// Assign ans = mid` `            ``ans = mid;`   `            ``// Update low` `            ``low = mid + 1;` `        ``}` `        ``else`   `            ``// Update high` `            ``high = mid - 1;` `    ``}`   `    ``// return ans` `    ``return` `ans;` `}`   `// Function to check if the given` `// number is palindrome or not` `static` `bool` `isPalindrome(``int` `n)` `{` `    ``int` `rev = 0;` `    ``int` `temp = n;`   `    ``// Generate reverse` `    ``// of the given number` `    ``while` `(n > 0)` `    ``{` `        ``rev = rev * 10 + n % 10;` `        ``n /= 10;` `    ``}`   `    ``// If n is a palindrome` `    ``return` `rev == temp;` `}`   `// Driver Code` `public` `static` `void` `Main(``string``[] args)` `{` `    ``int` `L = 98, R = 112;` `    ``int` `K = 13;`   `    ``Console.WriteLine(countNumbers(L, R, K));` `}` `}`   `// This code is contributed by avijitmondal1998`

## Javascript

 ``

Output:

`3`

Time Complexity: O((R – L) * log(R – L))
Auxiliary Space: O(R – L)

My Personal Notes arrow_drop_up
Related Articles