Find all pairs (a,b) and (c,d) in array which satisfy ab = cd
Given an array of distinct integers, the task is to find two pairs (a, b) and (c, d) such that ab = cd, where a, b, c and d are distinct elements.
Examples:
Input : arr[] = {3, 4, 7, 1, 2, 9, 8}
Output : 4 2 and 1 8
Product of 4 and 2 is 8 and
also product of 1 and 8 is 8 .
Input : arr[] = {1, 6, 3, 9, 2, 10};
Output : 6 3 and 9 2
A Simple Solution is to run four loops to generate all possible quadruples of array element. For every quadruple (a, b, c, d), check if a*b = c*d. Time complexity of this solution is O(n4).
An Efficient Solution of this problem is to use hashing. We use product as key and pair as value in hash table.
1. For i=0 to n-1
2. For j=i+1 to n-1
a) Find prod = arr[i]*arr[j]
b) If prod is not available in hash then make
H[prod] = make_pair(i, j) // H is hash table
c) If product is also available in hash
then print previous and current elements
of array
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
void findPairs( int arr[], int n)
{
bool found = false ;
unordered_map< int , pair < int , int > > H;
for ( int i=0; i<n; i++)
{
for ( int j=i+1; j<n; j++)
{
int prod = arr[i]*arr[j];
if (H.find(prod) == H.end())
H[prod] = make_pair(i,j);
else
{
pair< int , int > pp = H[prod];
cout << arr[pp.first] << " " << arr[pp.second]
<< " and " << arr[i]<< " " <<arr[j]<<endl;
found = true ;
}
}
}
if (found == false )
cout << "No pairs Found" << endl;
}
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
int n = sizeof (arr)/ sizeof ( int );
findPairs(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static class pair {
int first,second;
pair( int f, int s)
{
first = f;
second = s;
}
};
public static void findPairs( int arr[], int n)
{
boolean found = false ;
HashMap<Integer, pair> hp =
new HashMap<Integer, pair>();
for ( int i = 0 ; i < n; i++)
{
for ( int j = i + 1 ; j < n; j++)
{
int prod = arr[i] * arr[j];
if (!hp.containsKey(prod))
hp.put(prod, new pair(i,j));
else
{
pair p = hp.get(prod);
System.out.println(arr[p.first]
+ " " + arr[p.second]
+ " " + "and" + " " +
arr[i] + " " + arr[j]);
found = true ;
}
}
}
if (found == false )
System.out.println( "No pairs Found" );
}
public static void main (String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };
int n = arr.length;
findPairs(arr, n);
}
}
|
Python3
def findPairs(arr, n):
found = False
H = dict ()
for i in range (n):
for j in range (i + 1 , n):
prod = arr[i] * arr[j]
if (prod not in H.keys()):
H[prod] = [i, j]
else :
pp = H[prod]
print (arr[pp[ 0 ]], arr[pp[ 1 ]],
"and" , arr[i], arr[j])
found = True
if (found = = False ):
print ( "No pairs Found" )
arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]
n = len (arr)
findPairs(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public class pair
{
public int first,second;
public pair( int f, int s)
{
first = f;
second = s;
}
};
public static void findPairs( int [] arr, int n)
{
bool found = false ;
Dictionary< int , pair> hp =
new Dictionary< int , pair>();
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
int prod = arr[i] * arr[j];
if (!hp.ContainsKey(prod))
hp.Add(prod, new pair(i,j));
else
{
pair p = hp[prod];
Console.WriteLine(arr[p.first]
+ " " + arr[p.second]
+ " " + "and" + " " +
arr[i] + " " + arr[j]);
found = true ;
}
}
}
if (found == false )
Console.WriteLine( "No pairs Found" );
}
public static void Main (String[] args)
{
int []arr = {1, 2, 3, 4, 5, 6, 7, 8};
int n = arr.Length;
findPairs(arr, n);
}
}
|
Javascript
<script>
function findPairs(arr,n)
{
let found = false ;
let hp = new Map();
for (let i = 0; i < n; i++)
{
for (let j = i + 1; j < n; j++)
{
let prod = arr[i] * arr[j];
if (!hp.has(prod))
hp.set(prod, [i,j]);
else
{
let p = hp.get(prod);
document.write(arr[p[0]]
+ " " + arr[p[1]]
+ " " + "and" + " " +
arr[i] + " " + arr[j]+ "<br>" );
found = true ;
}
}
}
if (found == false )
document.write( "No pairs Found" );
}
let arr=[1, 2, 3, 4, 5, 6, 7, 8];
let n = arr.length;
findPairs(arr, n);
</script>
|
Output
1 6 and 2 3
1 8 and 2 4
2 6 and 3 4
3 8 and 4 6
Time Complexity: O(n2) assuming hash search and insert operations take O(1) time.
Auxiliary Space: O(n2)
Related Article:
Find four elements a, b, c and d in an array such that a+b = c+d
Last Updated :
13 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...