# Maximum GCD among all pairs (i, j) of first N natural numbers

Given a positive integer N > 1, the task is to find the maximum GCD among all the pairs (i, j) such that i < j < N.
Examples:

Input: N = 3
Output: 3
Explanation:
All the possible pairs are: (1, 2) (1, 3) (2, 3) with GCD 1.
Input: N = 4
Output: 2
Explanation:
Out of all the possible pairs the pair with max GCD is (2, 4) with a value 2.

Naive Approach: Generate all possible pair of integers from the range [1, N] and calculate GCD of all pairs. Finally, print the maximum GCD obtained.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach`   `#include ` `using` `namespace` `std;`   `// Function to find maximum gcd` `// of all pairs possible from` `// first n natural numbers` `int` `maxGCD(``int` `n)` `{` `    ``// Stores maximum gcd` `    ``int` `maxHcf = INT_MIN;`   `    ``// Iterate over all possible pairs` `    ``for` `(``int` `i = 1; i <= n; i++) {` `        ``for` `(``int` `j = i + 1; j <= n; j++) {`   `            ``// Update maximum GCD` `            ``maxHcf` `                ``= max(maxHcf, __gcd(i, j));` `        ``}` `    ``}`   `    ``return` `maxHcf;` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `n = 4;` `    ``cout << maxGCD(n);`   `    ``return` `0;` `}`

## Java

 `// Java program for ` `// the above approach` `import` `java.util.*;` `class` `GFG{`   `// Function to find maximum gcd` `// of all pairs possible from` `// first n natural numbers` `static` `int` `maxGCD(``int` `n)` `{` `  ``// Stores maximum gcd` `  ``int` `maxHcf = Integer.MIN_VALUE;`   `  ``// Iterate over all possible pairs` `  ``for` `(``int` `i = ``1``; i <= n; i++) ` `  ``{` `    ``for` `(``int` `j = i + ``1``; j <= n; j++) ` `    ``{` `      ``// Update maximum GCD` `      ``maxHcf = Math.max(maxHcf, ` `                        ``__gcd(i, j));` `    ``}` `  ``}`   `  ``return` `maxHcf;` `}` `  `  `static` `int` `__gcd(``int` `a, ``int` `b)  ` `{  ` `  ``return` `b == ``0` `? a : ` `         ``__gcd(b, a % b);     ` `}` `  `  `// Driver Code` `public` `static` `void` `main(String[] args)` `{` `  ``int` `n = ``4``;` `  ``System.out.print(maxGCD(n));` `}` `}`   `// This code is contributed by Rajput-Ji`

## Python3

 `# Python3 program for ` `# the above approach` `def` `__gcd(a, b):` `    ``if``(b ``=``=` `0``):` `        ``return` `a;` `    ``else``:` `        ``return` `__gcd(b, a ``%` `b);`   `# Function to find maximum gcd` `# of all pairs possible from` `# first n natural numbers` `def` `maxGCD(n):` `  `  `    ``# Stores maximum gcd` `    ``maxHcf ``=` `-``2391734235435``;`   `    ``# Iterate over all possible pairs` `    ``for` `i ``in` `range``(``1``, n ``+` `1``):` `        ``for` `j ``in` `range``(i ``+` `1``, n ``+` `1``):` `          `  `            ``# Update maximum GCD` `            ``maxHcf ``=` `max``(maxHcf, __gcd(i, j));` `    ``return` `maxHcf;`   `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `    ``n ``=` `4``;` `    ``print``(maxGCD(n));`   `# This code is contributed by gauravrajput1`

## C#

 `// C# program for ` `// the above approach` `using` `System;` `class` `GFG{`   `// Function to find maximum gcd` `// of all pairs possible from` `// first n natural numbers` `static` `int` `maxGCD(``int` `n)` `{` `  ``// Stores maximum gcd` `  ``int` `maxHcf = ``int``.MinValue;`   `  ``// Iterate over all possible pairs` `  ``for` `(``int` `i = 1; i <= n; i++) ` `  ``{` `    ``for` `(``int` `j = i + 1; j <= n; j++) ` `    ``{` `      ``// Update maximum GCD` `      ``maxHcf = Math.Max(maxHcf, ` `                        ``__gcd(i, j));` `    ``}` `  ``}`   `  ``return` `maxHcf;` `}` `  `  `static` `int` `__gcd(``int` `a, ``int` `b)  ` `{  ` `  ``return` `b == 0 ? a : ` `         ``__gcd(b, a % b);     ` `}` `  `  `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` `  ``int` `n = 4;` `  ``Console.Write(maxGCD(n));` `}` `}`   `// This code is contributed by 29AjayKumar`

Output:

```2

```

Time Complexity: O(N 2 log N)
Auxiliary Space: O(1)
Efficient Approach: The GCD of N and N / 2 is N / 2 which is the maximum of all GCDs possible for any pair from 1 to N.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the above approach`   `#include ` `using` `namespace` `std;`   `// Function to find the maximum GCD` `// among all the pairs from` `// first n natural numbers` `int` `maxGCD(``int` `n)` `{`   `    ``// Return max GCD` `    ``return` `(n / 2);` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `n = 4;` `    ``cout << maxGCD(n);`   `    ``return` `0;` `}`

## Java

 `// Java implementation of the above approach ` `class` `GFG{ `   `// Function to find the maximum GCD` `// among all the pairs from` `// first n natural numbers` `static` `int` `maxGCD(``int` `n)` `{` `    `  `    ``// Return max GCD` `    ``return` `(n / ``2``);` `}`   `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `n = ``4``;`   `    ``System.out.print(maxGCD(n));` `} ` `} `   `// This code is contributed by Dewanti `

## Python3

 `# Python3 implementation of the` `# above approach `   `# Function to find the maximum GCD` `# among all the pairs from first n` `# natural numbers` `def` `maxGCD(n):`   `    ``# Return max GCD` `    ``return` `(n ``/``/` `2``);`   `# Driver code` `if` `__name__ ``=``=` `'__main__'``:` `    `  `    ``n ``=` `4``;`   `    ``print``(maxGCD(n));`   `# This code is contributed by Amit Katiyar`

## C#

 `// C# implementation of ` `// the above approach ` `using` `System;` `class` `GFG{ `   `// Function to find the maximum GCD` `// among all the pairs from` `// first n natural numbers` `static` `int` `maxGCD(``int` `n)` `{` `  ``// Return max GCD` `  ``return` `(n / 2);` `}`   `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `  ``int` `n = 4;` `  ``Console.Write(maxGCD(n));` `} ` `} `   `// This code is contributed by Rajput-Ji`

Output:

```2

```

Time Complexity: O(1)
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.

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.