Open In App

Minimum money required to buy items with given cashbacks

Last Updated : 28 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] of length N, denoting the cost of N items and the cashback upon buying that item, the task is to find the minimum amount of money required to buy all the items irrespective of the order in which they are bought.

Examples:

Input: arr[] = [ [ 7, 2 ], [ 10, 5 ], [ 2, 1] ] 
Output: 16
Explanation: The geek can buy the goodies in the following ways:
[7, 2] ->  [10, 5] -> [2, 1]  minimum money required are 15.
Initially 15. After buying the first element (15-7+2) = 10.
After buying the second element (10-10+5) = 5.
After buying the third item (5-2+1) = 4.
If anything less than 15 was used then one would not have enough money
to buy the second item. Similarly for the following cases
[7, 2] -> [2, 1] -> [10, 5] minimum money required is 16
[10, 5] -> [7, 2] -> [2, 1] minimum money required is12
[10, 5] -> [2, 1] -> [7, 2] minimum money required is 13
[2, 1] -> [7, 2] -> [10, 5] minimum money required is 16
[2, 1] -> [10, 5] -> [7, 2] minimum money required is 13
In the worst case, geek requires 16 unit of money

Input: arr[] = [ [ 5, 6 ], [40, 2], [89, 8] ]
Output: 127

Naive Approach: To solve the problem follow the below idea:

Generate all the ways possible and find the minimum money required for each permutation.

Time Complexity: O(N*N!)
Auxiliary Space: O(N)

Efficient Approach: This problem can be solved by using the below observation:

  • Calculate the effective amount of money spent on items where the price ? refund. Effective amount spend = price – refund. 
  • Calculate the maximum price among those items where the refund > price. Effective amount spend = price of most expensive item.
  • Add refund from the last transaction because we cannot use refund from the last transaction to reduce effective amount spend. 
    • Last transaction in worst case will be either having maximum refund such that price of that item > refund. 
    • Otherwise, last transaction will have maximum cost if refund on purchasing that item is greater than its price.

Follow the steps mentioned below to implement the above idea:

  • Iterate through the array from i = 0 to N-1:
    • Add the effective cost.
    • In each iteration calculate the maximum value incurred in the last transaction in worst case using the idea mentioned above.
  • At the end add the maximum cost of last transaction in worst case with the total effective cost to get the required answer.

Below is the implementation of the above idea.

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum money required
long long minimumGeekBits(vector<vector<int> >& Goodies)
{
    long long ans = 0;
 
    // v will store the refund from the last
    // transaction in the worst case
    int v = 0;
 
    // Calculate Effective Geek bit spent
    for (int i = 0; i < Goodies.size(); i++) {
        ans += max(Goodies[i][0] - Goodies[i][1], 0);
        v = max(v, min(Goodies[i][0], Goodies[i][1]));
    }
 
    // Add refund from last transaction
    // in worst case
    return ans + v;
}
 
// Driver code
int main()
{
    vector<vector<int> > arr
        = { { 7, 2 }, { 10, 5 }, { 2, 1 } };
 
    // Function Call
    cout << minimumGeekBits(arr);
    return 0;
}


Java




// Java code to implement the approach
import java.io.*;
 
class GFG {
    // Function to find the minimum money required
    public static long minimumGeekBits(int Goodies[][])
    {
        long ans = 0;
 
        // v will store the refund from the last
        // transaction in the worst case
        int v = 0;
 
        // Calculate Effective Geek bit spent
        for (int i = 0; i < Goodies.length; i++) {
            ans += Math.max(Goodies[i][0] - Goodies[i][1],
                            0);
            v = Math.max(
                v, Math.min(Goodies[i][0], Goodies[i][1]));
        }
 
        // Add refund from last transaction
        // in worst case
        return ans + v;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[][] = { { 7, 2 }, { 10, 5 }, { 2, 1 } };
 
        // Function Call
        System.out.print(minimumGeekBits(arr));
    }
}
 
// This code is contributed by Rohit Pradhan


Python3




# python code to implement the approach
 
# Function to find the minimum money required
def minimumGeekBits(Goodies):
    ans = 0
 
    # v will store the refund from the last
    # transaction in the worst case
    v = 0
    l= len(Goodies)
     
    # Calculate Effective Geek bit spent
    for i in range(0,l):
        ans += max(Goodies[i][0] - Goodies[i][1], 0)
        v = max(v, min(Goodies[i][0], Goodies[i][1]))
 
    # Add refund from last transaction
    # in worst case
    return ans + v;
 
# driver code
arr = [ [ 7, 2 ], [ 10, 5 ], [ 2, 1 ] ]
 
# Function Call
print(minimumGeekBits(arr))
 
# This code is contributed by ksam24000


C#




// C# implementation
using System;
public class HelloWorld
{
 
// Function to find the minimum money required
public static long minimumGeekBits(int[,] Goodies)
{
    long ans = 0;
 
    // v will store the refund from the last
    // transaction in the worst case
    int v = 0;
   
    // Calculate Effective Geek bit spent
    for (int i = 0; i < Goodies.GetLength(0); i++) {
        ans += Math.Max(Goodies[i,0] - Goodies[i,1], 0);
        v = Math.Max(v, Math.Min(Goodies[i,0], Goodies[i,1]));
    }
 
    // Add refund from last transaction
    // in worst case
    return ans + v;
}
    public static void Main(string[] args)
    {
        int [,] arr
        = { { 7, 2 }, { 10, 5 }, { 2, 1 } };
         
    // Function Call
    Console.WriteLine(minimumGeekBits(arr));
 
    }
}
 
// This code is contributed by ksam24000


Javascript




<script>
       // JavaScript code for the above approach
 
       // Function to find the minimum money required
       function minimumGeekBits(Goodies) {
           let ans = 0;
 
           // v will store the refund from the last
           // transaction in the worst case
           let v = 0;
 
           // Calculate Effective Geek bit spent
           for (let i = 0; i < Goodies.length; i++) {
               ans += Math.max(Goodies[i][0] - Goodies[i][1], 0);
               v = Math.max(v, Math.min(Goodies[i][0], Goodies[i][1]));
           }
 
           // Add refund from last transaction
           // in worst case
           return ans + v;
       }
 
       // Driver code
       let arr
           = [[7, 2], [10, 5], [2, 1]];
 
       // Function Call
       document.write(minimumGeekBits(arr));
 
// This code is contributed by Potta Lokesh
 
   </script>


Output

16

Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads