Skip to content
Related Articles
Maximize GCD of all possible pairs from 1 to N
• Difficulty Level : Medium
• Last Updated : 05 Apr, 2021

Given an integer N (? 2), the task is to find the maximum GCD among all pairs possible by the integers in the range [1, N].

Example:

Input: N = 5
Output:
Explanation :
GCD(1, 2) : 1
GCD(1, 3) : 1
GCD(1, 4) : 1
GCD(1, 5) : 1
GCD(2, 3) : 1
GCD(2, 4) : 2
GCD(2, 5) : 1
GCD(3, 4) : 1
GCD(3, 5) : 1
GCD(4, 5) : 1

Input: N = 6
Output:
Explanation: GCD of pair (3, 6) is the maximum.

Naive Approach:
The simplest approach to solve the problem is to generate all possible pairs from [1, N] and calculate GCD of each pair. Finally, print the maximum GCD obtained.
Time Complexity: O(N2logN)
Auxiliary Space: O(1)

Efficient Approach:
Follow the steps below to solve the problem:

• Since all the pairs are distinct, then, for any pair {a, b} with GCD g, either of a or b is greater than g.
• Considering b to be the greater number, b ? 2g, since 2g is the smallest multiple of g greater than it.
• Since b cannot exceed N, and 2g ? N.
• Therefore, g ? floor(n/2).
• Therefore, the maximum GCD that can be obtained is floor(n/2), when pair chosen is (floor(n/2), 2*floor(n/2)).

Illustration:
N = 6
Maximum GCD = 6/2 = 3, occurs for the pair (3, 6)

Below is the implementation of the above approach:

## C++

 `// C++ Program to implement``// the approach``#include ``using` `namespace` `std;` `// Function to obtain the maximum``// gcd of all pairs from 1 to n``void` `find(``int` `n)``{``    ``// Print the answer``    ``cout << n / 2 << endl;``}` `// Driver code``int` `main()``{``    ``int` `n = 5;``    ``// Function call``    ``find(n);``    ``return` `0;``}`

## Java

 `// Java Program to implement``// the approach``class` `GFG{``  ` `// Function to obtain the maximum``// gcd of all pairs from 1 to n``static` `void` `find(``int` `n)``{``    ``// Print the answer``    ``System.out.println(n / ``2``);``}`` ` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `n = ``5``;``    ``// Function call``    ``find(n);``}``}` `// This code is contributed by Ritik Bansal`

## Python3

 `# Python3 program to implement``# the approach` `# Function to obtain the maximum``# gcd of all pairs from 1 to n``def` `find(n):` `    ``# Print the answer``    ``print``(n ``/``/` `2``)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:` `    ``# Given n``    ``n ``=` `5` `    ``# Function call``    ``find(n)` `# This code is contributed by Shivam Singh`

## C#

 `// C# Program to implement``// the approach``using` `System;``class` `GFG{``   ` `// Function to obtain the maximum``// gcd of all pairs from 1 to n``static` `void` `find(``int` `n)``{``    ``// Print the answer``    ``Console.Write(n / 2);``}``  ` `// Driver code``public` `static` `void` `Main(``string``[] args)``{``    ``int` `n = 5;``    ``// Function call``    ``find(n);``}``}`` ` `// This code is contributed by rock_cool`

## Javascript

 ``
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.  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 industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up