Given a number N, the task is to find a pair of integers in the range [2, N] with maximum GCD.
Examples:
Input: N = 10
Output: 5
Explanation:
Maximum possible GCD between all possible pairs is 5 which occurs for the pair (10, 5).
Input: N = 13
Output: 6
Explanation:
Maximum possible GCD between all possible pairs is 6 which occurs for the pair (12, 6).
Approach:
Follow the steps below to solve the problem:
- If N is even, return the pair {N, N / 2}.
Illustration:
If N = 10, Maximum possible GCD for any pair is 5( for the pair {5, 10}).
If N = 20, Maximum possible GCD for any pair is 10( for the pair {20, 10}).
- If N is odd, then return the pair{N – 1, (N – 1) / 2}.
Illustration:
If N = 11, Maximum possible GCD for any pair is 5( for the pair {5, 10}).
If N = 21, Maximum possible GCD for any pair is 10( for the pair {20, 10}).
Below is the implementation of the above approach:
C++
// C++ Program to find a pair of // integers less than or equal // to N such that their GCD // is maximum #include <bits/stdc++.h> using namespace std; // Function to find the required // pair whose GCD is maximum void solve( int N) { // If N is even if (N % 2 == 0) { cout << N / 2 << " " << N << endl; } // If N is odd else { cout << (N - 1) / 2 << " " << (N - 1) << endl; } } // Driver Code int main() { int N = 10; solve(N); return 0; } |
Java
// Java program to find a pair of // integers less than or equal // to N such that their GCD // is maximum class GFG{ // Function to find the required // pair whose GCD is maximum static void solve( int N) { // If N is even if (N % 2 == 0 ) { System.out.print(N / 2 + " " + N + "\n" ); } // If N is odd else { System.out.print((N - 1 ) / 2 + " " + (N - 1 ) + "\n" ); } } // Driver Code public static void main(String[] args) { int N = 10 ; solve(N); } } // This code is contributed by Amit Katiyar |
Python3
# Python3 Program to find a pair # of integers less than or equal # to N such that their GCD # is maximum # Function to find the required # pair whose GCD is maximum def solve(N): # If N is even if (N % 2 = = 0 ): print (N / / 2 , N) # If N is odd else : print ((N - 1 ) / / 2 , (N - 1 )) # Driver Code N = 10 solve(N) # This code is contributed by divyamohan123 |
C#
// C# program to find a pair of // integers less than or equal // to N such that their GCD // is maximum using System; class GFG{ // Function to find the required // pair whose GCD is maximum static void solve( int N) { // If N is even if (N % 2 == 0) { Console.Write(N / 2 + " " + N + "\n" ); } // If N is odd else { Console.Write((N - 1) / 2 + " " + (N - 1) + "\n" ); } } // Driver Code public static void Main(String[] args) { int N = 10; solve(N); } } // This code is contributed by shivanisinghss2110 |
Javascript
<script> // JavaScript program to find a pair of // integers less than or equal // to N such that their GCD // is maximum // Function to find the required // pair whose GCD is maximum function solve(N) { // If N is even if (N % 2 == 0) { document.write(N / 2 + " " + N + "<br/>" ); } // If N is odd else { document.write((N - 1) / 2 + " " + (N - 1) + "<br/>" ); } } // Driver Code var N = 10; solve(N); // This code is contributed by todaysgaurav </script> |
5 10
Time Complexity: O(1)
Auxiliary Space: O(1)
New Approach:-
Another approach to solve this problem is to use a nested loop to iterate over all possible pairs of integers in the range [2, N] and calculate their GCD using the Euclidean algorithm. We can keep track of the maximum GCD seen so far and the pair of integers that produced it. The time complexity of this approach would be O(N^2) since we are iterating over N^2 pairs of integers.
Algorithm:
- Define a function “gcd” to calculate the GCD of two numbers.
- Define a function “findMaxGCDPair” that takes an integer N as input.
- Initialize the maximum GCD to -1 and x, y to 0.
- Loop through all pairs of integers i and j in the range [2, N].
- Calculate the GCD of i and j using the “gcd” function.
- If the GCD is greater than the current maximum GCD, update the maximum GCD and set x and y to i and j, respectively.
- Print the pair (x, y) with the maximum GCD.
- In the main function, call the “findMaxGCDPair” function with the input value N.
- End of the program.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h> using namespace std; // Function to calculate the GCD of two numbers int gcd( int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } // Function to find the pair of integers with maximum GCD in the range [2, N] void findMaxGCDPair( int N) { int max_gcd = -1; int x, y; for ( int i = 2; i <= N; i++) { for ( int j = i + 1; j <= N; j++) { int cur_gcd = gcd(i, j); if (cur_gcd > max_gcd) { max_gcd = cur_gcd; x = i; y = j; } } } cout << "Pair with maximum GCD: " << x << " " << y << endl; } // Driver code int main() { int N = 10; findMaxGCDPair(N); return 0; } |
Java
import java.util.*; public class Main { // Function to calculate the GCD of two numbers public static int gcd( int a, int b) { if (b == 0 ) { return a; } return gcd(b, a % b); } // Function to find the pair of integers with maximum GCD in the range [2, N] public static void findMaxGCDPair( int N) { int max_gcd = - 1 ; int x = 0 , y = 0 ; for ( int i = 2 ; i <= N; i++) { for ( int j = i + 1 ; j <= N; j++) { int cur_gcd = gcd(i, j); if (cur_gcd > max_gcd) { max_gcd = cur_gcd; x = i; y = j; } } } System.out.println( "Pair with maximum GCD: " + x + " " + y); } // Driver code public static void main(String[] args) { int N = 10 ; findMaxGCDPair(N); } } |
C#
using System; public class Program { // Function to calculate the GCD of two numbers public static int gcd( int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } // Function to find the pair of integers with maximum GCD in the range [2, N] public static void findMaxGCDPair( int N) { int max_gcd = -1; int x = 0, y = 0; for ( int i = 2; i <= N; i++) { for ( int j = i + 1; j <= N; j++) { int cur_gcd = gcd(i, j); if (cur_gcd > max_gcd) { max_gcd = cur_gcd; x = i; y = j; } } } Console.WriteLine( "Pair with maximum GCD: " + x + " " + y); } // Driver code public static void Main() { int N = 10; findMaxGCDPair(N); } } |
Javascript
function gcd(a, b) { if (b == 0) { return a; } return gcd(b, a % b); } function findMaxGCDPair(N) { let max_gcd = -1; let x, y; for (let i = 2; i <= N; i++) { for (let j = i + 1; j <= N; j++) { let cur_gcd = gcd(i, j); if (cur_gcd > max_gcd) { max_gcd = cur_gcd; x = i; y = j; } } } console.log( "Pair with maximum GCD: " + x + " " + y); } let N = 10; findMaxGCDPair(N); |
Python
# Function to calculate the GCD of two numbers def gcd(a, b): if b = = 0 : return a return gcd(b, a % b) # Function to find the pair of integers with maximum GCD in the range [2, N] def findMaxGCDPair(N): max_gcd = - 1 x, y = 0 , 0 for i in range ( 2 , N + 1 ): for j in range (i + 1 , N + 1 ): cur_gcd = gcd(i, j) if cur_gcd > max_gcd: max_gcd = cur_gcd x, y = i, j print ( "Pair with maximum GCD: {} {}" . format (x, y)) # Driver code N = 10 findMaxGCDPair(N) |
Pair with maximum GCD: 5 10
“Note that this approach is not very efficient for large values of N, since the time complexity is quadratic. However, it can be useful for small values of N.”
Time Complexity: O(N2)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!