Minimize operations to make both arrays equal by decrementing a value from either or both
Last Updated :
03 Jun, 2022
Given two arrays A[] and B[] having N integers, the task is to find the minimum operations required to make all the elements of both the array equal where at each operation, the following can be done:
- Decrement the value of A[i] by 1 where i lies in the range [0, N).
- Decrement the value of B[i] by 1 where i lies in the range [0, N).
- Decrement the value of A[i] and B[i] by 1 where i lies in the range [0, N).
Note: Elements in array A[] and B[] need not be equal to one another.
Example:
Input: arr1[] = {1, 2, 3}, arr2[] = {5, 4, 3}
Output: 5
Explanation: Operations can be performed in the following way:
- Decrement element at index 2 of A[] by 1. Hence, A[] = {1, 2, 2}.
- Decrement element at index 2 of A[] by 1. Hence, A[] = {1, 2, 1}.
- Decrement element at index 0 of B[] by 1. Hence, B[] = {4, 4, 3}.
- Decrement element at index 0 of B[] by 1. Hence, B[] = {3, 4, 3}.
- Decrement element at index 1 of both A[] and B[] by 1. Hence A[] = {1, 1, 1} and B[] = {3, 3, 3}
Therefore, all the elements of both the arrays A[] and B[] can be made equal in 5 operation which is the minimum possible.
Input: A[] = {7, 2, 8, 5, 3}, B[] = {3, 4, 5, 9, 1}, N = 5
Output: 23
Approach: The given problem can be solved using a Greedy Approach. Since all the possible operations only decrement the array values, all elements must be made equal to the smallest element in the given array. Suppose min_A and min_B are the smallest integers in the array A[] and B[] respectively. Hence the required answer will be the sum of max(A[i] – min_A, B[i] – min_B) for all possible values of i in the range [0, N).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperations( int a[], int b[], int N)
{
int min_a = *min_element(a, a + N);
int min_b = *min_element(b, b + N);
int ans = 0;
for ( int i = 0; i < N; i++) {
int x = a[i] - min_a;
int y = b[i] - min_b;
ans += max(x, y);
}
return ans;
}
int main()
{
int a[] = { 7, 2, 8, 5, 3 };
int b[] = { 3, 4, 5, 9, 1 };
int N = sizeof (a) / sizeof (b[0]);
cout << minOperations(a, b, N);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <limits.h>
int max( int a, int b)
{
return (a > b) ? a : b;
}
int minOperations( int a[], int b[], int N)
{
int min_a = INT_MAX;
int min_b = INT_MAX;
for ( int i = 0; i < N; i++)
{
if (a[i] < min_a)
min_a = a[i];
if (b[i] < min_b)
min_b = b[i];
}
int ans = 0;
for ( int i = 0; i < N; i++)
{
int x = a[i] - min_a;
int y = b[i] - min_b;
ans += max(x, y);
}
return ans;
}
int main()
{
int a[] = {7, 2, 8, 5, 3};
int b[] = {3, 4, 5, 9, 1};
int N = sizeof (a) / sizeof (b[0]);
printf ( "%d" , minOperations(a, b, N));
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int minOperations( int []a, int []b, int N)
{
int min_a = Arrays.stream(a).min().getAsInt();
int min_b = Arrays.stream(b).min().getAsInt();
int ans = 0 ;
for ( int i = 0 ; i < N; i++) {
int x = a[i] - min_a;
int y = b[i] - min_b;
ans += Math.max(x, y);
}
return ans;
}
public static void main(String args[])
{
int []a = { 7 , 2 , 8 , 5 , 3 };
int []b = { 3 , 4 , 5 , 9 , 1 };
int N = a.length;
System.out.println(minOperations(a, b, N));
}
}
|
Python3
def minOperations(a, b, N):
min_a = min (a)
min_b = min (b)
ans = 0
for i in range (N):
x = a[i] - min_a
y = b[i] - min_b
ans + = max (x, y)
return ans
if __name__ = = "__main__" :
a = [ 7 , 2 , 8 , 5 , 3 ]
b = [ 3 , 4 , 5 , 9 , 1 ]
N = len (a)
print (minOperations(a, b, N))
|
C#
using System;
using System.Linq;
public class GFG
{
static int minOperations( int []a, int []b, int N)
{
int min_a = a.Min();
int min_b = b.Min();
int ans = 0;
for ( int i = 0; i < N; i++) {
int x = a[i] - min_a;
int y = b[i] - min_b;
ans += Math.Max(x, y);
}
return ans;
}
public static void Main()
{
int []a = { 7, 2, 8, 5, 3 };
int []b = { 3, 4, 5, 9, 1 };
int N = a.Length;
Console.Write(minOperations(a, b, N));
}
}
|
Javascript
<script>
function minOperations(a, b, N)
{
let min_a = Math.min.apply(Math,a);
let min_b = Math.min.apply(Math,b);
let ans = 0;
for (let i = 0; i < N; i++) {
let x = a[i] - min_a;
let y = b[i] - min_b;
ans += Math.max(x, y);
}
return ans;
}
let a = [ 7, 2, 8, 5, 3 ];
let b = [ 3, 4, 5, 9, 1 ];
let N = a.length;
document.write(minOperations(a, b, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...