Sum of given N fractions in reduced form

Given two arrays arr1[] and arr2[] of length N which contains Numerator and Denominator of N fractions respectively, the task is to find the sum of the given N fractions in reduced form.
Examples: 
 

Input: arr1[] = { 1, 2, 5 }, arr2[] = { 2, 1, 6 } 
Output: 10/3
Input: arr1[] = { 1, 1 } arr2[] = { 2, 2 } 
Output: 1/1 
 

 

Approach: 
 

  1. Find the Least Common Multiple(LCM) of all the denominators stored in arr2[].
  2. Change numerator of every fraction stored in arr1[] as: 
     

 
 



Let L be the resultant LCM of all denominator(say L) and Numerator and Denominator of the fraction be N and D respectively. 
Then then value of each numerator must be changed to:
 

 

New Numerator = \frac{N*L}{D}

 

 

 

  1. Find the sum of new Numerator(say sumN) formed after above step.
  2. Divide the sumL and L by the GCD of sumL and L to get the resultant fraction in reduced form.

Below is the implementation of the above approach:
 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find  GCD of a & b
// using Euclid Lemma
int gcd(int a, int b)
{
    // Base Case
    if (b == 0) {
        return a;
    }
 
    return gcd(b, a % b);
}
 
// Function to find the LCM of all
// elements in arr[]
int findlcm(int arr[], int n)
{
    // Initialize result
    int ans = arr[0];
 
    // Iterate arr[] to find LCM
    for (int i = 1; i < n; i++) {
        ans = (((arr[i] * ans)) / (gcd(arr[i], ans)));
    }
 
    // Return the final LCM
    return ans;
}
 
// Function to find the sum of N
// fraction in reduced form
void addReduce(int n, int num[],
               int den[])
{
 
    // To store the sum of all
    // final numerators
    int final_numerator = 0;
 
    // Find the LCM of all denominator
    int final_denominator = findlcm(den, n);
 
    // Find the sum of all N
    // numerators & denominators
    for (int i = 0; i < n; i++) {
 
        // Add each fraction one by one
        final_numerator = final_numerator
                          + (num[i]) * (final_denominator
                                        / den[i]);
    }
 
    // Find GCD of final numerator and
    // denominator
    int GCD = gcd(final_numerator,
                  final_denominator);
 
    // Convert into reduced form
    // by dividing from GCD
    final_numerator /= GCD;
    final_denominator /= GCD;
 
    // Print the final fraction
    cout << final_numerator
         << "/"
         << final_denominator
         << endl;
}
 
// Driven Code
int main()
{
    // Given N
    int N = 3;
 
    // Given Numerator
    int arr1[] = { 1, 2, 5 };
 
    // Given Denominator
    int arr2[] = { 2, 1, 6 };
 
    // Function Call
    addReduce(N, arr1, arr2);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find GCD of a & b
// using Euclid Lemma
static int gcd(int a, int b)
{
     
    // Base case
    if (b == 0)
    {
        return a;
    }
 
    return gcd(b, a % b);
}
 
// Function to find the LCM of all
// elements in arr[]
static int findlcm(int arr[], int n)
{
     
    // Initialize result
    int ans = arr[0];
 
    // Iterate arr[] to find LCM
    for(int i = 1; i < n; i++)
    {
        ans = (((arr[i] * ans)) /
             (gcd(arr[i], ans)));
    }
 
    // Return the final LCM
    return ans;
}
 
// Function to find the sum of N
// fraction in reduced form
static void addReduce(int n, int num[],
                             int den[])
{
     
    // To store the sum of all
    // final numerators
    int final_numerator = 0;
 
    // Find the LCM of all denominator
    int final_denominator = findlcm(den, n);
 
    // Find the sum of all N
    // numerators & denominators
    for(int i = 0; i < n; i++)
    {
 
        // Add each fraction one by one
        final_numerator = final_numerator + (num[i]) *
                         (final_denominator / den[i]);
    }
 
    // Find GCD of final numerator and
    // denominator
    int GCD = gcd(final_numerator,
                  final_denominator);
 
    // Convert into reduced form
    // by dividing from GCD
    final_numerator /= GCD;
    final_denominator /= GCD;
 
    // Print the final fraction
    System.out.println(final_numerator + "/" +
                       final_denominator);
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given N
    int N = 3;
     
    // Given numerator
    int arr1[] = { 1, 2, 5 };
     
    // Given denominator
    int arr2[] = { 2, 1, 6 };
     
    // Function call
    addReduce(N, arr1, arr2);
}
}
 
// This code is contributed by offbeat

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
class GFG{
  
// Function to find GCD of a & b
// using Euclid Lemma
static int gcd(int a, int b)
{
      
    // Base case
    if (b == 0)
    {
        return a;
    }
  
    return gcd(b, a % b);
}
  
// Function to find the LCM of all
// elements in arr[]
static int findlcm(int []arr, int n)
{
      
    // Initialize result
    int ans = arr[0];
  
    // Iterate arr[] to find LCM
    for(int i = 1; i < n; i++)
    {
        ans = (((arr[i] * ans)) /
             (gcd(arr[i], ans)));
    }
  
    // Return the final LCM
    return ans;
}
  
// Function to find the sum of N
// fraction in reduced form
static void addReduce(int n, int []num,
                             int []den)
{
      
    // To store the sum of all
    // final numerators
    int final_numerator = 0;
  
    // Find the LCM of all denominator
    int final_denominator = findlcm(den, n);
  
    // Find the sum of all N
    // numerators & denominators
    for(int i = 0; i < n; i++)
    {
  
        // Add each fraction one by one
        final_numerator = final_numerator + (num[i]) *
                         (final_denominator / den[i]);
    }
  
    // Find GCD of final numerator and
    // denominator
    int GCD = gcd(final_numerator,
                  final_denominator);
  
    // Convert into reduced form
    // by dividing from GCD
    final_numerator /= GCD;
    final_denominator /= GCD;
  
    // Print the final fraction
    Console.Write(final_numerator + "/" +
                  final_denominator);
}
  
// Driver code
public static void Main(string[] args)
{
      
    // Given N
    int N = 3;
      
    // Given numerator
    int []arr1 = { 1, 2, 5 };
      
    // Given denominator
    int []arr2 = { 2, 1, 6 };
      
    // Function call
    addReduce(N, arr1, arr2);
}
}
  
// This code is contributed by Ritik Bansal

chevron_right


Output: 

10/3


 

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 : offbeat, bansal_rtk_

Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.