Related Articles

# Find GCD of each element of array B[] added to all elements of array A[]

• Difficulty Level : Medium
• Last Updated : 03 Aug, 2021

Given two arrays a[] and b[] of length n and m respectively, the task is to find Greatest Common Divisor(GCD) of {a + b[i], a + b[i], a + b[i], …, a[n – 1] + b[i]} ( where 0 <= i <= m – 1).

Input: a[] = {1, 10, 22, 64},  b[] = {5, 23, 14, 13}
Output: 3 3 3 1
Explanation:

• i = 1 :  GCD(5+1, 5+10, 5+22, 5+64) = GCD(6, 15, 27, 69) = 3
• i = 2 : GCD(23+1, 23+10, 23+22, 23+64) = GCD(24, 33, 45, 87) = 3
• i = 3 : GCD(14+1, 14+10, 14+22, 14+64) = GCD(15, 24, 36, 78) = 3
• i = 4 : GCD(13+1, 13+10, 13+22, 13+64) = GCD(14, 23, 35, 77) = 1

Input: a[] = {12, 30},  b[] = {5, 23, 14, 13}
Output: 1 1 2 1

Approach: The problem can be solved efficiently using the property of Euclidean GCD Algorithm which states GCD(x, y) = GCD(x−y, y). The same applies for multiple arguments GCD(x, y, z, …) = GCD(x−y. y, z, …). Applying this property, the problem can be solved using the steps given below:

• To calculate GCD(a + b[i], …, a[n – 1] + b[i]), subtract a + b[i] from all other arguments.
• Now, GCD ( a + b[i], …, a[n – 1] + b[i]) = GCD ( a + b[i], a − a, …, a[n – 1] − a).
• Find G = GCD(a − a, …, a[n – 1] − a), then gcd for any i can be calculated to be GCD(a + b[i], G).
• Iterate over every possible value of i from 1 to m and calculate gcd as G(i) = GCD(a+b[i], G), and print G(i).

Below is the implementation of the above approach.

## C++

 `// C++ program for above approach``#include ``using` `namespace` `std;` `#define ll long long` `// Function to calculate gcd for every i``void` `findGCDs(vector a, vector b)``{``    ``ll g = 0;` `    ``int` `n = a.size();``    ``int` `m = b.size();` `    ``// Update GCD of a - a,``    ``// a - a, .... a[n - 1] - a``    ``for` `(``int` `i = 1; i < n; i++) {` `        ``g = __gcd(g, a[i] - a);``    ``}` `    ``// Print gcd(g, a + b[j])``    ``for` `(``int` `j = 0; j < m; j++) {` `        ``cout << __gcd(g, a + b[j]) << ``" "``;``    ``}``}` `// Driver Code``int` `main()``{``    ``// Given Input``    ``vector a = { 1, 10, 22, 64 },``               ``b = { 5, 23, 14, 13 };` `    ``// Function Call``    ``findGCDs(a, b);` `    ``return` `0;``}`

## Java

 `// Java code for the above approach``public` `class` `GFG{``    ``static` `int` `gcd(``int` `a, ``int` `b)``    ``{    ``       ``if` `(b == ``0``)``          ``return` `a;``       ``return` `gcd(b, a % b);``    ``}``    ` `    ``// Function to calculate gcd for every i``    ``static` `void` `findGCDs(``int``[] a, ``int``[] b)``    ``{``        ``int` `g = ``0``;``    ` `        ``int` `n = a.length;``        ``int` `m = b.length;``    ` `        ``// Update GCD of a - a,``        ``// a - a, .... a[n - 1] - a``        ``for` `(``int` `i = ``1``; i < n; i++) {``    ` `            ``g = gcd(g, a[i] - a[``0``]);``        ``}``    ` `        ``// Print gcd(g, a + b[j])``        ``for` `(``int` `j = ``0``; j < m; j++) {``    ` `            ``System.out.print(gcd(g, a[``0``] + b[j]) + ``" "``);``        ``}``    ``}``    ` `    ``// Driver Code``public` `static` `void` `main(String args[])``   ``{``      ` `    ``// Given Input``    ``int``[] a = { ``1``, ``10``, ``22``, ``64` `},``        ``b = { ``5``, ``23``, ``14``, ``13` `};` `    ``// Function Call``    ``findGCDs(a, b);``   ``}``}` `// This code is contributed by SoumikMondal.`

## Python3

 `# Python program for above approach``import` `math` `# Function to calculate gcd for every i``def` `findGCDs( a,  b):``    ``g ``=` `0``    ``n ``=` `len``(a)``    ``m ``=` `len``(b)``    ` `    ``# Update GCD of a - a,``    ``# a - a, .... a[n - 1] - a``    ``for` `i ``in` `range``(``1``,n):``        ``g ``=` `math.gcd(g, a[i] ``-` `a[``0``])``    ` `    ``# Pr gcd(g, a + b[j])``    ``for` `j ``in` `range``(m):``        ``print``(math.gcd(g, a[``0``] ``+` `b[j]), end``=``" "``)``    ` `# Driver Code` `# Given Input``a ``=` `[``1``, ``10``, ``22``, ``64``]``b ``=` `[``5``, ``23``, ``14``, ``13``]` `# Function Call``findGCDs(a, b)` `#This code is contributed by shubhamsingh10`

## C#

 `//C# code for the above approach``using` `System;` `public` `class` `GFG{``    ``static` `int` `gcd(``int` `a, ``int` `b)``    ``{    ``       ``if` `(b == 0)``          ``return` `a;``       ``return` `gcd(b, a % b);``    ``}``    ` `    ``// Function to calculate gcd for every i``    ``static` `void` `findGCDs(``int``[] a, ``int``[] b)``    ``{``        ``int` `g = 0;``    ` `        ``int` `n = a.Length;``        ``int` `m = b.Length;``    ` `        ``// Update GCD of a - a,``        ``// a - a, .... a[n - 1] - a``        ``for` `(``int` `i = 1; i < n; i++) {``    ` `            ``g = gcd(g, a[i] - a);``        ``}``    ` `        ``// Print gcd(g, a + b[j])``        ``for` `(``int` `j = 0; j < m; j++) {``    ` `            ``Console.Write(gcd(g, a + b[j]) + ``" "``);``        ``}``    ``}``    ` `    ``// Driver Code``    ``static` `public` `void` `Main ()``   ``{``      ` `    ``// Given Input``    ``int``[] a = { 1, 10, 22, 64 },``        ``b = { 5, 23, 14, 13 };` `    ``// Function Call``    ``findGCDs(a, b);``   ``}``}` `// This code is contributed by shubhamsingh10.`

## Javascript

 ``

Output:
`3 3 3 1`

Time Complexity: O((N + M) * log(X)), where M is the maximum element in the array a[].
Auxiliary Space: O(1)

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up