Find minimum GCD of all pairs in an array
Last Updated :
18 Apr, 2023
Given an array arr of positive integers, the task is to find the minimum GCD possible for any pair of the given array.
Examples:
Input: arr[] = {1, 2, 3, 4, 5}
Output: 1
Explanation:
GCD(1, 2) = 1.
Input: arr[] = {2, 4, 6, 8, 3}
Output: 1
Naive approach: Generate all the pairs of elements and take GCD of them, finally minimizing the output with the result.
- Initialize the result with the maximum possible gcd
- Iterate over the array
- Generate every pair
- Find the gcd of pair of elements
- Minimize the result with output.
- Return result
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int find_min_GCD_Pair(int arr[], int N)
{
// Initialise the result with maximum possible gcd
int result = INT_MAX;
// Iterate over the array
for (int i = 0; i < N; i++) {
// Generate every pair
for (int j = i + 1; j < N; j++) {
// Find the gcd of pair of elements
int gcd = __gcd(arr[i], arr[j]);
// Minimise the result with output.
result = min(result, gcd);
}
}
// return result
return result;
}
int main()
{
int arr[] = { 6, 10, 15 };
int N = 3;
cout << find_min_GCD_Pair(arr, N);
return 0;
}
Python3
# Python Equivalent
import math
def find_min_GCD_Pair(arr, N):
# Initialise the result with maximum possible gcd
result = float('inf')
# Iterate over the array
for i in range(N):
# Generate every pair
for j in range(i+1, N):
# Find the gcd of pair of elements
gcd = math.gcd(arr[i], arr[j])
# Minimise the result with output.
result = min(result, gcd)
# return result
return result
if __name__ == "__main__":
arr = [6, 10, 15]
N = 3
print(find_min_GCD_Pair(arr, N))
Java
// Java program for the above approach
import java.util.*;
public class Main {
public static int findMinGCDPair(int[] arr, int n)
{
// Initialize the result with maximum possible gcd
int result = Integer.MAX_VALUE;
// Iterate over the array
for (int i = 0; i < n; i++) {
// Generate every pair
for (int j = i + 1; j < n; j++) {
// Find the gcd of pair of elements
int gcd = gcd(arr[i], arr[j]);
// Minimise the result with output.
result = Math.min(result, gcd);
}
}
// return result
return result;
}
public static int gcd(int a, int b)
{
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static void main(String[] args)
{
int[] arr = { 6, 10, 15 };
int n = 3;
System.out.println(findMinGCDPair(arr, n));
}
}
// Contributed by adityasharmadev01
JavaScript
// JS Equivalent
const findMinGCDPair = (arr, N) => {
// Initialise the result with maximum possible gcd
let result = Infinity;
// Iterate over the array
for (let i = 0; i < N; i++) {
// Generate every pair
for (let j = i + 1; j < N; j++) {
// Find the gcd of pair of elements
let gcd = gcdFunc(arr[i], arr[j]);
// Minimise the result with output.
result = Math.min(result, gcd);
}
}
// return result
return result;
}
const gcdFunc = (a, b) => {
if (b === 0) {
return a;
}
return gcdFunc(b, a % b);
};
const arr = [6, 10, 15];
const N = 3;
console.log(findMinGCDPair(arr, N));
C#
// C# code addition
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program {
public static int _gcd(int a, int b)
{
if (a == 0)
return b;
return _gcd(b % a, a);
}
// The function finds the minimum gcd pair
public static int find_min_GCD_Pair(int[] arr, int N)
{
// Initialise the result with maximum possible gcd
int result = int.MaxValue;
// Iterate over the array
for (int i = 0; i < N; i++) {
// Generate every pair
for (int j = i + 1; j < N; j++) {
// Find the gcd of pair of elements
int gcd = _gcd(arr[i], arr[j]);
// Minimise the result with output.
result = Math.Min(result, gcd);
}
}
// return result
return result;
}
// Driver code.
static void Main()
{
int[] arr = { 6, 10, 15 };
int N = 3;
Console.WriteLine(find_min_GCD_Pair(arr, N));
}
}
// The code is contributed by Arushi Goel.
Time Complexity: O(N2), where N is the length of the given input array.
Auxiliary Space: O(1)
Similar Reads
Find pair with maximum GCD in an array We are given an array of positive integers. Find the pair in array with maximum GCD.Examples: Input : arr[] : { 1 2 3 4 5 }Output : 2Explanation : Pair {2, 4} has GCD 2 which is highest. Other pairs have a GCD of 1.Input : arr[] : { 2 3 4 8 8 11 12 }Output : 8Explanation : Pair {8, 8} has GCD 8 whic
15+ min read
Minimum LCM of all pairs in a given array Given an array arr[] of size N, the task is to find the minimum LCM (Least Common Multiple) of all unique pairs in the given array, where 1 <= N <= 105, 1 <= arr[i] <= 105. Examples: Input: arr[] = {2, 4, 3} Output: 4 Explanation LCM (2, 4) = 4 LCM (2, 3) = 6 LCM (4, 3) = 12 Minimum poss
12 min read
Find the GCD of LCM of all unique pairs in an Array Given an integer array arr[] of size N, the task is to find the GCD of LCM of all unique pair (i, j) of the array, such that i < j.Examples: Input: arr[] = {10, 24, 40, 80} Output: 40 Explanation: LCM of all unique pairs following given conditions are: LCM(10, 24) = 120 LCM(10, 40) = 40 LCM(10, 8
9 min read
Number of co-prime pairs in an array Given an array arr[], count the number of pairs (i, j) such that i < j and gcd(arr[i], arr[j]) == 1. Examples: Input : arr[] = [1, 2, 3] Output : 3 Explanation : Here, Co-prime pairs are (1, 2), (2, 3), (1, 3) .Input : arr[] = [4, 8, 3, 9] Output : 4 Explanation : Here, Co-prime pairs are (4, 3),
12 min read
Minimum gcd operations to make all array elements one Given an array A[] of size N. You can replace any number in the array with the gcd of that element with any of its adjacent elements. Find the minimum number of such operation to make the element of whole array equal to 1. If its not possible print -1. Examples: Input : A[] = {4, 8, 9} Output : 3 Ex
8 min read