Given two arrays A[] and B[] consisting of N and M integers, the task is to find the minimum number of operations required to make the minimum element of the array A[] at least the maximum element of the array B[] such that in each operation any array element A[] can be incremented by 1 or any array element B[] can be decremented by 1.
Examples:
Input: A[] = {2, 3}, B[] = {3, 5}
Output: 3
Explanation:
Following are the operations performed:
- Increase the value of A[1] by 1 modifies the array A[] = {3, 3}.
- Decrease the value of B[2] by 1 modifies the array B[] = {3, 4}.
- Decrease the value of B[2] by 1 modifies the array B[] = {3, 3}.
After the above operations, the minimum elements of the array A[] is 3 which is greater than or equal to the maximum element of the array B[] is 3. Therefore, the total number of operations is 3.
Input: A[] = {1, 2, 3}, B[] = {4}
Output: 3
Approach: The problem can be solved by using the Greedy Approach. Follow the steps below to solve the given problem:
- Sort the array A[] in increasing order.
- Sort the array B[] in decreasing order.
- Traverse both of the arrays while A[i] < B[i], in order to make all elements of the arrays A[] and B[], till index i, equal, say x, then the total number of operations is given by:
=> (B[0] + B[1] + … + B[i]) – i*x + (A[0] + A[1] + … + A[i]) + i*x
=> (B[0] – A[0]) + (B[1] – A[1]) + … + (B[i] – A[i]).
- Traverse both the arrays until the value of A[i] is smaller than B[i], and the value of (B[i] – A[i]) to the variable, say ans.
- After completing the above steps, print the value of ans as the minimum number of operations required.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;#define ll long longÂ
// Comparator functionbool cmp(ll a, ll b) { return a > b; }Â
// Function to find the minimum number// of operation required to satisfy the// given conditionsint FindMinMoves(vector<ll> A, vector<ll> B){Â Â Â Â int n, m;Â Â Â Â n = A.size();Â Â Â Â m = B.size();Â
    // sort the array A and B in the    // ascending and descending order    sort(A.begin(), A.end());    sort(B.begin(), B.end(), cmp);Â
    ll ans = 0;Â
    // Iterate over both the arrays    for (int i = 0; i < min(m, n)                    && A[i] < B[i];         ++i) {Â
        // Add the difference to the        // variable answer        ans += (B[i] - A[i]);    }Â
    // Return the resultant operations    return ans;}Â
// Driver Codeint main(){Â Â Â Â vector<ll> A = { 2, 3 };Â Â Â Â vector<ll> B = { 3, 5 };Â Â Â Â cout << FindMinMoves(A, B);Â
    return 0;} |
Java
// Java program for the above approachimport java.util.Arrays;Â
class GFG{Â Â Â Â Â // Comparator functionpublic static boolean cmp(int a, int b){Â Â Â Â return a > b;}Â
// Function to find the minimum number// of operation required to satisfy the// given conditionspublic static int FindMinMoves(int[] A, int[] B){Â Â Â Â int n, m;Â Â Â Â n = A.length;Â Â Â Â m = B.length;Â
    // Sort the array A and B in the    // ascending and descending order    Arrays.sort(A);    Arrays.sort(B);Â
    int ans = 0;Â
    // Iterate over both the arrays    for(int i = 0;             i < Math.min(m, n) && A[i] < B[i]; ++i)     {                 // Add the difference to the        // variable answer        ans += (B[i] - A[i]);    }Â
    // Return the resultant operations    return ans;}Â
// Driver Codepublic static void main(String args[]){Â Â Â Â int[] A = { 2, 3 };Â Â Â Â int[] B = { 3, 5 };Â Â Â Â Â Â Â Â Â System.out.println(FindMinMoves(A, B));}}Â
// This code is contributed by _saurabh_jaiswal |
Python3
# Python3 program for the above approachÂ
# Function to find the minimum number# of operation required to satisfy the# given conditionsdef FindMinMoves(A, B):Â Â Â Â Â Â Â Â Â n = len(A)Â Â Â Â m = len(B)Â
    # sort the array A and B in the    # ascending and descending order    A.sort()    B.sort(reverse = True)    ans = 0Â
    # Iterate over both the arrays    i = 0         for i in range(min(m, n)):Â
        # Add the difference to the        # variable answer        if A[i] < B[i]:            ans += (B[i] - A[i])Â
    # Return the resultant operations    return ansÂ
# Driver CodeA = [ 2, 3 ]B = [ 3, 5 ]Â
print(FindMinMoves(A, B))Â
# This code is contributed by gfgking |
C#
// C# program for the above approachusing System;Â
class GFG{Â
// Comparator functionpublic static bool cmp(int a, int b){Â Â Â Â return a > b;}Â
// Function to find the minimum number// of operation required to satisfy the// given conditionspublic static int FindMinMoves(int[] A, int[] B){Â Â Â Â int n, m;Â Â Â Â n = A.Length;Â Â Â Â m = B.Length;Â
    // Sort the array A and B in the    // ascending and descending order    Array.Sort(A);    Array.Sort(B);Â
    int ans = 0;Â
    // Iterate over both the arrays    for(int i = 0;             i < Math.Min(m, n) && A[i] < B[i]; ++i)     {                 // Add the difference to the        // variable answer        ans += (B[i] - A[i]);    }Â
    // Return the resultant operations    return ans;}Â
// Driver Codepublic static void Main(){Â Â Â Â int[] A = { 2, 3 };Â Â Â Â int[] B = { 3, 5 };Â Â Â Â Â Â Â Â Â Console.Write(FindMinMoves(A, B));}}Â
// This code is contributed by target_2. |
Javascript
<script>Â
       // JavaScript program for the above approachÂ
       // Function to find the minimum number       // of operation required to satisfy the       // given conditions       function FindMinMoves(A, B)        {           let n, m;           n = A.length;           m = B.length;Â
           // sort the array A and B in the           // ascending and descending order           A.sort(function (a, b) { return a - b; });           B.sort(function (a, b) { return b - a; });Â
           let ans = 0;Â
           // Iterate over both the arrays           for (let i = 0; i < Math.min(m, n)               && A[i] < B[i];               ++i) {Â
               // Add the difference to the               // variable answer               ans += (B[i] - A[i]);           }Â
           // Return the resultant operations           return ans;       }Â
       // Driver Code       let A = [2, 3];       let B = [3, 5];       document.write(FindMinMoves(A, B));Â
   // This code is contributed by Potta Lokesh   </script> |
3
Â
Time Complexity: O(K*log K), where the value of K is max(N, M).
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
