Making elements of two arrays same with minimum increment/decrement

Given two arrays of same size, we need to convert the first array into another with minimum operations. In an operation, we can either increment or decrement an element by one. Note that orders of appearance of elements do not need to be same.

Here to convert one number into another we can add or subtract 1 from it.

Examples :

Input : a = { 3, 1, 1 }, b = { 1, 2, 2 }
Output : 2
Explanation : Here we can increase any 1 into 2 by 1 operation and 3 to 2 in one decrement operation. So a[] becomes {2, 2, 1} which is a permutation of b[].

Input : a = { 3, 1, 1 }, b = { 1, 1, 2 }
Output : 1



Algorithm :
1. First sort both the arrays.
2. After sorting we will run a loop in which we compare the first and second array elements and calculate the required operation needed to make first array equal to second.

Below is implementation of the above approach

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find minimum increment/decrement
// operations to make array elements same.
#include <bits/stdc++.h>
using namespace std;
  
int MinOperation(int a[], int b[], int n)
{
    // sorting both arrays in
    // ascending order
    sort(a, a + n);
    sort(b, b + n);
  
    // variable to store the
    // final result
    int result = 0;
  
    // After sorting both arrays
    // Now each array is in non-
    // decreasing order. Thus,
    // we will now compare each
    // element of the array and
    // do the increment or decrement
    // operation depending upon the
    // value of array b[].
    for (int i = 0; i < n; ++i) {
        if (a[i] > b[i])
            result = result + abs(a[i] - b[i]);
  
        else if (a[i] < b[i])
            result = result + abs(a[i] - b[i]);
    }
  
    return result;
}
  
// Driver code
int main()
{
    int a[] = { 3, 1, 1 };
    int b[] = { 1, 2, 2 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << MinOperation(a, b, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find minimum 
// increment/decrement operations
// to make array elements same.
import java.util.Arrays;
import java.io.*;
  
class GFG 
{
static int MinOperation(int a[],
                        int b[], 
                        int n)
{
    // sorting both arrays 
    // in ascending order
    Arrays.sort(a);
    Arrays.sort(b);
      
  
    // variable to store 
    // the final result
    int result = 0;
  
    // After sorting both arrays
    // Now each array is in non-
    // decreasing order. Thus,
    // we will now compare each
    // element of the array and
    // do the increment or decrement
    // operation depending upon the
    // value of array b[].
    for (int i = 0; i < n; ++i) 
    {
        if (a[i] > b[i])
            result = result +
                     Math.abs(a[i] - b[i]);
  
        else if (a[i] < b[i])
            result = result + 
                     Math.abs(a[i] - b[i]);
    }
  
    return result;
}
  
// Driver code
public static void main (String[] args) 
{
    int a[] = {3, 1, 1};
    int b[] = {1, 2, 2};
    int n = a.length;
    System.out.println(MinOperation(a, b, n));
}
}
  
// This code is contributed
// by akt_mit

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

//C# program to find minimum  
// increment/decrement operations 
// to make array elements same. 
using System;
public class GFG { 
  
static int MinOperation(int []a, 
                        int []b,  
                        int n) 
    // sorting both arrays  
    // in ascending order 
    Array.Sort(a); 
    Array.Sort(b); 
        
    
    // variable to store  
    // the final result 
    int result = 0; 
    
    // After sorting both arrays 
    // Now each array is in non- 
    // decreasing order. Thus, 
    // we will now compare each 
    // element of the array and 
    // do the increment or decrement 
    // operation depending upon the 
    // value of array b[]. 
    for (int i = 0; i < n; ++i)  
    
        if (a[i] > b[i]) 
            result = result + 
                     Math.Abs(a[i] - b[i]); 
    
        else if (a[i] < b[i]) 
            result = result +  
                     Math.Abs(a[i] - b[i]); 
    
    
    return result; 
    
// Driver code 
public static void Main ()  
    int []a = {3, 1, 1}; 
    int []b = {1, 2, 2}; 
    int n = a.Length; 
    Console.WriteLine(MinOperation(a, b, n)); 
/*This C# code is contributed by 29AjayKumar*/

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find minimum 
// increment/decrement operations 
// to make array elements same.
function MinOperation($a, $b, $n)
{
    // sorting both arrays in
    // ascending order
      
    sort($a);
    sort($b);
  
    // variable to store 
    // the final result
    $result = 0;
  
    // After sorting both arrays
    // Now each array is in non-
    // decreasing order. Thus,
    // we will now compare each
    // element of the array and
    // do the increment or decrement
    // operation depending upon the
    // value of array b[].
    for ($i = 0; $i < $n; ++$i
    {
        if ($a[$i] > $b[$i])
            $result = $result + abs($a[$i] - 
                                    $b[$i]);
  
        else if ($a[$i] < $b[$i])
            $result = $result + abs($a[$i] - 
                                    $b[$i]);
    }
  
    return $result;
}
  
// Driver code
$a = array ( 3, 1, 1 );
$b = array ( 1, 2, 2 );
$n = sizeof($a);
echo MinOperation($a, $b, $n);
  
// This code is contributed by ajit
?>

chevron_right


Output :

2

Time Complexity : O(n Log n)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t, 29AjayKumar