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}{2}
Frac[1] => \frac{2}{4} = \frac{1}{2}
Frac[2] => \frac{3}{6} = \frac{1}{2}
Frac[3] => \frac{4}{1}
Frac[4] => \frac{5}{11}
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{10}{10}
Frac[1] => \frac{20}{10}
Frac[2] => \frac{30}{10}
Frac[3] => \frac{50}{10}
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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to find 
// fractions in its lowest form
  
#include <bits/stdc++.h>
  
using namespace std;
  
// Recursive function to 
// find gcd of a and b
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
  
// Function to count the unique
// fractios in the given array
int countUniqueFractions(int num[], 
                  int den[], int N){
      
    // Hash-map to store the fractions
    // in its lowest form
    map<pair<int, int>, int> mp;
      
    // Loop to iterate over the 
    // fractions and store is lowest
    // form in the hash-map
    for (int i = 0; i < N; i++) {
        int numer, denom;
          
        // To find the Lowest form
        numer = num[i] / gcd(num[i], den[i]);
        denom = den[i] / gcd(num[i], den[i]);
        mp[make_pair(numer, denom)] += 1;
    }
      
    return mp.size();
}
  
// Driver code
int main()
{
    int N = 6;
      
    // Numerator Array
    int num[] = { 1, 40, 20, 5, 6, 7 };
      
    // Denominator Array
    int den[] = { 10, 40, 2, 5, 12, 14 };
      
    cout << countUniqueFractions(num, den, N);
      
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to find 
# fractions in its lowest form
from collections import defaultdict 
  
# Recursive function to 
# find gcd of a and b
def gcd(a, b):
  
    if (b == 0):
        return a
    return gcd(b, a % b)
  
# Function to count the unique
# fractios in the given array
def countUniqueFractions(num, den, N):
      
    # Hash-map to store the fractions
    # in its lowest form
    mp = defaultdict(int)
      
    # Loop to iterate over the 
    # fractions and store is lowest
    # form in the hash-map
    for i in range(N):
          
        # To find the Lowest form
        numer = num[i] // gcd(num[i], den[i])
        denom = den[i] // gcd(num[i], den[i])
        mp[(numer, denom)] += 1
      
    return len(mp)
  
# Driver code
if __name__ == "__main__":
      
    N = 6
      
    # Numerator Array
    num = [ 1, 40, 20, 5, 6, 7 ]
      
    # Denominator Array
    den = [ 10, 40, 2, 5, 12, 14 ]
      
    print(countUniqueFractions(num, den, N))
  
# This code is contributed by chitranayal

chevron_right


Output:

4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : chitranayal, nidhi_biet