📌  相关文章
📜  最小化在所有成对的数组元素之间产生差异所需的增量

📅  最后修改于: 2021-04-17 18:56:14             🧑  作者: Mango

给定一个数组, arr []N个整数组成,任务是最小化使所有差值对偶数成对所需的数组元素的增量数量。

例子:

方法:可以通过观察以下事实来解决给定的问题为了使所有成对的数组元素之间的差异均匀,两个元素必须具有相同的奇偶性。因此,我们的想法是将所有数组元素都转换为偶数奇数。最小增量数等于偶数和奇数数组元素的计数的最小值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum increments
// required to difference between all
// pairs of array elements even
void minimumOperations(int arr[], int N)
{
    // Store the count of odd
    // and even numbers
    int oddCnt = 0, evenCnt = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        if (arr[i] % 2 == 0) {
 
            // Increment evenCnt by 1
            evenCnt++;
        }
        else {
 
            // Increment eveCnt by 1
            oddCnt++;
        }
    }
 
    // Print the minimum of oddCnt
    // and eveCnt
    cout << min(oddCnt, evenCnt);
}
 
// Driver Code
int main()
{
    int arr[] = { 4, 1, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
    minimumOperations(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
public class GFG
{
 
  // Function to find the minimum increments
  // required to difference between all
  // pairs of array elements even
  static void minimumOperations(int[] arr, int N)
  {
 
    // Store the count of odd
    // and even numbers
    int oddCnt = 0, evenCnt = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
      if (arr[i] % 2 == 0)
      {
 
        // Increment evenCnt by 1
        evenCnt++;
      }
      else
      {
 
        // Increment oddCnt by 1
        oddCnt++;
      }
    }
 
    // Print the minimum of oddCnt
    // and eveCnt
    System.out.print(Math.min(oddCnt, evenCnt));
  }  
 
  // Driver code
  public static void main(String args[])
  {
    int[] arr = { 4, 1, 2 };
    int N = arr.length;
    minimumOperations(arr, N);
  }
}
 
// This code is contributed by AnkThon


Python3
# Python program for the above approach
 
# Function to find the minimum increments
# required to difference between all
# pairs of array elements even
def minimumOperations(arr, N) :
     
    # Store the count of odd
    # and even numbers
    oddCnt = 0
    evenCnt = 0
 
    # Traverse the array
    for i in range(N):
        if (arr[i] % 2 == 0) :
 
            # Increment evenCnt by 1
            evenCnt += 1
         
        else :
 
            # Increment eveCnt by 1
            oddCnt += 1
         
    # Prthe minimum of oddCnt
    # and eveCnt
    print(min(oddCnt, evenCnt))
 
# Driver Code
arr = [ 4, 1, 2 ]
N = len(arr)
minimumOperations(arr, N)
 
# This code is contributed by sanjoy_62.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
     
    // Function to find the minimum increments
    // required to difference between all
    // pairs of array elements even
    static void minimumOperations(int[] arr, int N)
    {
       
        // Store the count of odd
        // and even numbers
        int oddCnt = 0, evenCnt = 0;
       
        // Traverse the array
        for (int i = 0; i < N; i++)
        {
            if (arr[i] % 2 == 0)
            {
       
                // Increment evenCnt by 1
                evenCnt++;
            }
            else
            {
       
                // Increment eveCnt by 1
                oddCnt++;
            }
        }
       
        // Print the minimum of oddCnt
        // and eveCnt
        Console.Write(Math.Min(oddCnt, evenCnt));
    }  
 
  // Driver code
  static void Main()
  {
    int[] arr = { 4, 1, 2 };
    int N = arr.Length;
    minimumOperations(arr, N);
  }
}
 
// This code is contributed by divyeshrabadiya07.


输出:
1

时间复杂度: O(N)
辅助空间: O(1)