Open In App

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
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the possible pairs
void find(int N, int Arr[])
{
    int min_element = Arr[0];
 
    // Get the minimum element
    for (int i = 0; i < N; i++) {
        if (Arr[i] < min_element) {
            min_element = Arr[i];
        }
    }
 
    // Pair rest N - 1 elements with
    // the minimum element and print
    for (int i = 0; i < N; i++) {
        if (Arr[i] != min_element) {
            cout << Arr[i] << " " <<
                min_element << endl;
        }
    }
}
 
// Driver Code
int main()
{
 
    // Initialize N and the Array
    int N = 4;
    int Arr[4] = { 2, 3, 4, 5 };
 
    find(N, Arr);
 
    return 0;
}


Java




// Java  program for the above approach
import java.util.*;
public class GFG
{
   
// Function to find the possible pairs
static void find(int N, int Arr[])
{
    int min_element = Arr[0];
 
    // Get the minimum element
    for (int i = 0; i < N; i++) {
        if (Arr[i] < min_element) {
            min_element = Arr[i];
        }
    }
 
    // Pair rest N - 1 elements with
    // the minimum element and print
    for (int i = 0; i < N; i++) {
        if (Arr[i] != min_element) {
            System.out.println(Arr[i] + " " +
                min_element);
        }
    }
}
 
// Driver Code
public static void main(String args[])
{
    int N = 4;
    int Arr[] = { 2, 3, 4, 5 };
 
    find(N, Arr);
 
}
}
 
// This code is contributed by Samim Hossain Mondal.


Python3




# python3 program for the above approach
 
# Function to find the possible pairs
def find(N, Arr):
 
    min_element = Arr[0]
 
    # Get the minimum element
    for i in range(0, N):
        if (Arr[i] < min_element):
            min_element = Arr[i]
 
    # Pair rest N - 1 elements with
    # the minimum element and print
    for i in range(0, N):
        if (Arr[i] != min_element):
            print(f"{Arr[i]} {min_element}")
 
# Driver Code
if __name__ == "__main__":
 
    # Initialize N and the Array
    N = 4
    Arr = [2, 3, 4, 5]
 
    find(N, Arr)
 
    # This code is contributed by rakeshsahni


C#




// C# program for the above approach
using System;
class GFG
{
   
// Function to find the possible pairs
static void find(int N, int []Arr)
{
    int min_element = Arr[0];
 
    // Get the minimum element
    for (int i = 0; i < N; i++) {
        if (Arr[i] < min_element) {
            min_element = Arr[i];
        }
    }
 
    // Pair rest N - 1 elements with
    // the minimum element and print
    for (int i = 0; i < N; i++) {
        if (Arr[i] != min_element) {
            Console.WriteLine(Arr[i] + " " +
                min_element);
        }
    }
}
 
// Driver Code
public static void Main()
{
    int N = 4;
    int []Arr = { 2, 3, 4, 5 };
 
    find(N, Arr);
 
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
// Javascript program for the above approach
 
// Function to find the possible pairs
function find(N, Arr)
{
     
    let min_element = Arr[0];
 
    // Get the minimum element
    for (let i = 0; i < N; i++) {
        if (Arr[i] < min_element) {
            min_element = Arr[i];
        }
    }
 
    // Pair rest N - 1 elements with
    // the minimum element and print
    for (let i = 0; i < N; i++) {
        if (Arr[i] != min_element) {
            document.write(Arr[i] + " " +
                min_element);
        }
    }
}
 
// Driver Code
// Initialize N and the Array
let N = 4;
let Arr = [ 2, 3, 4, 5 ];
 
find(N, Arr);
 
// This code is contributed by Samim Hossain Mondal.
</script>


 
 

Output

3 2
4 2
5 2

 

Time Complexity: O(N)
Auxiliary Space: O(1)

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads