Given an array of pairs **arr[]** of two numbers **{N, M}**, the task is to find the maximum count of common divisors for each pair **N** and **M** such that every pairs between the common divisor is co-prime.

A number

xis a common divisor ofNandMif,N%x = 0andM%x = 0.

Two numbers are co-prime if their Greatest Common Divisor is 1.

**Examples:**

Input:arr[][] = {{12, 18}, {420, 660}}Output:3 4Explanation:

For pair (12, 18):

{1, 2, 3} are common divisors of both 12 and 18, and are pairwise co-prime.

For pair (420, 660):

{1, 2, 3, 5} are common divisors of both 12 and 18, and are pairwise co-prime.Input:arr[][] = {{8, 18}, {20, 66}}Output:2 2

**Approach:** The maximum count of common divisors of **N** and **M** such that the **GCD** of all the pairs between them is always **1** is 1 and all the common prime divisors of **N and M**. To count all the common prime divisors the idea is to find the GCD(say **G**) of the given two numbers and then count the number of prime divisors of the number **G**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the gcd of` `// two numbers` `int` `gcd(` `int` `x, ` `int` `y)` `{` ` ` `if` `(x % y == 0)` ` ` `return` `y;` ` ` `else` ` ` `return` `gcd(y, x % y);` `}` `// Function to of pairwise co-prime` `// and common divisors of two numbers` `int` `countPairwiseCoprime(` `int` `N, ` `int` `M)` `{` ` ` `// Initialize answer with 1,` ` ` `// to include 1 in the count` ` ` `int` `answer = 1;` ` ` `// Count of primes of gcd(N, M)` ` ` `int` `g = gcd(N, M);` ` ` `int` `temp = g;` ` ` `// Finding prime factors of gcd` ` ` `for` `(` `int` `i = 2; i * i <= g; i++) {` ` ` `// Increment count if it is` ` ` `// divisible by i` ` ` `if` `(temp % i == 0) {` ` ` `answer++;` ` ` `while` `(temp % i == 0)` ` ` `temp /= i;` ` ` `}` ` ` `}` ` ` `if` `(temp != 1)` ` ` `answer++;` ` ` `// Return the total count` ` ` `return` `answer;` `}` `void` `countCoprimePair(` `int` `arr[][2], ` `int` `N)` `{` ` ` `// Function Call for each pair` ` ` `// to calculate the count of` ` ` `// pairwise co-prime divisors` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `cout << countPairwiseCoprime(arr[i][0],` ` ` `arr[i][1])` ` ` `<< ` `' '` `;` ` ` `}` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given array of pairs` ` ` `int` `arr[][2] = { { 12, 18 }, { 420, 660 } };` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `// Function Call` ` ` `countCoprimePair(arr, N);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach` `class` `GFG{` `// Function to find the gcd of` `// two numbers` `static` `int` `gcd(` `int` `x, ` `int` `y)` `{` ` ` `if` `(x % y == ` `0` `)` ` ` `return` `y;` ` ` `else` ` ` `return` `gcd(y, x % y);` `}` `// Function to of pairwise co-prime` `// and common divisors of two numbers` `static` `int` `countPairwiseCoprime(` `int` `N, ` `int` `M)` `{` ` ` `// Initialize answer with 1,` ` ` `// to include 1 in the count` ` ` `int` `answer = ` `1` `;` ` ` `// Count of primes of gcd(N, M)` ` ` `int` `g = gcd(N, M);` ` ` `int` `temp = g;` ` ` `// Finding prime factors of gcd` ` ` `for` `(` `int` `i = ` `2` `; i * i <= g; i++) ` ` ` `{` ` ` `// Increment count if it is` ` ` `// divisible by i` ` ` `if` `(temp % i == ` `0` `) ` ` ` `{` ` ` `answer++;` ` ` `while` `(temp % i == ` `0` `)` ` ` `temp /= i;` ` ` `}` ` ` `}` ` ` `if` `(temp != ` `1` `)` ` ` `answer++;` ` ` `// Return the total count` ` ` `return` `answer;` `}` `static` `void` `countCoprimePair(` `int` `arr[][], ` `int` `N)` `{` ` ` `// Function Call for each pair` ` ` `// to calculate the count of` ` ` `// pairwise co-prime divisors` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) ` ` ` `{` ` ` `System.out.print(countPairwiseCoprime(arr[i][` `0` `],` ` ` `arr[i][` `1` `]) + ` `" "` `);` ` ` `}` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `// Given array of pairs` ` ` `int` `arr[][] = { { ` `12` `, ` `18` `}, { ` `420` `, ` `660` `} };` ` ` `int` `N = arr.length;` ` ` `// Function Call` ` ` `countCoprimePair(arr, N);` `}` `}` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `# Function to find the gcd of` `# two numbers` `def` `gcd(x, y):` ` ` `if` `(x ` `%` `y ` `=` `=` `0` `):` ` ` `return` `y` ` ` `else` `:` ` ` `return` `gcd(y, x ` `%` `y)` `# Function to of pairwise co-prime` `# and common divisors of two numbers` `def` `countPairwiseCoprime(N, M):` ` ` `# Initialize answer with 1,` ` ` `# to include 1 in the count` ` ` `answer ` `=` `1` ` ` `# Count of primes of gcd(N, M)` ` ` `g ` `=` `gcd(N, M)` ` ` `temp ` `=` `g` ` ` `# Finding prime factors of gcd` ` ` `for` `i ` `in` `range` `(` `2` `, g ` `+` `1` `):` ` ` `if` `i ` `*` `i > g:` ` ` `break` ` ` `# Increment count if it is` ` ` `# divisible by i` ` ` `if` `(temp ` `%` `i ` `=` `=` `0` `) :` ` ` `answer ` `+` `=` `1` ` ` `while` `(temp ` `%` `i ` `=` `=` `0` `):` ` ` `temp ` `/` `/` `=` `i` ` ` `if` `(temp !` `=` `1` `):` ` ` `answer ` `+` `=` `1` ` ` `# Return the total count` ` ` `return` `answer` `def` `countCoprimePair(arr, N):` ` ` `# Function Call for each pair` ` ` `# to calculate the count of` ` ` `# pairwise co-prime divisors` ` ` `for` `i ` `in` `range` `(N):` ` ` `print` `(countPairwiseCoprime(arr[i][` `0` `], ` ` ` `arr[i][` `1` `]),` ` ` `end ` `=` `" "` `)` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Given array of pairs` ` ` `arr` `=` `[ [ ` `12` `, ` `18` `], [ ` `420` `, ` `660` `] ]` ` ` `N ` `=` `len` `(arr)` ` ` `# Function Call` ` ` `countCoprimePair(arr, N)` `# This code is contributed by Mohit Kumar` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` `// Function to find the gcd of` `// two numbers` `static` `int` `gcd(` `int` `x, ` `int` `y)` `{` ` ` `if` `(x % y == 0)` ` ` `return` `y;` ` ` `else` ` ` `return` `gcd(y, x % y);` `}` `// Function to of pairwise co-prime` `// and common divisors of two numbers` `static` `int` `countPairwiseCoprime(` `int` `N, ` `int` `M)` `{` ` ` `// Initialize answer with 1,` ` ` `// to include 1 in the count` ` ` `int` `answer = 1;` ` ` `// Count of primes of gcd(N, M)` ` ` `int` `g = gcd(N, M);` ` ` `int` `temp = g;` ` ` `// Finding prime factors of gcd` ` ` `for` `(` `int` `i = 2; i * i <= g; i++) ` ` ` `{` ` ` `// Increment count if it is` ` ` `// divisible by i` ` ` `if` `(temp % i == 0) ` ` ` `{` ` ` `answer++;` ` ` `while` `(temp % i == 0)` ` ` `temp /= i;` ` ` `}` ` ` `}` ` ` `if` `(temp != 1)` ` ` `answer++;` ` ` `// Return the total count` ` ` `return` `answer;` `}` `static` `void` `countCoprimePair(` `int` `[,]arr, ` `int` `N)` `{` ` ` `// Function Call for each pair` ` ` `// to calculate the count of` ` ` `// pairwise co-prime divisors` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{` ` ` `Console.Write(countPairwiseCoprime(arr[i, 0],` ` ` `arr[i, 1]) + ` `" "` `);` ` ` `}` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `// Given array of pairs` ` ` `int` `[,]arr = { { 12, 18 }, { 420, 660 } };` ` ` `int` `N = arr.GetLength(0);` ` ` `// Function Call` ` ` `countCoprimePair(arr, N);` `}` `}` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

**Output:**

3 4

**Time Complexity:** *O(X*(sqrt(N) + sqrt(M))), where X is the number of pairs and N & M are two pairs in arr[].***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.

## Recommended Posts:

- Find the length of the Largest subset such that all elements are Pairwise Coprime
- Count of integers up to N which are non divisors and non coprime with N
- Count all pairs of divisors of a number N whose sum is coprime with N
- Coprime divisors of a number
- Divide the two given numbers by their common divisors
- Check if a number has an odd count of odd divisors and even count of even divisors
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Sum of common divisors of two numbers A and B
- Maximum count of common divisors of A and B such that all are co-primes to one another
- Check if count of even divisors of N is equal to count of odd divisors
- Common Divisors of Two Numbers
- C++ Program for Common Divisors of Two Numbers
- Java Program for Common Divisors of Two Numbers
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Partition first N natural number into two sets such that their sum is not coprime
- Largest Coprime Set Between two integers
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Composite XOR and Coprime AND
- Common divisors of N numbers
- Count divisors of n that have at-least one digit common with n

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.