Given two integer arrays A[] and D[], where Ai and Di represent the first element and common difference of an arithmetic progression respectively, the task is to find an element that is common in the maximum number of given arithmetic progressions.
Examples:
Input: A[] = {3, 1, 2, 5}, D[] = {2, 3, 1, 2}
Output: 7
Explanation: The integer 7 is present in all the given APs.Input: A[] = {13, 1, 2, 5}, D[] = {5, 10, 1, 12}
Output: 41
Approach: The problem can be easily solved using Hashing. Initialize a cnt[] array. For each element of every AP, increment the count of the element in the cnt[] array. Return the index of the cnt[] array with the maximum count.
Below is the implementation of the above approach:
CPP
// C++ implementation of the approach #include <bits/stdc++.h> 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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- K-th term from given N merged Arithmetic Progressions
- Progressions (AP, GP, HP)
- Longest arithmetic progression with the given common difference
- Least common element in given two Arithmetic sequences
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- Count common elements in two arrays which are in Arithmetic Progression
- Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
- Find the maximum sum (a+b) for a given input integer N satisfying the given condition
- Find the largest number smaller than integer N with maximum number of set bits
- Minimum decrements to make integer A divisible by integer B
- Maximum count of pairwise co-prime and common divisors of two given numbers
- Find an integer in the given range that satisfies the given conditions
- Find the missing number in Arithmetic Progression
- Find the missing number in unordered Arithmetic Progression
- Length of longest sub-array with maximum arithmetic mean.
- Find a pair (n,r) in an integer array such that value of nPr is maximum
- Find a pair (n,r) in an integer array such that value of nCr is maximum
- Find if two given Quadratic equations have common roots or not
- Number of common tangents between two circles if their centers and radius is given
- Maximum count of common divisors of A and B such that all are co-primes to one another
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

