Count of pairs in given Array having same ratio
Last Updated :
15 Feb, 2022
Given an array arr[] consisting of N pairs of the form {A, B}, the task is to count the pairs of indices (i, j) such that the ratio of pairs of arr[i] and arr[j] are the same.
Examples:
Input: arr[] = {{2, 6}, {1, 3}, {8, 24}}
Output: 3
Explanation:
Following are the pairs of indices whose ratios are the same:
- (0, 1): Ratio of pair arr[0] = 2/6 = 1/3 and the ratio of pair arr[1] = 1/3, which are the same.
- (0, 2): Ratio of pair arr[0] = 2/6 = 1/3 and the ratio of pair arr[2] = 8/24 = 1/3, which are the same.
- (1, 2): Ratio of pair arr[1] = 1/3 and the ratio of pair arr[2] = 8/24 = 1/3, which are the same.
Therefore, the count of such pairs are 3.
Input: arr[] = {{4, 5}, {7, 8}}
Output: 0
Naive Approach: The simplest approach to solve the given problem is to generate all possible pairs of the given array and count those pairs whose ratios are the same. After checking for all the pairs, print the total count of pairs obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long pairsWithSameRatio(
vector<pair< int , int > >& arr)
{
int count = 0;
for ( int i = 0; i < arr.size(); i++) {
double ratio1
= ( double )arr[i].first
/ ( double )arr[i].second;
for ( int j = i + 1; j < arr.size(); j++) {
double ratio2 = ( double )arr[j].first
/ ( double )arr[j].second;
if (ratio1 == ratio2) {
count++;
}
}
}
return count;
}
int main()
{
vector<pair< int , int > > arr = {
{ 2, 6 }, { 1, 3 }, { 8, 24 }, { 4, 12 }, { 16, 48 }
};
cout << pairsWithSameRatio(arr);
return 0;
}
|
Java
class GFG {
static class pair {
int first, second;
public pair( int first, int second) {
this .first = first;
this .second = second;
}
}
static long pairsWithSameRatio(pair[] arr)
{
int count = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
double ratio1 = ( double ) arr[i].first / ( double ) arr[i].second;
for ( int j = i + 1 ; j < arr.length; j++) {
double ratio2 = ( double ) arr[j].first / ( double ) arr[j].second;
if (ratio1 == ratio2) {
count++;
}
}
}
return count;
}
public static void main(String[] args) {
pair[] arr = { new pair( 2 , 6 ), new pair( 1 , 3 ), new pair( 8 , 24 ), new pair( 4 , 12 ), new pair( 16 , 48 ) };
System.out.print(pairsWithSameRatio(arr));
}
}
|
Python3
def pairsWithSameRatio(arr):
count = 0
for i in range ( len (arr)):
ratio1 = arr[i][ 0 ] / / arr[i][ 1 ]
for j in range (i + 1 , len (arr), 1 ):
ratio2 = arr[j][ 0 ] / / arr[j][ 1 ]
if (ratio1 = = ratio2):
count + = 1
return count
if __name__ = = '__main__' :
arr = [[ 2 , 6 ],[ 1 , 3 ],[ 8 , 24 ],[ 4 , 12 ],[ 16 , 48 ]]
print (pairsWithSameRatio(arr))
|
C#
using System;
public class GFG {
class pair {
public int first, second;
public pair( int first, int second) {
this .first = first;
this .second = second;
}
}
static long pairsWithSameRatio(pair[] arr)
{
int count = 0;
for ( int i = 0; i < arr.Length; i++) {
double ratio1 = ( double ) arr[i].first / ( double ) arr[i].second;
for ( int j = i + 1; j < arr.Length; j++) {
double ratio2 = ( double ) arr[j].first / ( double ) arr[j].second;
if (ratio1 == ratio2) {
count++;
}
}
}
return count;
}
public static void Main(String[] args) {
pair[] arr = { new pair(2, 6), new pair(1, 3), new pair(8, 24), new pair(4, 12), new pair(16, 48) };
Console.Write(pairsWithSameRatio(arr));
}
}
|
Javascript
<script>
function pairsWithSameRatio(
arr) {
let count = 0;
for (let i = 0; i < arr.length; i++) {
let ratio1
= arr[i].first
/ arr[i].second;
for (let j = i + 1; j < arr.length; j++) {
ratio2 = arr[j].first
/ arr[j].second;
if (ratio1 == ratio2) {
count++;
}
}
}
return count;
}
let arr = [
{ first: 2, second: 6 }, { first: 1, second: 3 }, { first: 8, second: 24 }, { first: 4, second: 12 }, { first: 16, second: 48 }
]
document.write(pairsWithSameRatio(arr));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by using the map by storing the frequency of the ratios of every pair in the given array arr[] and then count the total number of pairs formed. Follow the step below to solve the given problem:
- Initialize a variable, say ans as 0 that stores the total count of pairs having the same ratio.
- Create an unordered map, say Map that stores the key as the ratio of pair of array elements and value as their frequency.
- Traverse the given array arr[] and for each pair {A, B} increment the frequency of A/B in the map by 1.
- Iterate over the map Map and for each key-value pair if the frequency of any key is greater than 1 then add the value of frequency*(frequency – 1)/2 to the variable ans storing the resultant count of pairs with the current key as the ratio.
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int fact( int n)
{
int res = 1;
for ( int i = 2; i <= n; i++)
res = res * i;
return res;
}
int nCr( int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
int pairsWithSameRatio(
vector<pair< int , int > >& arr)
{
unordered_map< double , int > mp;
int ans = 0;
for ( auto x : arr) {
mp[x.first / x.second] += 1;
}
for ( auto x : mp) {
int val = x.second;
if (val > 1) {
ans += nCr(val, 2);
}
}
return ans;
}
int main()
{
vector<pair< int , int > > arr = {
{ 2, 6 }, { 1, 3 }, { 8, 24 }, { 4, 12 }, { 16, 48 }
};
cout << pairsWithSameRatio(arr);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class pair
{
int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static int fact( int n)
{
int res = 1 ;
for ( int i = 2 ; i <= n; i++)
res = res * i;
return res;
}
static int nCr( int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
static int pairsWithSameRatio(
pair []arr)
{
Map<Double, Integer> mp = new HashMap<Double, Integer>();
int ans = 0 ;
for (pair x : arr) {
if (mp.containsKey(( double ) (x.first / x.second))){
mp.put(( double ) (x.first / x.second), mp.get(( double ) (x.first / x.second))+ 1 );
}
else {
mp.put(( double )(x.first / x.second), 1 );
}
}
for (Map.Entry<Double,Integer> x : mp.entrySet()){
int val = x.getValue();
if (val > 1 ) {
ans += nCr(val, 2 );
}
}
return ans;
}
public static void main(String[] args)
{
pair []arr = {
new pair( 2 , 6 ), new pair( 1 , 3 ), new pair( 8 , 24 ), new pair( 4 , 12 ), new pair( 16 , 48 )
};
System.out.print(pairsWithSameRatio(arr));
}
}
|
Python3
from collections import defaultdict
def fact(n):
res = 1
for i in range ( 2 , n + 1 ):
res = res * i
return res
def nCr(n, r):
return fact(n) / / (fact(r) * fact(n - r))
def pairsWithSameRatio(arr):
mp = defaultdict( int )
ans = 0
for x in arr:
mp[x[ 0 ] / / x[ 1 ]] + = 1
for x in mp:
val = mp[x]
if (val > 1 ):
ans + = nCr(val, 2 )
return ans
if __name__ = = "__main__" :
arr = [[ 2 , 6 ], [ 1 , 3 ], [ 8 , 24 ], [ 4 , 12 ], [ 16 , 48 ]]
print (pairsWithSameRatio(arr))
|
C#
using System;
using System.Collections.Generic;
public class GFG{
class pair
{
public int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static int fact( int n)
{
int res = 1;
for ( int i = 2; i <= n; i++)
res = res * i;
return res;
}
static int nCr( int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
static int pairsWithSameRatio(
pair []arr)
{
Dictionary<Double, int > mp = new Dictionary<Double, int >();
int ans = 0;
foreach (pair x in arr) {
if (mp.ContainsKey(( double ) (x.first / x.second))){
mp[( double ) (x.first / x.second)]=mp[( double ) (x.first / x.second)]+1;
}
else {
mp.Add(( double )(x.first / x.second), 1);
}
}
foreach (KeyValuePair<Double, int > x in mp){
int val = x.Value;
if (val > 1) {
ans += nCr(val, 2);
}
}
return ans;
}
public static void Main(String[] args)
{
pair []arr = {
new pair( 2, 6 ), new pair( 1, 3 ), new pair( 8, 24 ), new pair( 4, 12 ), new pair( 16, 48 )
};
Console.Write(pairsWithSameRatio(arr));
}
}
|
Javascript
<script>
class pair {
constructor(first , second) {
this .first = first;
this .second = second;
}
}
function fact(n) {
var res = 1;
for (i = 2; i <= n; i++)
res = res * i;
return res;
}
function nCr(n , r) {
return fact(n) / (fact(r) * fact(n - r));
}
function pairsWithSameRatio( arr) {
var mp = new Map();
var ans = 0;
for ( var x of arr) {
if (mp.has( (x.first / x.second))) {
mp.set((x.first / x.second), mp.get( (x.first / x.second)) + 1);
} else {
mp.set( (x.first / x.second), 1);
}
}
for ( x of mp) {
var val = x[1];
if (val > 1) {
ans += nCr(val, 2);
}
}
return ans;
}
var arr = [ new pair(2, 6), new pair(1, 3),
new pair(8, 24), new pair(4, 12), new pair(16, 48) ];
document.write(pairsWithSameRatio(arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...