📜  查找在给定算术级数的最大数量中通用的整数

📅  最后修改于: 2021-04-29 14:17:08             🧑  作者: Mango

给定两个整数数组A []D [] ,其中A iD i分别表示算术级数的第一个元素和共同的差,任务是找到在给定算术级数的最大数目中是公共的元素。

例子:

方法:使用哈希可以轻松解决该问题。初始化一个cnt []数组。对于每个AP的每个元素,增加cnt []数组中元素的计数。返回具有最大计数的cnt []数组的索引。

下面是上述方法的实现:

CPP
// C++ implementation of the approach
#include 
using namespace std;
  
#define MAXN 1000000
  
// Function to return element common
// in maximum number of APs
int maxCommonElement(int A[], int D[], int N)
{
    // Initialize the count variable
    int cnt[MAXN] = { 0 };
  
    for (int i = 0; i < N; i++) {
  
        // Increment count for every
        // element of an AP
        for (int j = A[i]; j < MAXN; j += D[i])
            cnt[j]++;
    }
  
    // Find the index with maximum count
    int com = max_element(cnt, cnt + MAXN) - cnt;
  
    // Return the maximum common element
    return com;
}
  
// Driver code
int main()
{
    int A[] = { 13, 1, 2, 5 },
        D[] = { 5, 10, 1, 12 };
    int N = sizeof(A) / sizeof(A[0]);
  
    cout << maxCommonElement(A, D, N);
  
    return 0;
}


Java
// Java implementation of the approach 
class GFG 
{ 
      
    final static int MAXN = 1000000 ;
      
    static int max_element(int []A, int n) 
    { 
        int max = A[0]; 
        for(int i = 0; i < n; i++) 
            if (A[i] > max ) 
                max = A[i]; 
                  
        return max; 
    } 
  
    // Function to return element common 
    // in maximum number of APs 
    static int maxCommonElement(int A[], int D[], int N) 
    { 
        // Initialize the count variable 
        int cnt[] = new int[MAXN] ; 
      
        for (int i = 0; i < N; i++)
        { 
      
            // Increment count for every 
            // element of an AP 
            for (int j = A[i]; j < MAXN; j += D[i]) 
                cnt[j]++; 
        } 
          
          
        // Find the index with maximum count 
        int ans = 0;
        int com = 0;
          
        for(int i = 0; i < MAXN; i++)
        {
            if (cnt[i] > ans)
            { 
                ans = cnt[i] ;
                com = i ;
            }
        }
      
        // Return the maximum common element 
        return com; 
    } 
      
    // Driver code 
    public static void main (String args[]) 
    { 
        int A[] = { 13, 1, 2, 5 }, 
            D[] = { 5, 10, 1, 12 }; 
              
        int N = A.length; 
      
        System.out.println(maxCommonElement(A, D, N)); 
    } 
} 
  
// This code is contributed by AnkitRai01


Python
# Python implementation of the approach
  
MAXN = 1000000
  
# Function to return element common
# in maximum number of APs
def maxCommonElement(A, D, N):
      
    # Initialize the count variable
    cnt = [0] * MAXN
  
    for i in range(N):
  
        # Increment count for every
        # element of an AP
        for j in range(A[i], MAXN, D[i]):
            cnt[j] += 1
  
    # Find the index with maximum count
    ans = 0
    com = 0
    for i in range(MAXN):
        if cnt[i] > ans:
            ans = cnt[i]
            com = i
  
    # Return the maximum common element
    return com
  
# Driver code
  
A = [13, 1, 2, 5]
D = [5, 10, 1, 12]
N = len(A)
  
print(maxCommonElement(A, D, N))
  
# This code is contributed by mohit kumar 29


C#
// C# implementation of the approach 
using System;
  
class GFG 
{ 
      
    static int MAXN = 1000000 ;
      
    static int max_element(int []A, int n) 
    { 
        int max = A[0]; 
        for(int i = 0; i < n; i++) 
            if (A[i] > max ) 
                max = A[i]; 
                  
        return max; 
    } 
  
    // Function to return element common 
    // in maximum number of APs 
    static int maxCommonElement(int []A, int []D, int N) 
    { 
        // Initialize the count variable 
        int []cnt = new int[MAXN] ; 
      
        for (int i = 0; i < N; i++)
        { 
      
            // Increment count for every 
            // element of an AP 
            for (int j = A[i]; j < MAXN; j += D[i]) 
                cnt[j]++; 
        } 
          
          
        // Find the index with maximum count 
        int ans = 0;
        int com = 0;
          
        for(int i = 0; i < MAXN; i++)
        {
            if (cnt[i] > ans)
            { 
                ans = cnt[i] ;
                com = i ;
            }
        }
      
        // Return the maximum common element 
        return com; 
    } 
      
    // Driver code 
    public static void Main () 
    { 
        int []A = { 13, 1, 2, 5 }; 
        int []D = { 5, 10, 1, 12 }; 
              
        int N = A.Length; 
      
        Console.WriteLine(maxCommonElement(A, D, N)); 
    } 
} 
  
// This code is contributed by AnkitRai01


输出:
41