Skip to content
Related Articles

Related Articles

Maximum subtraction of Array values from K to make it 0 or minimum

Improve Article
Save Article
  • Last Updated : 31 May, 2022
Improve Article
Save Article

Given two arrays A[] and B[] (B[i] < A[i]) of size N each and an integer K, the task is to perform the operation of (K – A[i] + B[i]) the maximum number of times such that K becomes 0 or as minimum as possible.

Note: At no moment an operation will be performed such that K becomes negative i.e. the operation cannot be performed if K – A[i] is negative and any index can be selected as many times as possible.

Examples:

Input: N = 3, K = 3, A[] = {2, 3, 4}, B[] = {1, 1, 1}
Output: 2
Explanation: For maximum turns,  
Choose A[0] and B[0], so  K = 3 – 2 + 1 = 2.
Choose A[0] and B[0], so  K = 2 – 2 + 1 = 1.
No other operation is possible. So max turns = 2.

Input: N = 2, K = 10, A[] = {5, 4} B[] ={1, 2}
Output: 4

 

Approach: The problem can be solved based on the following idea:

For performing the operation maximum number of times, always keep picking the indices (say i) so that A[i] – B[i] is minimum.

Say such value is A[i] and B[i], so the maximum performed operations for that index are (K – B[i]) / (A[i] – B[i]) [Here K – B[i] to avoid the last operation where K-A[i] is negative]

Follow the steps mentioned below to implement the idea:

  • Create an array (say v) to store the values in the form of (A[i] – B[i], A[i]).
  • Sort the array in increasing order on the basis of (A[i] – B[i]).
  • Loop from i = 0 to N-1:
    • If K – second value of v[i] is not negative then use the above formula to find its contribution (say x).
    • Increment the count by x.
  • Return the final count as the answer.

Below is the implementation of the above approach.

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the maximum operations
int MakeitMin(int n, int k, int a[], int b[])
{
    // Create a vector pair to store the difference
    vector<pair<int, int> > v(n);
    for (int i = 0; i < n; i++)
        v[i] = { a[i] - b[i], a[i] };
 
    // Sort it accordingly
    sort(v.begin(), v.end());
    int ans = 0;
    for (int i = 0; i < n; i++) {
 
        // Check if we can perform that turn or not
        if (v[i].second > k)
            continue;
 
        // If we can perform, calculate max times
        // we can perform that turn
        // and add it to ans
        int diff
            = (k - (v[i].second - v[i].first)) / v[i].first;
        ans += diff;
 
        // Reduce the k value
        k -= (diff * v[i].first);
    }
    return ans;
}
 
// Driver Code
int main()
{
    int N = 2, K = 10;
    int A[] = { 5, 4 };
    int B[] = { 1, 2 };
    cout << MakeitMin(N, K, A, B);
    return 0;
}


Java




// Java code to implement the approach
import java.io.*;
import java.util.*;
 
class GFG {
    // Function to calculate the maximum operations
    public static int MakeitMin(int n, int k, int a[],
                                int b[])
    {
        // Create a array of pairs to store the difference
        int v[][] = new int[n][2];
        for (int i = 0; i < n; i++) {
            v[i][0] = a[i] - b[i];
            v[i][1] = a[i];
        }
 
        // Sort it accordingly
        Arrays.sort(v,
                    (x, y) -> Integer.compare(x[0], y[0]));
        int ans = 0;
        for (int i = 0; i < n; i++) {
 
            // Check if we can perform that turn or not
            if (v[i][1] > k)
                continue;
 
            // If we can perform, calculate max times
            // we can perform that turn
            // and add it to ans
            int diff = (k - (v[i][1] - v[i][0])) / v[i][0];
            ans += diff;
 
            // Reduce the k value
            k -= (diff * v[i][0]);
        }
        return ans;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 2, K = 10;
        int A[] = { 5, 4 };
        int B[] = { 1, 2 };
        System.out.print(MakeitMin(N, K, A, B));
    }
}
 
// This code is contributed by Rohit Pradhan


Python3




# Python code to implement the approach
 
# Function to calculate the maximum operations
def MakeitMin(n, k, a, b):
 
    # Create a vector pair to store the difference
    v = [[] for i in range(n)]
    for i in range(n):
        v[i] = [a[i] - b[i], a[i]]
 
    # Sort it accordingly
    v.sort()
    ans = 0
    for i in range(n):
 
        # Check if we can perform that turn or not
        if (v[i][1] > k):
            continue
 
        # If we can perform, calculate max times
        # we can perform that turn
        # and add it to ans
        diff = (k - (v[i][1] - v[i][0])) // v[i][0]
        ans += diff
 
        # Reduce the k value
        k -= (diff * v[i][0])
         
    return ans
 
 
# Driver Code
N,K = 2,10
A = [5, 4]
B = [1, 2]
print(MakeitMin(N, K, A, B))
 
# This code is contributed by shinjanpatra


C#




// C# code to implement the approach
using System;
using System.Collections.Generic;
 
public class GFG
{
   
    // Function to calculate the maximum operations
    public static int MakeitMin(int n, int k, int[] a,
                                int[] b)
    {
       
        // Create a array of pairs to store the difference
        int[][] v = new int[n][];
        for (int i = 0; i < n; i++) {
            v[i] = new int[] { a[i] - b[i], a[i] };
        }
 
        // Sort it accordingly
        Array.Sort(v, (x, y) => { return x[0] - y[0]; });
 
        int ans = 0;
        for (int i = 0; i < n; i++) {
 
            // Check if we can perform that turn or not
            if (v[i][1] > k)
                continue;
 
            // If we can perform, calculate max times
            // we can perform that turn
            // and add it to ans
            int diff = (k - (v[i][1] - v[i][0])) / v[i][0];
            ans += diff;
 
            // Reduce the k value
            k -= (diff * v[i][0]);
        }
        return ans;
    }
   
      // Driver Code
    public static void Main(string[] args)
    {
        int N = 2, K = 10;
        int[] A = { 5, 4 };
        int[] B = { 1, 2 };
       
          // Function Call
        Console.WriteLine(MakeitMin(N, K, A, B));
    }
}
 
// This code is contributed by phasing17.


Javascript




<script>
    // JavaScript code to implement the approach
     
    // Function to calculate the maximum operations
    const MakeitMin = (n, k, a, b) => {
     
        // Create a vector pair to store the difference
        let v = new Array(n).fill([]);
        for (let i = 0; i < n; i++)
            v[i] = [a[i] - b[i], a[i]];
 
        // Sort it accordingly
        v.sort();
        let ans = 0;
        for (let i = 0; i < n; i++) {
 
            // Check if we can perform that turn or not
            if (v[i][1] > k)
                continue;
 
            // If we can perform, calculate max times
            // we can perform that turn
            // and add it to ans
            let diff
                = (k - (v[i][1] - v[i][0])) / v[i][0];
            ans += diff;
 
            // Reduce the k value
            k -= (diff * v[i][0]);
        }
        return ans;
    }
 
    // Driver Code
    let N = 2, K = 10;
    let A = [5, 4];
    let B = [1, 2];
    document.write(MakeitMin(N, K, A, B));
 
// This code is contributed by rakeshsahni
 
</script>


Output

4

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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!