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

📅  最后修改于: 2021-06-26 16:39:27             🧑  作者: Mango

给定大小为N且整数K的数组arr [] ,任务是通过多次执行以下运算,找到使所有数组元素等于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]中减去一个偶数(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现场课程美国》。