Open In App

Maximize GCD of all possible pairs from 1 to N

Improve
Improve
Like Article
Like
Save
Share
Report

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 <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;
}


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




<script>
 
// Javascript program to implement
// the approach
 
// Function to obtain the maximum
// gcd of all pairs from 1 to n
function find(n)
{
     
    // Print the answer
    document.write(parseInt(n / 2, 10) + "</br>");
}
 
// Driver code
let n = 5;
 
// Function call
find(n);
 
// This code is contributed by divyeshrabadiya07
 
</script>


Output: 

2

 

Time Complexity: O(1) 
Auxiliary Space: O(1)
 



Last Updated : 15 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads