📌  相关文章
📜  使两个数组的元素相同,最小增减

📅  最后修改于: 2021-04-27 21:16:12             🧑  作者: Mango

给定两个相同大小的数组,我们需要使用最少的操作将第一个数组转换为另一个数组。在一个操作中,我们可以将元素增加或减少一个。注意,元素的出现顺序不必相同。

在这里,要将一个数字转换为另一个数字,我们可以对其加减1。

例子 :

算法 :
1.首先对两个数组进行排序。
2.排序后,我们将运行一个循环,在该循环中我们比较第一个和第二个数组元素,并计算使第一个数组等于第二个数组所需的所需操作。

下面是上述方法的实现

C++
// CPP program to find minimum increment/decrement
// operations to make array elements same.
#include 
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
 $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
?>


输出 :
2

时间复杂度: O(n Log n)