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:
=> (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++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
bool cmp(ll a, ll b) { return a > b; }
int FindMinMoves(vector<ll> A, vector<ll> B)
{
int n, m;
n = A.size();
m = B.size();
sort(A.begin(), A.end());
sort(B.begin(), B.end(), cmp);
ll ans = 0;
for ( int i = 0; i < min(m, n)
&& A[i] < B[i];
++i) {
ans += (B[i] - A[i]);
}
return ans;
}
int main()
{
vector<ll> A = { 2, 3 };
vector<ll> B = { 3, 5 };
cout << FindMinMoves(A, B);
return 0;
}
|
Java
import java.util.Arrays;
class GFG{
public static boolean cmp( int a, int b)
{
return a > b;
}
public static int FindMinMoves( int [] A, int [] B)
{
int n, m;
n = A.length;
m = B.length;
Arrays.sort(A);
Arrays.sort(B);
int ans = 0 ;
for ( int i = 0 ;
i < Math.min(m, n) && A[i] < B[i]; ++i)
{
ans += (B[i] - A[i]);
}
return ans;
}
public static void main(String args[])
{
int [] A = { 2 , 3 };
int [] B = { 3 , 5 };
System.out.println(FindMinMoves(A, B));
}
}
|
Python3
def FindMinMoves(A, B):
n = len (A)
m = len (B)
A.sort()
B.sort(reverse = True )
ans = 0
i = 0
for i in range ( min (m, n)):
if A[i] < B[i]:
ans + = (B[i] - A[i])
return ans
A = [ 2 , 3 ]
B = [ 3 , 5 ]
print (FindMinMoves(A, B))
|
C#
using System;
class GFG{
public static bool cmp( int a, int b)
{
return a > b;
}
public static int FindMinMoves( int [] A, int [] B)
{
int n, m;
n = A.Length;
m = B.Length;
Array.Sort(A);
Array.Sort(B);
int ans = 0;
for ( int i = 0;
i < Math.Min(m, n) && A[i] < B[i]; ++i)
{
ans += (B[i] - A[i]);
}
return ans;
}
public static void Main()
{
int [] A = { 2, 3 };
int [] B = { 3, 5 };
Console.Write(FindMinMoves(A, B));
}
}
|
Javascript
<script>
function FindMinMoves(A, B)
{
let n, m;
n = A.length;
m = B.length;
A.sort( function (a, b) { return a - b; });
B.sort( function (a, b) { return b - a; });
let ans = 0;
for (let i = 0; i < Math.min(m, n)
&& A[i] < B[i];
++i) {
ans += (B[i] - A[i]);
}
return ans;
}
let A = [2, 3];
let B = [3, 5];
document.write(FindMinMoves(A, B));
</script>
|
Time Complexity: O(K*log K), where the value of K is max(N, M).
Auxiliary Space: O(1)