Making elements of two arrays same with minimum increment/decrement
Last Updated :
17 Aug, 2022
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 :
- First sort both the arrays.
- 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 the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
int MinOperation( int a[], int b[], int n)
{
sort(a, a + n);
sort(b, b + n);
int result = 0;
for ( int i = 0; i < n; ++i) {
result = result + abs (a[i] - b[i]);
}
return result;
}
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;
}
|
Java
import java.util.Arrays;
import java.io.*;
class GFG
{
static int MinOperation( int a[],
int b[],
int n)
{
Arrays.sort(a);
Arrays.sort(b);
int result = 0 ;
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;
}
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));
}
}
|
Python3
def MinOperation(a, b, n):
a.sort(reverse = False )
b.sort(reverse = False )
result = 0
for i in range ( 0 , n, 1 ):
if (a[i] > b[i]):
result = result + abs (a[i] - b[i])
elif (a[i] < b[i]):
result = result + abs (a[i] - b[i])
return result
if __name__ = = '__main__' :
a = [ 3 , 1 , 1 ]
b = [ 1 , 2 , 2 ]
n = len (a)
print (MinOperation(a, b, n))
|
C#
using System;
public class GFG {
static int MinOperation( int []a,
int []b,
int n)
{
Array.Sort(a);
Array.Sort(b);
int result = 0;
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;
}
public static void Main ()
{
int []a = {3, 1, 1};
int []b = {1, 2, 2};
int n = a.Length;
Console.WriteLine(MinOperation(a, b, n));
}
}
|
PHP
<?php
function MinOperation( $a , $b , $n )
{
sort( $a );
sort( $b );
$result = 0;
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 ;
}
$a = array ( 3, 1, 1 );
$b = array ( 1, 2, 2 );
$n = sizeof( $a );
echo MinOperation( $a , $b , $n );
?>
|
Javascript
<script>
function MinOperation(a, b, n)
{
a.sort( function (a, b){ return a - b});
b.sort( function (a, b){ return a - b});
let result = 0;
for (let 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;
}
let a = [3, 1, 1];
let b = [1, 2, 2];
let n = a.length;
document.write(MinOperation(a, b, n));
</script>
|
Complexity Analysis:
- Time Complexity : O(n Log n)
- Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...