Find N-1 pairs (X, Y) from given array such that X and Y are different and X modulo Y is not present in array
Last Updated :
01 Jul, 2022
Given an array Arr[] of size N consisting of N pairwise distinct positive integers. The task is to find N – 1 different pair of positive integers X, Y that satisfy the following conditions :
- X ≠Y
- X, Y both belong to the array.
- X mod Y does not belong to the array.
Note: It is proved that N-1 such pairs always exists.
Examples:
Input: N = 4 , Arr [ ] = { 2 , 3 ,4 ,5 }
Output: (5 , 2) , (4 , 2) , (3 , 2)
Explanation: 4 – 1 = 3, hence 3 such pairs printed.
In the first pair 5 and 2 both belongs to the array, 5 not equals to 2 , 5 mod 2 does not belongs to the array.
Same is applicable for all the printed pairs.
Input: N = 2, Arr [ ] = { 1 , 3 }
Output: 3 , 1
Explanation: 2 – 1 = 1. Hence only one such pair exists. That is 3 , 1.
Approach: The above problem can be solved using the Greedy method. In this approach never look for all the possible cases. Instead, look for the part or amount that we need. Follow the below steps to solve this problem:
- Initialize the variable min_element as Arr[0].
- Iterate over the range [0, N) using the variable i and perform the following tasks:
- Iterate over the range [0, N) using the variable i and perform the following tasks:
- If Arr[i] is not equal to min_element, then print Arr[i], min_element as the pair.
This approach is based on the following observation:
Say X is the minimum element in the given array.
The value Arr[i] % X will always be less than X and no element in array is less than X
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void find( int N, int Arr[])
{
int min_element = Arr[0];
for ( int i = 0; i < N; i++) {
if (Arr[i] < min_element) {
min_element = Arr[i];
}
}
for ( int i = 0; i < N; i++) {
if (Arr[i] != min_element) {
cout << Arr[i] << " " <<
min_element << endl;
}
}
}
int main()
{
int N = 4;
int Arr[4] = { 2, 3, 4, 5 };
find(N, Arr);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static void find( int N, int Arr[])
{
int min_element = Arr[ 0 ];
for ( int i = 0 ; i < N; i++) {
if (Arr[i] < min_element) {
min_element = Arr[i];
}
}
for ( int i = 0 ; i < N; i++) {
if (Arr[i] != min_element) {
System.out.println(Arr[i] + " " +
min_element);
}
}
}
public static void main(String args[])
{
int N = 4 ;
int Arr[] = { 2 , 3 , 4 , 5 };
find(N, Arr);
}
}
|
Python3
def find(N, Arr):
min_element = Arr[ 0 ]
for i in range ( 0 , N):
if (Arr[i] < min_element):
min_element = Arr[i]
for i in range ( 0 , N):
if (Arr[i] ! = min_element):
print (f "{Arr[i]} {min_element}" )
if __name__ = = "__main__" :
N = 4
Arr = [ 2 , 3 , 4 , 5 ]
find(N, Arr)
|
C#
using System;
class GFG
{
static void find( int N, int []Arr)
{
int min_element = Arr[0];
for ( int i = 0; i < N; i++) {
if (Arr[i] < min_element) {
min_element = Arr[i];
}
}
for ( int i = 0; i < N; i++) {
if (Arr[i] != min_element) {
Console.WriteLine(Arr[i] + " " +
min_element);
}
}
}
public static void Main()
{
int N = 4;
int []Arr = { 2, 3, 4, 5 };
find(N, Arr);
}
}
|
Javascript
<script>
function find(N, Arr)
{
let min_element = Arr[0];
for (let i = 0; i < N; i++) {
if (Arr[i] < min_element) {
min_element = Arr[i];
}
}
for (let i = 0; i < N; i++) {
if (Arr[i] != min_element) {
document.write(Arr[i] + " " +
min_element);
}
}
}
let N = 4;
let Arr = [ 2, 3, 4, 5 ];
find(N, Arr);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...