📌  相关文章
📜  最小化使偶数和奇数数组元素的数量相等所需的增量

📅  最后修改于: 2021-05-14 07:14:05             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是找到要对数组元素执行的最小增量1 ,以使给定数组中的偶数和奇数整数的计数相等。如果不可能,则打印“ -1”

例子:

方法:解决给定问题的想法如下:

  • 如果N偶数,则遍历数组并保留奇数偶数整数的计数。偶数和奇数的整数除以2的绝对差给出了使偶数和奇数相等的最小增量操作。
  • 如果N奇数,则不可能使偶数和奇数相等,因此打印“ -1”

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find min operations
// to make even and odd count equal
int minimumIncrement(int arr[], int N)
{
    // Odd size will never make odd
    // and even counts equal
    if (N % 2 != 0) {
        cout << "-1";
        exit(0);
    }
 
    // Stores the count of even
    // numbers in the array arr[]
    int cntEven = 0;
 
    // Stores count of odd numbers
    // in the array arr[]
    int cntOdd = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        // If arr[i] is an
        // even number
        if (arr[i] % 2 == 0) {
 
            // Update cntEven
            cntEven += 1;
        }
    }
 
    // Odd numbers in arr[]
    cntOdd = N - cntEven;
 
    // Return absolute difference
    // divided by 2
    return abs(cntEven - cntOdd) / 2;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 3, 4, 9 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    cout << minimumIncrement(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
class GFG
{
     
// Function to find min operations
// to make even and odd count equal
static int minimumIncrement(int arr[], int N)
{
   
    // Odd size will never make odd
    // and even counts equal
    if (N % 2 != 0)
    {
        System.out.println( "-1");
        System.exit(0);
    }
 
    // Stores the count of even
    // numbers in the array arr[]
    int cntEven = 0;
 
    // Stores count of odd numbers
    // in the array arr[]
    int cntOdd = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++)
    {
 
        // If arr[i] is an
        // even number
        if (arr[i] % 2 == 0)
        {
 
            // Update cntEven
            cntEven += 1;
        }
    }
 
    // Odd numbers in arr[]
    cntOdd = N - cntEven;
 
    // Return absolute difference
    // divided by 2
    return Math.abs(cntEven - cntOdd) / 2;
}
   
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 3, 4, 9 };
    int N = arr.length;
 
    // Function call
    System.out.println(minimumIncrement(arr, N));
}
}
 
// This code is contributed by code_hunt.


Python3
# Python3 program for the above approach
 
# Function to find min operations
# to make even and odd count equal
def minimumIncrement(arr, N):
     
    # Odd size will never make odd
    # and even counts equal
    if (N % 2 != 0):
        print("-1")
        return
 
    # Stores the count of even
    # numbers in the array arr[]
    cntEven = 0
 
    # Stores count of odd numbers
    # in the array arr[]
    cntOdd = 0
 
    # Traverse the array arr[]
    for i in range(N):
 
        # If arr[i] is an
        # even number
        if (arr[i] % 2 == 0):
 
            # Update cntEven
            cntEven += 1
 
    # Odd numbers in arr[]
    cntOdd = N - cntEven
 
    # Return absolute difference
    # divided by 2
    return abs(cntEven - cntOdd) // 2
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 3, 4, 9]
    N = len(arr)
 
    # Function call
    print (minimumIncrement(arr, N))
 
    # Thiss code is contributed by mohit kumar 29.


C#
// C# program to implement
// the above approach
using System;
class GFG
{
   
// Function to find min operations
// to make even and odd count equal
static int minimumIncrement(int[] arr, int N)
{
   
    // Odd size will never make odd
    // and even counts equal
    if (N % 2 != 0)
    {
        Console.WriteLine( "-1");
        Environment.Exit(0);
    }
 
    // Stores the count of even
    // numbers in the array arr[]
    int cntEven = 0;
 
    // Stores count of odd numbers
    // in the array arr[]
    int cntOdd = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++)
    {
 
        // If arr[i] is an
        // even number
        if (arr[i] % 2 == 0)
        {
 
            // Update cntEven
            cntEven += 1;
        }
    }
 
    // Odd numbers in arr[]
    cntOdd = N - cntEven;
 
    // Return absolute difference
    // divided by 2
    return Math.Abs(cntEven - cntOdd) / 2;
}
 
  // Driver Code
  public static void  Main()
  {
    int[] arr = { 1, 3, 4, 9 };
    int N = arr.Length;
 
    // Function call
    Console.WriteLine(minimumIncrement(arr, N));
  }
}
 
// This code is contributed by susmitakundugoaldanga.


Javascript


输出:
1

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