Calculate GCD of all pairwise sum of given two Arrays
Last Updated :
04 May, 2022
Given two arrays A[] and B[] of size N and M calculate GCD of all pairwise sum of (A[i]+B[j]) 1<=i<=N and 1<=j<=M.
Examples:
Input: A[] = {1, 7, 25, 55}, B[] = {1, 3, 5}
Output: 2
Explanation: The GCD of all pairwise sum of (A[i]+B[j]) is equals to
GCD(1+1, 1+3, 1+5, 7+1, 7+3, 7+5, 25+1, 25+3, 25+5, 55+1, 55+3, 55+5)
GCD(2, 4, 6, 8, 10, 12, 26, 28, 30, 56, 58, 60) = 2
Input: A[] = {8, 16, 20}, B[] = {12, 24}
Output: 4
Naive Approach: The simple approach of this problem is to calculate all pairwise sums and then calculate their GCD.
Below is the implementation of this approach.
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int calculateGCD(vector< int >& a,
vector< int >& b,
int N, int M)
{
int ans = 0;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
int sum = a[i] + b[j];
ans = gcd(ans, sum);
}
}
return ans;
}
int main()
{
int N = 4, M = 3;
vector< int > A = { 1, 7, 25, 55 };
vector< int > B = { 1, 3, 5 };
cout << calculateGCD(A, B, N, M);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int calculateGCD( int a[],
int b[],
int N, int M)
{
int ans = 0 ;
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < M; j++) {
int sum = a[i] + b[j];
ans = gcd(ans, sum);
}
}
return ans;
}
public static void main (String[] args) {
int N = 4 , M = 3 ;
int A[] = { 1 , 7 , 25 , 55 };
int B[] = { 1 , 3 , 5 };
System.out.print(calculateGCD(A, B, N, M));
}
}
|
Python3
def gcd(a, b):
if b = = 0 :
return a
return gcd(b, a % b)
def calculateGCD(a, b, N, M):
ans = 0
for i in range (N):
for j in range (M):
sum = a[i] + b[j]
ans = gcd(ans, sum )
return ans
N = 4
M = 3
A = [ 1 , 7 , 25 , 55 ]
B = [ 1 , 3 , 5 ]
print (calculateGCD(A, B, N, M))
|
C#
using System;
using System.Numerics;
using System.Collections.Generic;
public class GFG {
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int calculateGCD( int [] a, int [] b, int N, int M)
{
int ans = 0;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
int sum = a[i] + b[j];
ans = gcd(ans, sum);
}
}
return ans;
}
public static void Main( string [] args)
{
int N = 4, M = 3;
int [] A = { 1, 7, 25, 55 };
int [] B = { 1, 3, 5 };
Console.WriteLine(calculateGCD(A, B, N, M));
}
}
|
Javascript
<script>
const gcd = (a, b) => {
if (b == 0)
return a;
return gcd(b, a % b);
}
const calculateGCD = (a, b, N, M) => {
let ans = 0;
for (let i = 0; i < N; i++) {
for (let j = 0; j < M; j++) {
let sum = a[i] + b[j];
ans = gcd(ans, sum);
}
}
return ans;
}
let N = 4, M = 3;
let A = [1, 7, 25, 55];
let B = [1, 3, 5];
document.write(calculateGCD(A, B, N, M));
</script>
|
Time Complexity: O(N*M)
Auxiliary Space: O(N)
Efficient Approach: The problem can be solved efficiently based on the following mathematical observation:
Observation:
- By Euclidean algorithm it can be said that GCD( a, b) = GCD(a-b, b) where(a>b) .
- Then for any j:
GCD(A[0] + B[j], A[1] + B[j], . . ., A[N-1] + B[j]) = GCD(A[0]+B[j], A[1]-A[0], A[2]-A[0], . . ., A[N]-A[0]).
The term GCD(A[1]-A[0], A[2]-A[0], . . ., A[N]-A[0]) is common for all j from 0 to M-1 (say this is X)
- This leaves us with only the elements of type (A[0] + B[j]) for which will calculate their GCD and will get the desired GCD upon calculating their GCD with X.
Follow the below steps to solve this problem:
- First sort both the arrays.
- Then iterate through i = 1 to N-1 and calculate GCD of all pairs of A[i] -A[0] (say X).
- Then iterate through i = 1 to M-1 and calculate GCD of all pairs of A[0] +B[i] (say Y).
- Return the final answer which is GCD of (X and Y).
Below is the implementation of this approach.
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int calculateGCD(vector< int >& a,
vector< int >& b, int N, int M)
{
int ans = 0;
sort(a.begin(), a.end());
sort(b.begin(), b.end());
for ( int i = 1; i < N; i++) {
ans = gcd(ans, a[i] - a[0]);
}
for ( int i = 0; i < M; i++) {
ans = gcd(ans, a[0] + b[i]);
}
return ans;
}
int main()
{
int N = 4, M = 3;
vector< int > A = { 1, 7, 25, 55 };
vector< int > B = { 1, 3, 5 };
cout << calculateGCD(A, B, N, M);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int calculateGCD( int a[],
int b[],
int N, int M)
{
int ans = 0 ;
Arrays.sort(a);
Arrays.sort(b);
for ( int i = 1 ; i < N; i++) {
ans = gcd(ans, a[i] - a[ 0 ]);
}
for ( int i = 0 ; i < M; i++) {
ans = gcd(ans, a[ 0 ] + b[i]);
}
return ans;
}
public static void main(String[] args)
{
int N = 4 , M = 3 ;
int A[] = { 1 , 7 , 25 , 55 };
int B[] = { 1 , 3 , 5 };
System.out.print(calculateGCD(A, B, N, M));
}
}
|
Python3
def gcd(a, b):
if b = = 0 :
return a
return gcd(b, a % b)
def calculateGCD(a, b, N, M):
ans = 0
a.sort()
b.sort()
for i in range ( 1 , N):
ans = gcd(ans, a[i] - a[ 0 ])
for i in range (M):
ans = gcd(ans, a[ 0 ] + b[i])
return ans
N, M = 4 , 3
A = [ 1 , 7 , 25 , 55 ]
B = [ 1 , 3 , 5 ]
print (calculateGCD(A, B, N, M))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int calculateGCD( int [] a,
int [] b,
int N, int M)
{
int ans = 0;
Array.Sort(a);
Array.Sort(b);
for ( int i = 1; i < N; i++) {
ans = gcd(ans, a[i] - a[0]);
}
for ( int i = 0; i < M; i++) {
ans = gcd(ans, a[0] + b[i]);
}
return ans;
}
public static void Main()
{
int N = 4, M = 3;
int [] A = { 1, 7, 25, 55 };
int [] B = { 1, 3, 5 };
Console.Write(calculateGCD(A, B, N, M));
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function calculateGCD(a, b, N, M)
{
var ans = 0;
a.sort()
b.sort()
for ( var i = 1; i < N; i++)
ans = gcd(ans, a[i] - a[0]);
for ( var i = 0; i < M; i++)
ans = gcd(ans, a[0] + b[i]);
return ans;
}
var N = 4;
var M = 3;
var A = [1, 7, 25, 55];
var B = [1, 3, 5];
document.write(calculateGCD(A, B, N, M))
</script>
|
Time Complexity : O(N*logN + M*logM)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...