Find an integer that is common in the maximum number of given arithmetic progressions
Last Updated :
20 Feb, 2022
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
#include <bits/stdc++.h>
using namespace std;
#define MAXN 1000000
int maxCommonElement( int A[], int D[], int N)
{
int cnt[MAXN] = { 0 };
for ( int i = 0; i < N; i++) {
for ( int j = A[i]; j < MAXN; j += D[i])
cnt[j]++;
}
int com = max_element(cnt, cnt + MAXN) - cnt;
return com;
}
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
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;
}
static int maxCommonElement( int A[], int D[], int N)
{
int cnt[] = new int [MAXN] ;
for ( int i = 0 ; i < N; i++)
{
for ( int j = A[i]; j < MAXN; j += D[i])
cnt[j]++;
}
int ans = 0 ;
int com = 0 ;
for ( int i = 0 ; i < MAXN; i++)
{
if (cnt[i] > ans)
{
ans = cnt[i] ;
com = i ;
}
}
return com;
}
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));
}
}
|
Python
MAXN = 1000000
def maxCommonElement(A, D, N):
cnt = [ 0 ] * MAXN
for i in range (N):
for j in range (A[i], MAXN, D[i]):
cnt[j] + = 1
ans = 0
com = 0
for i in range (MAXN):
if cnt[i] > ans:
ans = cnt[i]
com = i
return com
A = [ 13 , 1 , 2 , 5 ]
D = [ 5 , 10 , 1 , 12 ]
N = len (A)
print (maxCommonElement(A, D, N))
|
C#
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;
}
static int maxCommonElement( int []A, int []D, int N)
{
int []cnt = new int [MAXN] ;
for ( int i = 0; i < N; i++)
{
for ( int j = A[i]; j < MAXN; j += D[i])
cnt[j]++;
}
int ans = 0;
int com = 0;
for ( int i = 0; i < MAXN; i++)
{
if (cnt[i] > ans)
{
ans = cnt[i] ;
com = i ;
}
}
return com;
}
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));
}
}
|
Javascript
<script>
var MAXN = 1000000;
function maxCommonElement(A, D, N)
{
var cnt = Array(MAXN).fill(0);
for ( var i = 0; i < N; i++) {
for ( var j = A[i]; j < MAXN; j += D[i])
cnt[j]++;
}
var ans = 0;
var com = 0;
for ( var i = 0; i < MAXN; i++)
{
if (cnt[i] > ans)
{
ans = cnt[i] ;
com = i ;
}
}
return com;
}
var A = [ 13, 1, 2, 5 ],
D = [ 5, 10, 1, 12 ];
var N = A.length;
document.write( maxCommonElement(A, D, N));
</script>
|
Time Complexity: O(N * 106)
Auxiliary Space: O(106)
Share your thoughts in the comments
Please Login to comment...