给定正整数N> 1 ,任务是在所有对(i,j)中找到最大GCD,以使i
例子:
Input: N = 3
Output: 3
Explanation:
All the possible pairs are: (1, 2) (1, 3) (2, 3) with GCD 1.
Input: N = 4
Output: 2
Explanation:
Out of all the possible pairs the pair with max GCD is (2, 4) with a value 2.
天真的方法:生成范围为[1,N]的所有可能的整数对,并计算所有对的GCD。最后,打印获得的最大GCD。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find maximum gcd
// of all pairs possible from
// first n natural numbers
int maxGCD(int n)
{
// Stores maximum gcd
int maxHcf = INT_MIN;
// Iterate over all possible pairs
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
// Update maximum GCD
maxHcf
= max(maxHcf, __gcd(i, j));
}
}
return maxHcf;
}
// Driver Code
int main()
{
int n = 4;
cout << maxGCD(n);
return 0;
}
Java
// Java program for
// the above approach
import java.util.*;
class GFG{
// Function to find maximum gcd
// of all pairs possible from
// first n natural numbers
static int maxGCD(int n)
{
// Stores maximum gcd
int maxHcf = Integer.MIN_VALUE;
// Iterate over all possible pairs
for (int i = 1; i <= n; i++)
{
for (int j = i + 1; j <= n; j++)
{
// Update maximum GCD
maxHcf = Math.max(maxHcf,
__gcd(i, j));
}
}
return maxHcf;
}
static int __gcd(int a, int b)
{
return b == 0 ? a :
__gcd(b, a % b);
}
// Driver Code
public static void main(String[] args)
{
int n = 4;
System.out.print(maxGCD(n));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for
# the above approach
def __gcd(a, b):
if(b == 0):
return a;
else:
return __gcd(b, a % b);
# Function to find maximum gcd
# of all pairs possible from
# first n natural numbers
def maxGCD(n):
# Stores maximum gcd
maxHcf = -2391734235435;
# Iterate over all possible pairs
for i in range(1, n + 1):
for j in range(i + 1, n + 1):
# Update maximum GCD
maxHcf = max(maxHcf, __gcd(i, j));
return maxHcf;
# Driver Code
if __name__ == '__main__':
n = 4;
print(maxGCD(n));
# This code is contributed by gauravrajput1
C#
// C# program for
// the above approach
using System;
class GFG{
// Function to find maximum gcd
// of all pairs possible from
// first n natural numbers
static int maxGCD(int n)
{
// Stores maximum gcd
int maxHcf = int.MinValue;
// Iterate over all possible pairs
for (int i = 1; i <= n; i++)
{
for (int j = i + 1; j <= n; j++)
{
// Update maximum GCD
maxHcf = Math.Max(maxHcf,
__gcd(i, j));
}
}
return maxHcf;
}
static int __gcd(int a, int b)
{
return b == 0 ? a :
__gcd(b, a % b);
}
// Driver Code
public static void Main(String[] args)
{
int n = 4;
Console.Write(maxGCD(n));
}
}
// This code is contributed by 29AjayKumar
Javascript
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to find the maximum GCD
// among all the pairs from
// first n natural numbers
int maxGCD(int n)
{
// Return max GCD
return (n / 2);
}
// Driver Code
int main()
{
int n = 4;
cout << maxGCD(n);
return 0;
}
Java
// Java implementation of the above approach
class GFG{
// Function to find the maximum GCD
// among all the pairs from
// first n natural numbers
static int maxGCD(int n)
{
// Return max GCD
return (n / 2);
}
// Driver code
public static void main(String[] args)
{
int n = 4;
System.out.print(maxGCD(n));
}
}
// This code is contributed by Dewanti
Python3
# Python3 implementation of the
# above approach
# Function to find the maximum GCD
# among all the pairs from first n
# natural numbers
def maxGCD(n):
# Return max GCD
return (n // 2);
# Driver code
if __name__ == '__main__':
n = 4;
print(maxGCD(n));
# This code is contributed by Amit Katiyar
C#
// C# implementation of
// the above approach
using System;
class GFG{
// Function to find the maximum GCD
// among all the pairs from
// first n natural numbers
static int maxGCD(int n)
{
// Return max GCD
return (n / 2);
}
// Driver code
public static void Main(String[] args)
{
int n = 4;
Console.Write(maxGCD(n));
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
2
时间复杂度: O(N 2 log N)
辅助空间: O(1)
高效的方法: N和N / 2的GCD为N / 2 ,这是从1到N的任何一对中所有GCD的最大值。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to find the maximum GCD
// among all the pairs from
// first n natural numbers
int maxGCD(int n)
{
// Return max GCD
return (n / 2);
}
// Driver Code
int main()
{
int n = 4;
cout << maxGCD(n);
return 0;
}
Java
// Java implementation of the above approach
class GFG{
// Function to find the maximum GCD
// among all the pairs from
// first n natural numbers
static int maxGCD(int n)
{
// Return max GCD
return (n / 2);
}
// Driver code
public static void main(String[] args)
{
int n = 4;
System.out.print(maxGCD(n));
}
}
// This code is contributed by Dewanti
Python3
# Python3 implementation of the
# above approach
# Function to find the maximum GCD
# among all the pairs from first n
# natural numbers
def maxGCD(n):
# Return max GCD
return (n // 2);
# Driver code
if __name__ == '__main__':
n = 4;
print(maxGCD(n));
# This code is contributed by Amit Katiyar
C#
// C# implementation of
// the above approach
using System;
class GFG{
// Function to find the maximum GCD
// among all the pairs from
// first n natural numbers
static int maxGCD(int n)
{
// Return max GCD
return (n / 2);
}
// Driver code
public static void Main(String[] args)
{
int n = 4;
Console.Write(maxGCD(n));
}
}
// This code is contributed by Rajput-Ji
Java脚本
输出:
2
时间复杂度: O(1)
辅助空间: O(1)