通过对给定 Array 重新排序,GCD 大于 1 的 Array 中的最大对数
给定一个大小为N的数组arr[] 。任务是重新排序arr[]并找到遵循以下条件的最大 GCD 对数。
- 选择数组Ai 和 Aj的任意两个元素,其中 0 <= i < j < N。
- 将Aj 与大于 1的(Ai, 2 * Aj)相乘后计算GCD 。
例子
Input: arr[] = { 3, 6 . 5, 3}
Output: 4
Explanation: Reorder array like this : { 6, 5, 3, 3 } and below are the pairs formed from arr[].
P1 = GCD( 6, 5 * 2) => (6, 10) => 2 > 1
P2 = GCD( 6, 3 * 2) => (6, 6) => 6 > 1
P3 = GCD( 6, 3 * 2) => (6, 6) => 6 > 1
P4 = GCD( 3, 3 * 2) => (3, 6) => 3 > 1
Input: arr[] = { 1, 7 }
Output: 0
Explanation: If array is order like { 7, 1 } no pair can be formed
GCD(7, 1 * 2) = > (7, 2 ), GCD(1, 7 * 2) => (1, 14) == 1
方法:如果我们观察到如果偶数元素在起始位置,那么(GCD > 1)对是最大的,因为存在乘以arr[j] * 2的条件,并且奇数元素的顺序与其数量无关对总是相同的。请按照以下步骤解决给定的问题。
- 用值0初始化变量idx 。
- 遍历数组。
- 如果arr[i]是偶数,则与arr[idx]交换并将 idx 增加1 。
- 遍历所有元素后。
- 用0初始化变量ans 。
- 首先使用两个循环i和第二个使用j = i + 1 。
- 现在如果gcd(arr[i], 2 * arr[j] * 2) > 1将ans增加1 。
下面是上述方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
// Function to find the maximum number of pairs
int maximumpairs(int arr[], int n)
{
// Reorder array with even element first
int idx = 0;
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
swap(arr[i], arr[idx]);
idx++;
}
}
// Now count the ans
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (__gcd(arr[i], 2 * arr[j]) > 1) {
ans++;
}
}
}
return ans;
}
// Driver Code
int main()
{
// Initializations
int arr[] = { 5, 3, 6, 3 };
int N = sizeof(arr) / sizeof(int);
// Function Call
int ans = maximumpairs(arr, N);
cout << ans;
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// find gcd
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to find the maximum number of pairs
static int maximumpairs(int arr[], int n)
{
// Reorder array with even element first
int idx = 0;
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
//swap
int temp = arr[i];
arr[i] = arr[idx];
arr[idx] = temp;
idx++;
}
}
// Now count the ans
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int a = arr[i];
int b = 2 * arr[j];
if (gcd(a, b) > 1) {
ans++;
}
}
}
return ans;
}
public static void main (String[] args)
{
// Initializations
int arr[] = { 5, 3, 6, 3 };
int N = arr.length;
// Function Call
int ans = maximumpairs(arr, N);
System.out.println(ans);
}
}
// This code is contributed by hrithikgarg03188
Python3
# Python program for above approach
# Function to find GCD
def gcd(a, b):
if(b == 0):
return a
else:
return gcd(b, a % b)
# Function to find the maximum number of pairs
def maximumpairs(arr,n):
# Reorder array with even element first
idx = 0
for i in range(0, n):
if (arr[i] % 2 == 0):
arr[i], arr[idx] = arr[idx], arr[i]
idx = idx + 1
# Now count the ans
ans = 0
for i in range(0,n):
for j in range(i + 1,n):
if (gcd(arr[i], 2*arr[j]) > 1):
ans = ans + 1
return ans
# Driver Code
# Initializations
arr = [ 5, 3, 6, 3 ]
N = len(arr)
# Function Call
ans = maximumpairs(arr, N)
print(ans)
# This code is contributed by Taranpreet
C#
// C# program for the above approach
using System;
class GFG {
// find gcd
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to find the maximum number of pairs
static int maximumpairs(int []arr, int n)
{
// Reorder array with even element first
int idx = 0;
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
//swap
int temp = arr[i];
arr[i] = arr[idx];
arr[idx] = temp;
idx++;
}
}
// Now count the ans
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int a = arr[i];
int b = 2 * arr[j];
if (gcd(a, b) > 1) {
ans++;
}
}
}
return ans;
}
public static void Main ()
{
// Initializations
int []arr = { 5, 3, 6, 3 };
int N = arr.Length;
// Function Call
int ans = maximumpairs(arr, N);
Console.Write(ans);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
4
时间复杂度: O(N * N * logN)
辅助空间: O(1)