# Find pair with maximum GCD for integers in range 2 to N

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

Explaination:

Maximum possible GCD between all possible pairs is 5 which occurs for the pair (10, 5).

Input:N = 13

Output:6

Explaination:

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

`}`

*chevron_right**filter_none*## 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`

*chevron_right**filter_none***Output:**5 10

**Time Complexity:**O(1)

**Auxiliary Space:**O(1)Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find a pair with maximum product in array of Integers
- Maximum Bitwise AND pair from given range
- Maximum Bitwise AND pair (X, Y) from given range such that X and Y can be same
- Find sum in range L to R in given sequence of integers
- Find Prime Adam integers in the given range [L, R]
- Find a distinct pair (x, y) in given range such that x divides y
- Given an array and two integers l and r, find the kth largest element in the range [l, r]
- Queries to find the count of integers in a range that contain the given pattern
- Find three integers less than or equal to N such that their LCM is maximum
- Find a pair from the given array with maximum nCr value
- Find Sum of pair from two arrays with maximum sum
- Find pair with maximum GCD in an array
- Find a pair (n,r) in an integer array such that value of nCr is maximum
- Find the maximum sum pair in an Array with even parity
- Find pair with maximum ratio in an Array
- Find a pair (n,r) in an integer array such that value of nPr is maximum
- Find integers that divides maximum number of elements of the array
- Find the maximum range [L,R] whose sum is divisible by M
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Probability of a random pair being the maximum weighted pair

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.