Maximum GCD among all pairs ( i , j ) of first N natural numbers | C ++ program for the above approach ; Function to find maximum gcd of all pairs possible from first n natural numbers ; Stores maximum gcd ; Iterate over all possible pairs ; Update maximum GCD ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxGCD ( int n ) { int maxHcf = INT_MIN ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = i + 1 ; j <= n ; j ++ ) { maxHcf = max ( maxHcf , __gcd ( i , j ) ) ; } } return maxHcf ; } int main ( ) { int n = 4 ; cout << maxGCD ( n ) ; return 0 ; }