Open In App

Minimize operations to make both arrays equal by decrementing a value from either or both

Last Updated : 03 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Decrement element at index 2 of A[] by 1. Hence, A[] = {1, 2, 2}.
  2. Decrement element at index 2 of A[] by 1. Hence, A[] = {1, 2, 1}.
  3. Decrement element at index 0 of B[] by 1. Hence, B[] = {4, 4, 3}.
  4. Decrement element at index 0 of B[] by 1. Hence, B[] = {3, 4, 3}.
  5. 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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum operations
// required to make elements of each array
// equal of the given two arrays
int minOperations(int a[], int b[], int N)
{
    // Stores the minimum element in array a[]
    int min_a = *min_element(a, a + N);
 
    // Stores the minimum element in array b[]
    int min_b = *min_element(b, b + N);
 
    // Variable to store the required ans
    int ans = 0;
 
    // Iterate over the elements
    for (int i = 0; i < N; i++) {
        // Store the difference between current
        // element and minimum of respective array
        int x = a[i] - min_a;
        int y = b[i] - min_b;
 
        // Add maximum of x and y to ans
        ans += max(x, y);
    }
 
    // Return Answer
    return ans;
}
 
// Driver Code
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++)
  {
    // Stores the minimum element in array a[]
    if (a[i] < min_a)
      min_a = a[i];
    // Stores the minimum element in array b[]
    if (b[i] < min_b)
      min_b = b[i];
  }
  // Variable to store the required ans
  int ans = 0;
 
  for (int i = 0; i < N; i++)
  {
    // Store the difference between current
    // element and minimum of respective array
    int x = a[i] - min_a;
    int y = b[i] - min_b;
    // Add maximum of x and y to ans
    ans += max(x, y);
  }
  // Return ans
  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;
}
 
// This code is contributed by abhinavprkash.


Java




// Java program for the above approach
import java.util.*;
 
public class GFG
{
// Function to find the minimum operations
// required to make elements of each array
// equal of the given two arrays
static int minOperations(int []a, int []b, int N)
{
    // Stores the minimum element in array a[]
    int min_a = Arrays.stream(a).min().getAsInt();
 
    // Stores the minimum element in array b[]
    int min_b = Arrays.stream(b).min().getAsInt();
 
    // Variable to store the required ans
    int ans = 0;
 
    // Iterate over the elements
    for (int i = 0; i < N; i++) {
        // Store the difference between current
        // element and minimum of respective array
        int x = a[i] - min_a;
        int y = b[i] - min_b;
 
        // Add maximum of x and y to ans
        ans += Math.max(x, y);
    }
 
    // Return Answer
    return ans;
}
 
// Driver Code
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));
}
}
// This code is contributed by Samim Hossain Mondal.


Python3




# Python program for the above approach
 
# Function to find the minimum operations
# required to make elements of each array
# equal of the given two arrays
def minOperations(a, b, N):
 
    # Stores the minimum element in array a[]
    min_a = min(a)
 
    # Stores the minimum element in array b[]
    min_b = min(b)
 
    # Variable to store the required ans
    ans = 0
 
    # Iterate over the elements
    for i in range(N):
       
       # Store the difference between current
       # element and minimum of respective array
        x = a[i] - min_a
        y = b[i] - min_b
 
       # Add maximum of x and y to ans
        ans += max(x, y)
 
    # Return Answer
    return ans
 
# Driver Code
if __name__ == "__main__":
    a = [7, 2, 8, 5, 3]
    b = [3, 4, 5, 9, 1]
    N = len(a)
    print(minOperations(a, b, N))
 
# This code is contributed by Potta Lokesh


C#




// C# program for the above approach
using System;
using System.Linq;
 
public class GFG
{
// Function to find the minimum operations
// required to make elements of each array
// equal of the given two arrays
static int minOperations(int []a, int []b, int N)
{
    // Stores the minimum element in array a[]
    int min_a = a.Min();
 
    // Stores the minimum element in array b[]
    int min_b = b.Min();
 
    // Variable to store the required ans
    int ans = 0;
 
    // Iterate over the elements
    for (int i = 0; i < N; i++) {
        // Store the difference between current
        // element and minimum of respective array
        int x = a[i] - min_a;
        int y = b[i] - min_b;
 
        // Add maximum of x and y to ans
        ans += Math.Max(x, y);
    }
 
    // Return Answer
    return ans;
}
 
// Driver Code
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));
}
}
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
// Javascript program for the above approach
 
// Function to find the minimum operations
// required to make elements of each array
// equal of the given two arrays
function minOperations(a, b, N)
{
    // Stores the minimum element in array a[]
    let min_a = Math.min.apply(Math,a);
 
    // Stores the minimum element in array b[]
    let min_b = Math.min.apply(Math,b);
 
    // Variable to store the required ans
    let ans = 0;
 
    // Iterate over the elements
    for (let i = 0; i < N; i++) {
        // Store the difference between current
        // element and minimum of respective array
        let x = a[i] - min_a;
        let y = b[i] - min_b;
 
        // Add maximum of x and y to ans
        ans += Math.max(x, y);
    }
 
    // Return Answer
    return ans;
}
 
// Driver Code
let a = [ 7, 2, 8, 5, 3 ];
let b = [ 3, 4, 5, 9, 1 ];
let N = a.length;
 
document.write(minOperations(a, b, N));
 
// This code is contributed by Samim Hossain Mondal.
</script>


Output

23

Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads