Maximize GCD of all possible pairs from 1 to N

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to implement
// the approach
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


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.



Improved By : bansal_rtk_, rock_cool

Article Tags :
Practice Tags :


6


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.