Given an array arr[] of 2 * N integers, the task is to find a set of N – 1 pairs such that the GCD of all pair sums is greater than 1.
Examples:
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
Approach: The given problem is an observation-based problem. The idea is the create N – 1 pairs such that their sum is even. Also, for a pair to have an even sum, either both the integers should be even or both should be odd. Below are the steps to follow:
- Initialize two vectors Odd and Even to store odd and even integers respectively.
- Traverse the given array arr[] and insert odd integers in Odd vector and even integers in Even vector.
- Pair the consecutive elements of both the vectors Odd and Even and print the N – 1 first pair of integers.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findPairs( int arr[], int N)
{
vector< int > even, odd;
for ( int i = 0; i < 2 * N; i++) {
if (arr[i] & 1)
odd.push_back(arr[i]);
else
even.push_back(arr[i]);
}
vector<pair< int , int > > ans;
for ( int i = 0; i + 1 < odd.size(); i += 2)
ans.push_back({ odd[i], odd[i + 1] });
for ( int i = 0; i + 1 < even.size(); i += 2)
ans.push_back({ even[i], even[i + 1] });
for ( int i = 0; i < N - 1; i++) {
cout << ans[i].first << " " << ans[i].second
<< endl;
}
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int N = 3;
findPairs(arr, N);
return 0;
}
|
Java
import java.util.*;
class Pair {
int x;
int y;
public Pair( int x, int y)
{
this .x = x;
this .y = y;
}
}
class GFG
{
public static void findPairs( int arr[], int N)
{
ArrayList<Integer> even = new ArrayList<Integer>();
ArrayList<Integer> odd = new ArrayList<Integer>();
for ( int i = 0 ; i < 2 * N; i++) {
if (arr[i] % 2 == 1 )
odd.add(arr[i]);
else
even.add(arr[i]);
}
Pair[] ans1 = new Pair[N];
Pair[] ans2 = new Pair[N];
for ( int i = 0 ; i + 1 < odd.size(); i += 2 ) {
ans1[i] = new Pair(odd.get(i), odd.get(i + 1 ));
}
for ( int i = 0 ; i + 1 < even.size(); i += 2 ) {
ans2[i]
= new Pair(even.get(i), even.get(i + 1 ));
}
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 ;
}
}
public static void main(String[] args)
{
int [] arr = new int [] { 1 , 2 , 3 , 4 , 5 , 6 };
int N = 3 ;
findPairs(arr, N);
}
}
|
Python3
def findPairs(arr, N):
even = []
odd = [];
for i in range ( 2 * N):
if (arr[i] & 1 ):
odd.append(arr[i]);
else :
even.append(arr[i]);
ans = [];
i = 0 ;
while (i + 1 < len (odd)):
ans.append({ "first" : odd[i], "second" : odd[i + 1 ] });
i + = 2
i = 0
while (i + 1 < len (odd)):
ans.append({ "first" : even[i], "second" : even[i + 1 ] });
i + = 2
for i in range (N - 1 ):
for y in ans[i].values():
print (y, end = " " )
print ("")
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ];
N = 3 ;
findPairs(arr, N);
|
C#
using System;
using System.Collections.Generic;
public class Pair {
public int x;
public int y;
public Pair( int x, int y) {
this .x = x;
this .y = y;
}
}
public class GFG
{
public static void findPairs( int []arr, int N) {
List< int > even = new List< int >();
List< int > odd = new List< int >();
for ( int i = 0; i < 2 * N; i++) {
if (arr[i] % 2 == 1)
odd.Add(arr[i]);
else
even.Add(arr[i]);
}
Pair[] ans1 = new Pair[N];
Pair[] ans2 = new Pair[N];
for ( int i = 0; i + 1 < odd.Count; i += 2) {
ans1[i] = new Pair(odd[i], odd[i + 1]);
}
for ( int i = 0; i + 1 < even.Count; i += 2) {
ans2[i] = new Pair(even[i], even[i + 1]);
}
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 ;
}
}
public static void Main(String[] args) {
int [] arr = new int [] { 1, 2, 3, 4, 5, 6 };
int N = 3;
findPairs(arr, N);
}
}
|
Javascript
<script>
function findPairs(arr, N)
{
let even = [], odd = [];
for (let i = 0; i < 2 * N; i++) {
if (arr[i] & 1)
odd.push(arr[i]);
else
even.push(arr[i]);
}
let ans = [];
for (let i = 0; i + 1 < odd.length; i += 2)
ans.push({ first: odd[i], second: odd[i + 1] });
for (let i = 0; i + 1 < even.length; i += 2)
ans.push({ first: even[i], second: even[i + 1] });
for (let i = 0; i < N - 1; i++) {
document.write(ans[i].first + " " + ans[i].second
+ "<br>" );
}
}
let arr = [1, 2, 3, 4, 5, 6];
let N = 3;
findPairs(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)