Minimize swap of elements such that one Array has greater sum than other
Given two arrays A[] and B[] with size N and M, the task is to find the minimum number of swaps between two arrays (can be any two element of two arrays) required to make the sum of array A[] strictly greater than array B[].
Examples:
Input: N = 5, A[] = {1, 5, 4, 6, 2}, M = 7, B[] = {0, 1, 17, 4, 6, 2, 9}
Output: 1
Explanation: In the given example, to make A[] strictly greater:
Firstly, swap 0th index element from A[] with 2nd index element from B[].
After that, sum of array A[] is 17+5+4+6+2 = 34 and
sum of array B is 0+1+1+4+6+2+9=23.
Thus, sum of array A[] is greater than sum of array B[] by performing only 1 swap.
Input: N = 5, A[] = {5, 6, 7, 8, 9} , M = 5, B[] = {0, 1, 2, 3, 4}
Output: 0
Explanation: As, the sum of array A[] is greater than sum of array B[].
Hence, no swap between the arrays is required.
Approach: The problem can be solved based on the following idea:
To minimize the number of operations, the most optimal choice is to swap the minimum elements of array A[] with maximum elements of array B[].
Follow the steps to solve the problem:
- Sort array A[] in increasing order and array B[] in decreasing order.
- Calculate the sum of both arrays.
- If the sum of array A[] is greater than the sum of array B[] then return 0.
- Otherwise, swap maximum element from array B[] and minimum element from array A[] until sum of A[] is not greater than B[].
- Return the number of operations required to make array A[] strictly greater than array B[].
- If not possible, return -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumopretion( int A[], int N,
int B[], int M)
{
sort(A, A + N);
sort(B, B + M, greater< int >());
int suma = 0, sumb = 0;
for ( int i = 0; i < N; i++) {
suma += A[i];
}
for ( int i = 0; i < M; i++) {
sumb += B[i];
}
int count = 0, flag = 0;
if (suma > sumb) {
return 0;
}
else {
int x = min(M, N);
for ( int i = 0; i < x; i++) {
suma += (B[i] - A[i]);
sumb += (A[i] - B[i]);
count++;
if (suma > sumb)
break ;
}
if (suma <= sumb)
return -1;
else
return count;
}
}
int main()
{
int A[] = { 1, 5, 4, 6, 2 };
int B[] = { 0, 1, 17, 4, 6, 2, 9 };
int N = sizeof (A) / sizeof (A[0]);
int M = sizeof (B) / sizeof (B[0]);
cout << minimumopretion(A, N, B, M);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void reverse( int [] array)
{
int n = array.length;
for ( int i = 0 ; i < n / 2 ; i++) {
int temp = array[i];
array[i] = array[n - i - 1 ];
array[n - i - 1 ] = temp;
}
}
static int minimumopretion( int [] A, int N, int [] B,
int M)
{
Arrays.sort(A);
Arrays.sort(B);
reverse(B);
int suma = 0 , sumb = 0 ;
for ( int i = 0 ; i < N; i++) {
suma += A[i];
}
for ( int i = 0 ; i < M; i++) {
sumb += B[i];
}
int count = 0 , flag = 0 ;
if (suma > sumb) {
return 0 ;
}
else {
int x = Math.min(M, N);
for ( int i = 0 ; i < x; i++) {
suma += (B[i] - A[i]);
sumb += (A[i] - B[i]);
count++;
if (suma > sumb)
break ;
}
if (suma <= sumb)
return - 1 ;
else
return count;
}
}
public static void main (String[] args) {
int A[] = { 1 , 5 , 4 , 6 , 2 };
int B[] = { 0 , 1 , 17 , 4 , 6 , 2 , 9 };
int N = A.length;
int M = B.length;
System.out.print(minimumopretion(A, N, B, M));
}
}
|
Python3
from audioop import reverse
def minimumopretion(A, N, B, M):
A.sort()
B.sort(reverse = True )
suma, sumb = 0 , 0
for i in range ( 0 , N):
suma + = A[i]
for i in range ( 0 , M):
sumb + = B[i]
count, flag = 0 , 0
if (suma > sumb):
return 0
else :
x = min (M, N)
for i in range ( 0 , x):
suma + = (B[i] - A[i])
sumb + = (A[i] - B[i])
count + = 1
if (suma > sumb):
break
if (suma < = sumb):
return - 1
else :
return count
if __name__ = = "__main__" :
A = [ 1 , 5 , 4 , 6 , 2 ]
B = [ 0 , 1 , 17 , 4 , 6 , 2 , 9 ]
N = len (A)
M = len (B)
print (minimumopretion(A, N, B, M))
|
C#
using System;
class GFG {
static int minimumopretion( int [] A, int N, int [] B,
int M)
{
Array.Sort(A);
Array.Sort< int >(
B, delegate ( int m, int n) { return n - m; });
int suma = 0, sumb = 0;
for ( int i = 0; i < N; i++) {
suma += A[i];
}
for ( int i = 0; i < M; i++) {
sumb += B[i];
}
int count = 0, flag = 0;
if (suma > sumb) {
return 0;
}
else {
int x = Math.Min(M, N);
for ( int i = 0; i < x; i++) {
suma += (B[i] - A[i]);
sumb += (A[i] - B[i]);
count++;
if (suma > sumb)
break ;
}
if (suma <= sumb)
return -1;
else
return count;
}
}
public static void Main()
{
int [] A = { 1, 5, 4, 6, 2 };
int [] B = { 0, 1, 17, 4, 6, 2, 9 };
int N = A.Length;
int M = B.Length;
Console.Write(minimumopretion(A, N, B, M));
}
}
|
Javascript
<script>
function minimumopretion(A, N, B, M)
{
A.sort();
B.sort( function (a, b) { return b - a })
let suma = 0, sumb = 0;
for (let i = 0; i < N; i++) {
suma += A[i];
}
for (let i = 0; i < M; i++) {
sumb += B[i];
}
let count = 0, flag = 0;
if (suma > sumb) {
return 0;
}
else {
let x = Math.min(M, N);
for (let i = 0; i < x; i++) {
suma += (B[i] - A[i]);
sumb += (A[i] - B[i]);
count++;
if (suma > sumb)
break ;
}
if (suma <= sumb)
return -1;
else
return count;
}
}
let A = [1, 5, 4, 6, 2];
let B = [0, 1, 17, 4, 6, 2, 9];
let N = A.length;
let M = B.length;
document.write(minimumopretion(A, N, B, M));
</script>
|
Time Complexity: O(N* log N)
Auxiliary Space: O(1)
Another Approach:
- Start by declaring the function minimumOperation with input parameters A, N, B, and M.
- Initialize variables sumA and sumB to zero.
- Use a for loop to iterate through each element of array A and add its value to sumA.
- Use another for loop to iterate through each element of array B and add its value to sumB.
- If sumA is greater than sumB, return 0 as no operation is required.
- Sort the array A in ascending order using the sort function.
- Sort the array B in descending order using the sort function and passing greater<int>() as the second argument.
- Initialize variables i, j, and count to zero.
- Use a while loop to iterate through each element of arrays A and B.
- Compute the difference between the j-th element of B and the i-th element of A and store it in variable diff.
- If diff is less than or equal to zero, move to the next element of A by incrementing i.
- If diff is greater than zero, increment count, as a swap operation is required. Also, update the values of sumA and sumB accordingly.
- Move to the next element of both A and B by incrementing i and j.
- Check if sumA is greater than sumB. If so, return count as the minimum number of operations required to make the sum of A greater than B.
- If the end of the loop is reached and sumA is still less than or equal to sumB, return -1 as it is not possible to make the sum of A greater than B.
- Finally, in the main function, declare arrays A and B with sample values and compute their sizes N and M, respectively.
- Call the minimumOperation function with input parameters A, N, B, and M, and output the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumOperation( int A[], int N, int B[], int M) {
int sumA = 0, sumB = 0;
for ( int i = 0; i < N; i++) {
sumA += A[i];
}
for ( int i = 0; i < M; i++) {
sumB += B[i];
}
if (sumA > sumB) {
return 0;
}
sort(A, A + N);
sort(B, B + M, greater< int >());
int i = 0, j = 0, count = 0;
while (i < N && j < M) {
int diff = B[j] - A[i];
if (diff <= 0) {
i++;
}
else {
count++;
sumA += diff;
sumB -= diff;
i++;
j++;
if (sumA > sumB) {
return count;
}
}
}
return -1;
}
int main() {
int A[] = { 1, 5, 4, 6, 2 };
int B[] = { 0, 1, 17, 4, 6, 2, 9 };
int N = sizeof (A) / sizeof (A[0]);
int M = sizeof (B) / sizeof (B[0]);
cout << minimumOperation(A, N, B, M) << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
public static int minimumOperation( int [] A, int [] B) {
int N = A.length;
int M = B.length;
int sumA = 0 ;
int sumB = 0 ;
for ( int num : A) {
sumA += num;
}
for ( int num : B) {
sumB += num;
}
if (sumA > sumB) {
return 0 ;
}
Arrays.sort(A);
Arrays.sort(B);
reverse(B);
int i = 0 ;
int j = 0 ;
int count = 0 ;
while (i < N && j < M) {
int diff = B[j] - A[i];
if (diff <= 0 ) {
i++;
} else {
count++;
sumA += diff;
sumB -= diff;
i++;
j++;
if (sumA > sumB) {
return count;
}
}
}
return - 1 ;
}
private static void reverse( int [] arr) {
int left = 0 ;
int right = arr.length - 1 ;
while (left < right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
public static void main(String[] args) {
int [] A = { 1 , 5 , 4 , 6 , 2 };
int [] B = { 0 , 1 , 17 , 4 , 6 , 2 , 9 };
System.out.println(minimumOperation(A, B));
}
}
|
Python3
def minimum_operation(A, B):
N = len (A)
M = len (B)
sumA = sum (A)
sumB = sum (B)
if sumA > sumB:
return 0
A.sort()
B.sort(reverse = True )
i = 0
j = 0
count = 0
while i < N and j < M:
diff = B[j] - A[i]
if diff < = 0 :
i + = 1
else :
count + = 1
sumA + = diff
sumB - = diff
i + = 1
j + = 1
if sumA > sumB:
return count
return - 1
A = [ 1 , 5 , 4 , 6 , 2 ]
B = [ 0 , 1 , 17 , 4 , 6 , 2 , 9 ]
print (minimum_operation(A, B))
|
C#
using System;
class GFG
{
static int MinimumOperation( int [] A, int N, int [] B, int M)
{
int sumA = 0, sumB = 0;
for ( int i = 0; i < N; i++)
{
sumA += A[i];
}
for ( int i = 0; i < M; i++)
{
sumB += B[i];
}
if (sumA > sumB)
{
return 0;
}
Array.Sort(A);
Array.Sort(B, (a, b) => b.CompareTo(a));
int indexA = 0, indexB = 0, count = 0;
while (indexA < N && indexB < M)
{
int diff = B[indexB] - A[indexA];
if (diff <= 0)
{
indexA++;
}
else
{
count++;
sumA += diff;
sumB -= diff;
indexA++;
indexB++;
if (sumA > sumB)
{
return count;
}
}
}
return -1;
}
static void Main()
{
int [] A = { 1, 5, 4, 6, 2 };
int [] B = { 0, 1, 17, 4, 6, 2, 9 };
int N = A.Length;
int M = B.Length;
Console.WriteLine(MinimumOperation(A, N, B, M));
}
}
|
Javascript
function GFG(A, N, B, M) {
let sumA = 0, sumB = 0;
for (let i = 0; i < N; i++) {
sumA += A[i];
}
for (let i = 0; i < M; i++) {
sumB += B[i];
}
if (sumA >= sumB) {
return 0;
}
A.sort((a, b) => a - b);
B.sort((a, b) => b - a);
let i = 0, j = 0, count = 0;
while (i < N && j < M) {
let diff = B[j] - A[i];
if (diff <= 0) {
i++;
} else {
count++;
sumA += diff;
sumB -= diff;
i++;
j++;
if (sumA >= sumB) {
return count;
}
}
}
return -1;
}
let A = [1, 5, 4, 6, 2];
let B = [0, 1, 17, 4, 6, 2, 9];
let N = A.length;
let M = B.length;
console.log(GFG(A, N, B, M));
|
Time Complexity: O(n*logn)
Auxiliary Space: O(n+m)
Last Updated :
06 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...