Related Articles
Sum of given N fractions in reduced form
• Last Updated : 12 Apr, 2021

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:

• Find the Least Common Multiple(LCM) of all the denominators stored in arr2[].
• 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:

• Find the sum of new Numerator(say sumN) formed after above step.
• 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++

 // C++ program for the above approach#include using namespace std; // Function to find  GCD of a & b// using Euclid Lemmaint 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 formvoid 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 Codeint 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;}

## Java

 // Java program for the above approachimport java.util.*; class GFG{ // Function to find GCD of a & b// using Euclid Lemmastatic 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 formstatic 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 codepublic 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

## Python3

 # Python3 program for the above approach  # Function to find  GCD of a & b# using Euclid Lemmadef gcd(a, b):         # Base Case    if (b == 0):        return a         return gcd(b, a % b) # Function to find the LCM of all# elements in arr[]def findlcm(arr, n):         # Initialize result    ans = arr[0]      # Iterate arr[] to find LCM    for i in range(1, n):        ans = (((arr[i] * ans)) //            (gcd(arr[i], ans)))         # Return the final LCM    return ans # Function to find the sum of N# fraction in reduced formdef addReduce(n, num, den):      # To store the sum of all    # final numerators    final_numerator = 0      # Find the LCM of all denominator    final_denominator = findlcm(den, n)      # Find the sum of all N    # numerators & denominators    for i in range(n):          # Add each fraction one by one        final_numerator = (final_numerator +                          (num[i]) * (final_denominator //                           den[i]))         # Find GCD of final numerator and    # denominator    GCD = gcd(final_numerator,              final_denominator)      # Convert into reduced form    # by dividing from GCD    final_numerator //= GCD    final_denominator //= GCD      # Print the final fraction    print(final_numerator, "/",          final_denominator) # Driver Code # Given NN = 3  # Given Numeratorarr1 = [ 1, 2, 5 ]  # Given Denominatorarr2 = [ 2, 1, 6 ]  # Function calladdReduce(N, arr1, arr2) # This code is contributed by code_hunt

## C#

 // C# program for the above approachusing System;class GFG{  // Function to find GCD of a & b// using Euclid Lemmastatic 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 formstatic 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 codepublic 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

## Javascript

 
Output:
10/3

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up