Count common elements in two arrays which are in Arithmetic Progression

Given two arrays arr1[] and arr2[] of size M and N respectively. Both arrays are in Arithmetic Progression and the first element of both arrays is the same. The task is to find the number of common elements in arr1[] and arr2[].

Examples:

Input: arr1[] = {2, 3, 4, 5, 6}, arr2[] = {2, 4, 6, 8, 10}
Output: 3
Explanation:
Common elements are {2, 4, 6}

Input: arr1[] = {1, 4, 7, 10, 13, 16}, arr2[] = {1, 3, 5, 7, 9}
Output: 2
Explanation:
Common elements are {1, 7}

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to use Least Common Multiple of the common difference of the two Arithmetic Progression to solve this problem. Below is the illustration of the steps:



  • Find the common difference of the two arithmetic progression with the help of the below formulae
    diff1 = arr1[1] - arr1[0]
    diff2 = arr2[1] - arr2[0]
  • Find the Least common multiple of the common difference of the two arithmetic progression.
  • Common elements that are possible in the two arithmetic progression will be the difference of the last elements of the arithmetic progression to the first element divided by the LCM of the common difference.
    elements1 = (arr1[m-1] - arr1[0]) / LCM(diff1, diff2)
    elements2 = (arr2[n-1] - arr2[0]) / LCM(diff1, diff2)
    
    // Common Elements
    ans = min(elements, elements2)
    

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to count the
// common elements of the two arithmetic
// progression of the given sequence
#include<bits/stdc++.h>
using namespace std;
  
// Function to find GCD
int gcd(int a,int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
  
// Function to find LCM
int findlcm(int a, int b)
{
    int gc = gcd(a, b);
    return a * b / gc;
  
// Function to count common element 
// of arr1[] and arr2[]
int CountCommon(int arr1[], int arr2[], 
                int m, int n)
{
      
    // Common Difference
    int diff1 = arr1[1] - arr1[0];
    int diff2 = arr2[1] - arr2[0];
  
    // Function calling
    int lcm = findlcm(diff1, diff2);
  
    int ans1 = (arr1[m - 1] - arr1[0]) / lcm;
    int ans2 = (arr2[n - 1] - arr2[0]) / lcm;
    int ans = min(ans1, ans2);
      
    return (ans + 1);
  
// Driver code
int main()
{
    int arr1[] = { 2, 5, 8, 11, 14, 17 };
    int arr2[] = { 2, 4, 6, 8, 10, 12 };
      
    int m = sizeof(arr1) / sizeof(arr1[0]);
    int n = sizeof(arr2) / sizeof(arr2[0]);
  
    // Function calling
    cout << CountCommon(arr1, arr2, m, n);
    return 0;
}
  
// This code is contributed by amal kumar choubey

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to count the
// common elements of the two arithmetic
// progression of the given sequence
import java.util.*;
class GFG{
  
// Function to find GCD
static int gcd(int a,int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
  
// Function to find LCM
static int findlcm(int a, int b)
{
    int gc = gcd(a, b);
    return a * b / gc;
  
// Function to count common element 
// of arr1[] and arr2[]
static int CountCommon(int []arr1, 
                       int []arr2, 
                       int m, int n)
{
      
    // Common Difference
    int diff1 = arr1[1] - arr1[0];
    int diff2 = arr2[1] - arr2[0];
  
    // Function calling
    int lcm = findlcm(diff1, diff2);
  
    int ans1 = (arr1[m - 1] - arr1[0]) / lcm;
    int ans2 = (arr2[n - 1] - arr2[0]) / lcm;
    int ans = Math.min(ans1, ans2);
      
    return (ans + 1);
  
// Driver code
public static void main(String args[])
{
    int []arr1 = { 2, 5, 8, 11, 14, 17 };
    int []arr2 = { 2, 4, 6, 8, 10, 12 };
      
    int m = arr1.length;
    int n = arr2.length;
  
    // Function calling
    System.out.print(CountCommon(arr1, arr2, m, n));
}
}
  
// This code is contributed by Nidhi_biet

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to count the
# common elements of the two arithmetic
# progression of the given sequence
  
# Function to find GCD
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)
  
# Function to find LCM
def findlcm(a, b):
    return a * b // gcd(a, b)
  
# Function to count Common Element 
# of arr1[] and arr2[]
def CountCommon(arr1, arr2, m, n):
      
    # Common Difference
    diff1 = arr1[1] - arr1[0]
    diff2 = arr2[1] - arr2[0]
  
    # Function calling
    lcm = findlcm(diff1, diff2)
  
    ans1 = (arr1[m - 1] - arr1[0]) // lcm
    ans2 = (arr2[n - 1] - arr2[0]) // lcm
    ans = min(ans1, ans2)
  
    # Print the total Common Element
    print (ans + 1)
  
# Driver Code
if __name__ == "__main__":
    arr1 = [ 2, 5, 8, 11, 14, 17 ]
    arr2 = [ 2, 4, 6, 8, 10, 12 ]
    m = len(arr1)
    n = len(arr2)
  
    # Function calling
    CountCommon(arr1, arr2, m, n)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to count the
// common elements of the two arithmetic
// progression of the given sequence
using System;
class GFG{
  
// Function to find GCD
static int gcd(int a,int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
  
// Function to find LCM
static int findlcm(int a, int b)
{
    int gc = gcd(a, b);
    return a * b / gc;
  
// Function to count common element 
// of arr1[] and arr2[]
int CountCommon(int []arr1, 
                int []arr2, 
                int m, int n)
{
      
    // Common Difference
    int diff1 = arr1[1] - arr1[0];
    int diff2 = arr2[1] - arr2[0];
  
    // Function calling
    int lcm = findlcm(diff1, diff2);
  
    int ans1 = (arr1[m - 1] - arr1[0]) / lcm;
    int ans2 = (arr2[n - 1] - arr2[0]) / lcm;
    int ans = min(ans1, ans2);
      
    return (ans + 1);
  
// Driver code
public static void Main()
{
    int []arr1 = { 2, 5, 8, 11, 14, 17 };
    int []arr2 = { 2, 4, 6, 8, 10, 12 };
      
    int m = arr1.Length;
    int n = arr2.Length;
  
    // Function calling
    Console.Write(CountCommon(arr1, arr2, m, n));
}
}
  
// This code is contributed by Code_Mech 

chevron_right


Output:

2

Time Complexity:O(log(max(diff1, diff2)))

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.