Max count of unique ratio/fraction pairs in given arrays
Given two arrays num[] and den[] which denotes the numerator and denominator respectively, the task is to find the count of the unique fractions.
Examples:
Input: num[] = {1, 2, 3, 4, 5}, den[] = {2, 4, 6, 1, 11}
Output: 3
Explanation:
Simplest forms of the fractions
Frac[0] =>
Frac[1] =>
Frac[2] =>
Frac[3] =>
Frac[4] =>
Count of Unique Fractions => 3
Input: num[] = {10, 20, 30, 50}, den[] = {10, 10, 10, 10}
Output: 4
Explanation:
Simplest forms of the fractions
Frac[0] =>
Frac[1] =>
Frac[2] =>
Frac[3] =>
Count of Unique Fractions => 4
Approach: The idea is to use hash-map to find the unique fractions. To store the fractions such that the duplicates are not there, we convert each fraction to its lowest form.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int countUniqueFractions( int num[],
int den[], int N){
map<pair< int , int >, int > mp;
for ( int i = 0; i < N; i++) {
if (num[i] == 0)
mp[make_pair(0, 0)] += 1;
else
{
int number, denom;
number = num[i] / gcd(num[i], den[i]);
denom = den[i] / gcd(num[i], den[i]);
if (denom < 0) number *= -1;
mp[make_pair(number, denom)] += 1;
}
}
return mp.size();
}
int main()
{
int N = 6;
int num[] = { 1, 40, 20, 5, 6, 7 };
int den[] = { 10, 40, 2, 5, 12, 14 };
cout << countUniqueFractions(num, den, N);
return 0;
}
|
Java
import java.lang.*;
import java.util.*;
class GFG{
static class pair
{
int x, y;
pair( int x, int y)
{
this .x = x;
this .y = y;
}
@Override
public int hashCode()
{
return this .x;
}
@Override
public boolean equals(Object obj)
{
if ( this == obj)
return true ;
if (obj == null ||
obj.getClass() !=
this .getClass())
return false ;
pair geek = (pair) obj;
return (geek.x == this .x &&
geek.y == this .y);
}
}
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int countUniqueFractions( int num[],
int den[], int N)
{
Map<pair, Integer> mp = new HashMap<>();
for ( int i = 0 ; i < N; i++)
{
if (num[i] == 0 )
{
pair tmp = new pair( 0 , 0 );
mp.put(tmp, 1 );
}
else
{
int number = num[i] / gcd(num[i], den[i]);
int denom = den[i] / gcd(num[i], den[i]);
if (denom < 0 ) number *= - 1 ;
pair tmp = new pair(number, denom);
mp.put(tmp, 1 );
}
}
return mp.size();
}
public static void main (String[] args)
{
int N = 6 ;
int num[] = { 1 , 40 , 20 , 5 , 6 , 7 };
int den[] = { 10 , 40 , 2 , 5 , 12 , 14 };
System.out.print(countUniqueFractions(num, den, N));
}
}
|
Python3
from fractions import Fraction
from collections import defaultdict
def gcd(a, b):
if b = = 0 :
return a
return gcd(b, a % b)
def countUniqueFractions(num, den, N):
mp = defaultdict( int )
for i in range (N):
if num[i] = = 0 :
mp[Fraction( 0 , 1 )] + = 1
else :
number = num[i] / / gcd(num[i], den[i])
denom = den[i] / / gcd(num[i], den[i])
if denom < 0 :
number * = - 1
mp[Fraction(number, denom)] + = 1
return len (mp)
if __name__ = = '__main__' :
N = 6
num = [ 1 , 40 , 20 , 5 , 6 , 7 ]
den = [ 10 , 40 , 2 , 5 , 12 , 14 ]
print (countUniqueFractions(num, den, N))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int Gcd( int a, int b)
{
if (b == 0)
return a;
return Gcd(b, a % b);
}
static int CountUniqueFractions( int [] num, int [] den, int N)
{
Dictionary<Tuple< int , int >, int > mp = new Dictionary<Tuple< int , int >, int >();
for ( int i = 0; i < N; i++)
{
if (num[i] == 0)
{
var key = Tuple.Create(0, 0);
if (mp.ContainsKey(key))
mp[key] += 1;
else
mp[key] = 1;
}
else
{
int number, denom;
number = num[i] / Gcd(num[i], den[i]);
denom = den[i] / Gcd(num[i], den[i]);
if (denom < 0)
number *= -1;
var key = Tuple.Create(number, denom);
if (mp.ContainsKey(key))
mp[key] += 1;
else
mp[key] = 1;
}
}
return mp.Count;
}
static void Main( string [] args)
{
int N = 6;
int [] num = { 1, 40, 20, 5, 6, 7 };
int [] den = { 10, 40, 2, 5, 12, 14 };
Console.WriteLine(CountUniqueFractions(num, den, N));
}
}
|
Javascript
function gcd(a, b) {
if (b === 0) {
return a;
}
return gcd(b, a % b);
}
function countUniqueFractions(num, den) {
const mp = {};
for (let i = 0; i < num.length; i++) {
if (num[i] === 0) {
mp[`${0}/${0}`] = (mp[`${0}/${0}`] || 0) + 1;
} else {
let number, denom;
number = num[i] / gcd(num[i], den[i]);
denom = den[i] / gcd(num[i], den[i]);
if (denom < 0) {
number *= -1;
}
mp[`${number}/${denom}`] = (mp[`${number}/${denom}`] || 0) + 1;
}
}
return Object.keys(mp).length;
}
const num = [1, 40, 20, 5, 6, 7];
const den = [10, 40, 2, 5, 12, 14];
const N = num.length;
console.log(countUniqueFractions(num, den, N));
|
Time Complexity: O(N*log(MAX)), where N is the size of the array and MAX is the maximum number in the array num and den.
Auxiliary Space: O(N + log(MAX))
Last Updated :
14 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...