从给定数组中找到 N – 1 对,使得所有对和的 GCD 大于 1
给定一个由2 * N 个整数组成的数组arr[] ,任务是找到一组N-1对,使得所有对和的 GCD 都大于 1。
例子:
Input: arr[] = {1, 2, 3, 4, 5, 6}
Output:
1 3
2 4
Explanation: The given array has 3 * 2 elements. The pair (1, 3) and (2, 4) has sum of elements as 4 and 6 respectively. Also, GCD(4, 6) > 1. Hence, (1, 3) and (2, 4) are the required pairs. Other possible pairs are (1, 5) and (2, 6), (3, 5) and (2, 5), etc..
Input: arr[] = {1, 1, 1, 2, 3, 4}
Output:
1 3
2 4
方法:给定的问题是一个基于观察的问题。这个想法是创建N - 1对,使得它们的总和是偶数。此外,对于具有偶数和的一对,要么两个整数都应该是偶数,要么都应该是奇数。以下是要遵循的步骤:
- 初始化两个向量Odd和Even分别存储奇数和偶数。
- 遍历给定数组arr[]并在奇数向量中插入奇数整数,在偶数向量中插入偶数整数。
- 将向量奇数和偶数的连续元素配对并打印N – 1第一对整数。
下面是上述方法的实现:
C++
// C++ Program of the above approach
#include
using namespace std;
// Function to find N - 1 pairs from
// given array such that GCD of all
// pair-sums is greater than 1
void findPairs(int arr[], int N)
{
// Stores even and odd
// integers respectively
vector even, odd;
// Loop to iterate arr[]
for (int i = 0; i < 2 * N; i++) {
if (arr[i] & 1)
odd.push_back(arr[i]);
else
even.push_back(arr[i]);
}
// Stores the required pairs
vector > ans;
// Insert all possible pairs
// from odd elements
for (int i = 0; i + 1 < odd.size(); i += 2)
ans.push_back({ odd[i], odd[i + 1] });
// Insert all possible pairs
// from even elements
for (int i = 0; i + 1 < even.size(); i += 2)
ans.push_back({ even[i], even[i + 1] });
// Print Answer
for (int i = 0; i < N - 1; i++) {
cout << ans[i].first << " " << ans[i].second
<< endl;
}
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int N = 3;
findPairs(arr, N);
return 0;
}
Java
// JAVA Program of the above approach
import java.util.*;
class Pair {
int x;
int y;
// Constructor
public Pair(int x, int y)
{
this.x = x;
this.y = y;
}
}
class GFG
{
// Function to find N - 1 pairs from
// given array such that GCD of all
// pair-sums is greater than 1
public static void findPairs(int arr[], int N)
{
// Stores even and odd
// integers respectively
ArrayList even = new ArrayList();
ArrayList odd = new ArrayList();
// Loop to iterate arr[]
for (int i = 0; i < 2 * N; i++) {
if (arr[i] % 2 == 1)
odd.add(arr[i]);
else
even.add(arr[i]);
}
// Stores the required pairs
Pair[] ans1 = new Pair[N];
Pair[] ans2 = new Pair[N];
// Insert all possible pairs
// from odd elements
for (int i = 0; i + 1 < odd.size(); i += 2) {
ans1[i] = new Pair(odd.get(i), odd.get(i + 1));
}
// Insert all possible pairs
// from even elements
for (int i = 0; i + 1 < even.size(); i += 2) {
ans2[i]
= new Pair(even.get(i), even.get(i + 1));
}
// Print Answer
for (int i = 0; i < N - 1; i++) {
System.out.println(ans1[i].x + " " + ans1[i].y);
System.out.println(ans2[i].x + " " + ans2[i].y);
break;
}
}
// Driver Code
public static void main(String[] args)
{
int[] arr = new int[] { 1, 2, 3, 4, 5, 6 };
int N = 3;
findPairs(arr, N);
}
}
// This code is contributed by Taranpreet
Python3
# Python code for the above approach
# Function to find N - 1 pairs from
# given array such that GCD of all
# pair-sums is greater than 1
def findPairs(arr, N):
# Stores even and odd
# integers respectively
even = []
odd = [];
# Loop to iterate arr[]
for i in range(2 * N):
if (arr[i] & 1):
odd.append(arr[i]);
else:
even.append(arr[i]);
# Stores the required pairs
ans = [];
# Insert all possible pairs
# from odd elements
i = 0;
while(i + 1 < len(odd)):
ans.append({ "first": odd[i], "second": odd[i + 1] });
i += 2
# Insert all possible pairs
# from even elements
i = 0
while(i + 1 < len(odd)):
ans.append({ "first": even[i], "second": even[i + 1] });
i += 2
# Print Answer
for i in range(N - 1):
for y in ans[i].values():
print(y, end=" ")
print("")
# Driver Code
arr = [1, 2, 3, 4, 5, 6];
N = 3;
findPairs(arr, N);
# This code is contributed by Saurabh Jaiswal
C#
// C# Program of the above approach
using System;
using System.Collections.Generic;
public class Pair {
public int x;
public int y;
// Constructor
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
public class GFG
{
// Function to find N - 1 pairs from
// given array such that GCD of all
// pair-sums is greater than 1
public static void findPairs(int []arr, int N) {
// Stores even and odd
// integers respectively
List even = new List();
List odd = new List();
// Loop to iterate []arr
for (int i = 0; i < 2 * N; i++) {
if (arr[i] % 2 == 1)
odd.Add(arr[i]);
else
even.Add(arr[i]);
}
// Stores the required pairs
Pair[] ans1 = new Pair[N];
Pair[] ans2 = new Pair[N];
// Insert all possible pairs
// from odd elements
for (int i = 0; i + 1 < odd.Count; i += 2) {
ans1[i] = new Pair(odd[i], odd[i + 1]);
}
// Insert all possible pairs
// from even elements
for (int i = 0; i + 1 < even.Count; i += 2) {
ans2[i] = new Pair(even[i], even[i + 1]);
}
// Print Answer
for (int i = 0; i < N - 1; i++) {
Console.WriteLine(ans1[i].x + " " + ans1[i].y);
Console.WriteLine(ans2[i].x + " " + ans2[i].y);
break;
}
}
// Driver Code
public static void Main(String[] args) {
int[] arr = new int[] { 1, 2, 3, 4, 5, 6 };
int N = 3;
findPairs(arr, N);
}
}
// This code is contributed by Rajput-Ji
Javascript
输出
1 3
2 4
时间复杂度: O(N)
辅助空间: O(N)