给定一个大小为N的数组arr[]和一个整数K ,任务是通过执行以下操作任意次数来找到使所有数组元素等于K所需的最小操作次数:
- 将arr[i]转换为arr[i] + X ,其中X是奇数。
- 将arr[i]转换为arr[i] – Y ,其中Y是偶数。
例子:
Input: arr[] = {8, 7, 2, 1, 3}, K = 5
Output: 8
Explanation: To make all elements of the given array equal to K(= 5), following operations are required:
arr[0] = arr[0] + X, X = 1
arr[0] = arr[0] – Y, Y = 4
arr[1] = arr[1] – Y, Y = 2
arr[2] = arr[2] + X, X = 3
arr[3] = arr[3] + X, X = 3
arr[3] = arr[3] + X, X = 1
arr[4] = arr[4] + X, X = 1
arr[4] = arr[4] + X, X = 1
Input: arr[] = {1, 2, 3, 4, 5, 6, 7}, K = 3
Output: 9
方法:该问题可以使用贪心技术解决。以下是观察结果:
Even + Even = Even
Even + Odd = Odd
Odd + Odd = Even
Odd + Even = Odd
请按照以下步骤解决问题:
- 遍历给定数组并检查以下条件。
- 如果K > arr[i] 且 (K – arr[i]) % 2 == 0则将两个奇数(X) 添加到arr[i] 中。因此,总共需要2 个操作。
- 如果K > arr[i] 且 (K – arr[i]) % 2 != 0则将一个奇数(X) 添加到arr[i] 中。因此,总共需要1 个操作。
- 如果K < arr[i] 并且 (arr[i] – arr[i]) % 2 == 0则将一个偶数(Y)减去一个偶数(Y)到arr[i] 中。因此,总共需要1 个操作。
- 如果k
则增加一个奇数(X)转换成ARR [i]和从ARR [I]中减去一个偶数(Y)。因此,总共需要2 个操作。
- 最后,打印使所有数组元素等于K所需的操作总数。
下面是上述方法的实现
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the minimum operations
// required to make array elements equal to K
int MinOperation(int arr[], int N, int K)
{
// Stores minimum count of operations
int cntOpe = 0;
// Traverse the given array
for (int i = 0; i < N; i++) {
// If K is greater than arr[i]
if (K > arr[i]) {
// If (K - arr[i]) is even
if ((K - arr[i]) % 2 == 0) {
// Update cntOpe
cntOpe += 2;
}
else {
// Update cntOpe
cntOpe += 1;
}
}
// If K is less than arr[i]
else if (K < arr[i]) {
// If (arr[i] - K) is even
if ((K - arr[i]) % 2 == 0) {
// Update cntOpe
cntOpe += 1;
}
else {
// Update cntOpe
cntOpe += 2;
}
}
}
return cntOpe;
}
// Driver Code
int main()
{
int arr[] = { 8, 7, 2, 1, 3 };
int K = 5;
int N = sizeof(arr) / sizeof(arr[0]);
cout << MinOperation(arr, N, K);
return 0;
}
Java
// Java program to implement
// the above approach
class GFG{
// Function to find the minimum
// operations required to make
// array elements equal to K
public static int MinOperation(int arr[],
int N, int K)
{
// Stores minimum count of
// operations
int cntOpe = 0;
// Traverse the given array
for (int i = 0; i < N; i++)
{
// If K is greater than
// arr[i]
if (K > arr[i])
{
// If (K - arr[i]) is even
if ((K - arr[i]) % 2 == 0)
{
// Update cntOpe
cntOpe += 2;
}
else
{
// Update cntOpe
cntOpe += 1;
}
}
// If K is less than
// arr[i]
else if (K < arr[i])
{
// If (arr[i] - K) is
// even
if ((K - arr[i]) % 2 == 0)
{
// Update cntOpe
cntOpe += 1;
}
else
{
// Update cntOpe
cntOpe += 2;
}
}
}
return cntOpe;
}
// Driver code
public static void main(String[] args)
{
int arr[] = {8, 7, 2, 1, 3};
int K = 5;
int N = arr.length;
System.out.println(
MinOperation(arr, N, K));
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python3 program to implement
# the above approach
# Function to find the minimum operations
# required to make array elements equal to K
def MinOperation(arr, N, K):
# Stores minimum count of operations
cntOpe = 0
# Traverse the given array
for i in range(N):
# If K is greater than arr[i]
if (K > arr[i]):
# If (K - arr[i]) is even
if ((K - arr[i]) % 2 == 0):
# Update cntOpe
cntOpe += 2
else:
# Update cntOpe
cntOpe += 1
# If K is less than arr[i]
elif (K < arr[i]):
# If (arr[i] - K) is even
if ((K - arr[i]) % 2 == 0):
# Update cntOpe
cntOpe += 1
else:
# Update cntOpe
cntOpe += 2
return cntOpe
# Driver Code
arr = [ 8, 7, 2, 1, 3 ]
K = 5
N = len(arr)
print(MinOperation(arr, N, K))
# This code is contributed by sanjoy_62
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to find the minimum
// operations required to make
// array elements equal to K
public static int MinOperation(int []arr,
int N, int K)
{
// Stores minimum count of
// operations
int cntOpe = 0;
// Traverse the given array
for(int i = 0; i < N; i++)
{
// If K is greater than
// arr[i]
if (K > arr[i])
{
// If (K - arr[i]) is even
if ((K - arr[i]) % 2 == 0)
{
// Update cntOpe
cntOpe += 2;
}
else
{
// Update cntOpe
cntOpe += 1;
}
}
// If K is less than
// arr[i]
else if (K < arr[i])
{
// If (arr[i] - K) is
// even
if ((K - arr[i]) % 2 == 0)
{
// Update cntOpe
cntOpe += 1;
}
else
{
// Update cntOpe
cntOpe += 2;
}
}
}
return cntOpe;
}
// Driver code
public static void Main(String[] args)
{
int []arr = {8, 7, 2, 1, 3};
int K = 5;
int N = arr.Length;
Console.WriteLine(
MinOperation(arr, N, K));
}
}
// This code is contributed by Amit Katiyar
Javascript
输出:
8
时间复杂度: O(N)
辅助空间: O(1)