# Maximize sum of ratios of N given fractions by incrementing numerator and denominators K times by 1

• Last Updated : 28 Oct, 2021

Given a positive integer K and an array arr[] consisting of {numerator, denominator} of N fractions, the task is to find the sum of the ratio of the given fractions after incrementing numerators and denominators by 1, K number of times.

Examples:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: arr[][] = {{1, 2}, {3, 5}, {2, 2}}, K = 2
Output: 0.78333
Explanation:
The most optimal choice is to increment the first fraction K(= 2) number of times. Therefore, the sum of ratio is (3/4 + 3/5 + 2/2) / 3 = 0.78333, which is maximum possible.

Input: arr[][] = {{1, 1}, {4, 5}}, K = 5
Output: 0.95

Approach: The given problem can be solved by using the Greedy Approach, the idea is to increment that fraction among the given fractions whose increment maximizes the sum of ratios of fractions. This idea can be implemented using the priority queue. Follow the below steps to solve the problem:

• Initialize a Max Heap, say PQ using the priority queue which stores the value that will be incremented in the total average ratio if an operation is performed on ith index for all values of i in the range [0, N).
• Insert all the indexes of the fraction in the array arr[] in the priority queue PQ with the incremented value of fractions.
• Iterate over the range [0, K – 1] and perform the following steps:
• Pop the top element of the PQ.
• Increment the fraction of the current popped index.
• Insert all the current fractions in the array arr[] in the priority queue PQ with the incremented value of fractions.
• After completing the above steps, print the sum of ratios of all the fractions stored in priority_queue PQ.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to increment the K fractions``// from the given array to maximize the``// sum of ratios of the given fractions``double` `maxAverageRatio(``    ``vector >& arr, ``int` `K)``{``    ``// Size of the array``    ``int` `N = arr.size();` `    ``// Max priority queue``    ``priority_queue > q;` `    ``// Iterate through the array``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// Insert the incremented value``        ``// if an operation is performed``        ``// on the ith index``        ``double` `extra``            ``= (((``double``)arr[i] + 1)``               ``/ ((``double``)arr[i] + 1))``              ``- ((``double``)arr[i]``                 ``/ (``double``)arr[i]);``        ``q.push(make_pair(extra, i));``    ``}` `    ``// Loop to perform K operations``    ``while` `(K--) {``        ``int` `i = q.top().second;``        ``q.pop();` `        ``// Increment the numerator and``        ``// denominator of ith fraction``        ``arr[i] += 1;``        ``arr[i] += 1;` `        ``// Add the incremented value``        ``double` `extra``            ``= (((``double``)arr[i] + 1)``               ``/ ((``double``)arr[i] + 1))``              ``- ((``double``)arr[i]``                 ``/ (``double``)arr[i]);` `        ``q.push(make_pair(extra, i));``    ``}` `    ``// Stores the average ratio``    ``double` `ans = 0;``    ``for` `(``int` `i = 0; i < N; i++) {``        ``ans += ((``double``)arr[i]``                ``/ (``double``)arr[i]);``    ``}` `    ``// Return the ratio``    ``return` `ans / N;``}` `// Driver Code``int` `main()``{``    ``vector > arr``        ``= { { 1, 2 }, { 3, 5 }, { 2, 2 } };``    ``int` `K = 2;` `    ``cout << maxAverageRatio(arr, K);` `    ``return` `0;``}`

## Javascript

 ``
Output:
`0.783333`

Time Complexity: O(K*log N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up