给定大小为N且整数K的数组arr [] ,任务是通过多次执行以下运算,找到使所有数组元素等于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]中减去一个偶数(Y) 。因此,总共需要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)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。