📌  相关文章
📜  最小化加奇减偶数使所有数组元素等于K

📅  最后修改于: 2021-10-26 06:49:33             🧑  作者: Mango

给定一个大小为N的数组arr[]和一个整数K ,任务是通过执行以下操作任意次数来找到使所有数组元素等于K所需的最小操作次数:

  • arr[i]转换为arr[i] + X ,其中X是奇数。
  • arr[i]转换为arr[i] – Y ,其中Y是偶数。

例子:

方法:该问题可以使用贪心技术解决。以下是观察结果:

请按照以下步骤解决问题:

  • 遍历给定数组并检查以下条件。
    • 如果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)