📜  [L,R]范围内最长的回文数集,最大和最小之间的差异最大为K

📅  最后修改于: 2021-04-17 16:43:30             🧑  作者: Mango

给定三个正整数LRK ,任务是从范围[L,R]中找到最大回文数组,以使该组中存在的最大元素和最小元素之间的差小于K。

例子:

方法:可以通过使用二进制搜索解决给定的问题。请按照以下步骤解决问题:

  • 初始化一个辅助数组,例如arr [],并存储在[L,R]范围内的所有回文数。
  • 定义一个函数,例如search(arr,X),以查找值小于X的最右边的索引:
    • 初始化三个变量,比如0(arr.size()– 1)ans-1
    • 迭代直到从高,然后执行以下操作:
      • 计算低至高+(高至低)/ 2
      • 如果在索引中期值为至多X,然后更新ANS的作为的值作为(MID + 1)。
      • 否则,将high的值更新为(mid – 1)
    • 完成上述步骤后,返回ans的值作为结果。
  • 遍历数组arr []并执行以下步骤:
    • 使用函数search()查找最右边的索引,该索引小于或等于(arr [i] + K – 1)并将其存储在变量中,例如rightIndex
    • 如果rightIndex的值不等于-1 ,则将count的值更新为count(rightIndex – i + 1)的最大值。
  • 完成上述步骤后,打印count的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
    
// Function to search the
// rightmost index of given number
static int search(vector list, int num)
{
    int low = 0, high = list.size() - 1;
 
    // Store the rightmost index
    int ans = -1;
 
    while (low <= high)
    {
         
        // Calulate 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 list;
 
    // Travese 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++)
    {
         
        // Calulate rightmost index in
        // the list < current elemnt + 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<>();
 
        // Travese 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++) {
 
            // Calulate rightmost index in
            // the list < current elemnt + 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) {
 
            // Calulate 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 = []
 
    # Travese 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)):
       
        # Calulate rightmost index in
        # the list < current elemnt + 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):
 
        # Calulate 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 list = new List();
 
    // Travese 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++)
    {
         
        // Calulate rightmost index in
        // the list < current elemnt + 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 list, int num)
{
    int low = 0, high = list.Count - 1;
 
    // Store the rightmost index
    int ans = -1;
 
    while (low <= high)
    {
         
        // Calulate 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


输出:
3

时间复杂度: O((R – L)* log(R – L))
辅助空间: O(R – L)