Making elements of two arrays same with minimum increment/decrement Last Updated : 17 Aug, 2022 Comments Improve Suggest changes Like Article Like Report 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++ // 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) { 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; } Java // 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 Python3 # Python 3 program to find minimum # increment/decrement operations to # make array elements same. def MinOperation(a, b, n): # sorting both arrays in ascending order a.sort(reverse = False) b.sort(reverse = False) # 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 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 # Driver code if __name__ == '__main__': a = [3, 1, 1] b = [1, 2, 2] n = len(a) print(MinOperation(a, b, n)) # This code is contributed by # Sahil_Shelangia C# //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*/ PHP <?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 ?> JavaScript <script> // Javascript program to find minimum // increment/decrement operations // to make array elements same. function MinOperation(a, b, n) { // sorting both arrays // in ascending order a.sort(function(a, b){return a - b}); b.sort(function(a, b){return a - b}); // variable to store // the final result let 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 (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> Output2 Complexity Analysis: Time Complexity : O(n Log n)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Making elements of two arrays same with minimum increment/decrement S Surya Priy Follow Improve Article Tags : Greedy Sorting Technical Scripter Competitive Programming DSA Arrays +2 More Practice Tags : ArraysGreedySorting Similar Reads Minimum Increment / decrement to make array elements equal Given an array of integers where 1 \leq A[i] \leq 10^{18} . In one operation you can either Increment/Decrement any element by 1. The task is to find the minimum operations needed to be performed on the array elements to make all array elements equal. Examples: Input : A[] = { 1, 5, 7, 10 } Output : 7 min read Minimum cost to make Array equal by increment/decrementing elements Given an array, arr[], and the cost array cost[], the task is to find the minimum cost to make all the array elements equal by incrementing or decrementing the element at any index by 1. The cost of the increment or decrement operation for the element at index i is the cost[i]. Examples: Input: arr[ 8 min read Minimum increment/decrement to make array non-Increasing Given an array a, your task is to convert it into a non-increasing form such that we can either increment or decrement the array value by 1 in the minimum changes possible. Examples : Input : a[] = {3, 1, 2, 1}Output : 1Explanation : We can convert the array into 3 1 1 1 by changing 3rd element of a 11 min read Minimum increments or decrements by 1 required to make all array elements in AP Given an array arr[] consisting of N integers, the task is to find the minimum number of increment/decrement by 1 required to be performed on array elements to make all the elements of the given array arr[] in AP. If it is not possible to make the array in AP, then print "-1". Examples: Input: arr[] 11 min read Minimize decrements to make each Array elements same or 0 Given an array arr[] consisting of N positive integers. In one operation any number of the array can be decremented by 1. The task is to find the minimum number of operations required to make all of the elements equal or 0. Examples: Input: arr[] = {4, 1, 6, 6}Output: 5Explanation: Remove 1 from arr 5 min read Minimum increments to make the array non-decreasing Given an array A[] of size N along with an integer M such that 0 <= A[i] < M, the task is to output minimum number of operations required to sort A[] to non-decreasing order using following operation, choose an element let say A[i] and update it as (A[i] + 1) mod M. Examples: Input: N = 4, M = 7 min read Minimize moves to make Array elements equal by incrementing and decrementing pairs Given an array arr[] of size N, the task is to print the minimum number of moves needed to make all array elements equal by selecting any two distinct indices and then increment the element at the first selected index and decrement the element at the other selected index by 1 in each move. If it is 14 min read Minimize increment or increment and decrement of Pair to make all Array elements equal Given an array arr[] of size N, the task is to minimize the number of steps to make all the array elements equal by performing the following operations: Choose an element of the array and increase it by 1.Select two elements simultaneously (arr[i], arr[j]) increase arr[i] by 1 and decrease arr[j] by 8 min read Maximize the minimum array element by M subarray increments of size S Given an array arr[] of N integers and two integers S and M, the task is to maximize the minimum array element by incrementing any subarray of size S by 1, M number of times. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6}, S = 2, M = 3Output: 3Explanation:Below are the operations performed:Operation 1: 10 min read Like