给定两个相同大小的数组,我们需要以最少的操作将第一个数组转换为另一个数组。在一个操作中,我们可以将一个元素加一或减一。请注意,元素的出现顺序不必相同。
在这里要将一个数字转换为另一个数字,我们可以从中加或减 1。
例子 :
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
算法 :
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
?>
Javascript
输出 :
2
时间复杂度: O(n Log n)