📌  相关文章
📜  使数组总和均匀的最小移除量

📅  最后修改于: 2022-05-13 01:57:47.122000             🧑  作者: Mango

使数组总和均匀的最小移除量

给定一个包含 N 个整数的数组 Arr[]。我们需要编写一个程序来找到需要从数组中删除的最小元素数,以便剩余元素的总和是偶数。
例子:

Input : {1, 2, 3, 4}
Output : 0
Sum is already even

Input : {4, 2, 3, 4}
Output : 1
We need to remove 3 to make
sum even.

解决这个问题的想法是首先回顾 ODD 和 EVEN 的以下属性:

  • 奇数 + 奇数 = 偶数
  • 奇数 + 偶数 = 奇数
  • 偶数+偶数=偶数
  • 奇数 * 偶数 = 偶数
  • 偶数 * 偶数 = 偶数
  • 奇数 * 奇数 = 奇数

因此,即使需要,我们也只需从数组中删除一些元素,即可求出数组元素的总和。我们可以注意到,任何偶数的总和总是偶数。但是奇数的奇数和是奇数。也就是说,3 + 3 + 3 = 9 这是奇数,但 3 + 3 + 3 + 3 = 12 是偶数。所以,我们只需要计算数组中奇数元素的数量。如果数组中奇数元素的计数是偶数,那么我们不需要从数组中删除任何元素,但如果数组中奇数元素的计数是奇数,则通过从数组中删除任何一个奇数元素,总和的数组将变得均匀。
下面是上述想法的实现:

C++
// CPP program to find minimum number of
// elements to be removed to make the sum
// even
 
#include 
using namespace std;
 
int findCount(int arr[], int n)
{
    int count = 0;
 
    for (int i = 0; i < n; i++)
        if (arr[i] % 2 == 1)
            count++; /* counts only odd numbers */
             
    /* if the counter is even return 0
       otherwise return 1 */
    if (count % 2 == 0)
        return 0;
    else
        return 1;
}
 
// Driver Code
int main()
{
    int arr[] = {1, 2, 4, 5, 1};
    int n = sizeof(arr)/sizeof(arr[0]);
    
    cout <


Java
// Java program to find minimum number of
// elements to be removed to make the sum
// even
class GFG {
     
    static int findCount(int arr[], int n)
    {
        int count = 0;
     
        for (int i = 0; i < n; i++)
            if (arr[i] % 2 == 1)
             
                /* counts only odd numbers */
                count++;
                 
        /* if the counter is even return 0
        otherwise return 1 */
        if (count % 2 == 0)
            return 0;
        else
            return 1;
    }
     
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = {1, 2, 4, 5, 1};
        int n = arr.length;
         
        System.out.println(findCount(arr, n));
    }
}
 
// This code is contribute by Smitha Dinesh Semwal


Python 3
# program to find minimum number
# of elements to be removed to
# make the sum even
 
def findCount(arr, n):
 
    count = 0
 
    for i in range(0, n):
        if (arr[i] % 2 == 1):
             
            # counts only odd
            # numbers
            count += -1
             
    # if the counter is
    # even return 0
    # otherwise return 1
    if (count % 2 == 0):
        return 0
    else:
        return 1
 
# Driver Code
arr = [1, 2, 4, 5, 1]
n = len(arr)
 
print(findCount(arr, n))
 
# This code is contributed by
# Smitha Dinesh Semwal


C#
// C# program to find minimum number of
// elements to be removed to make the sum
// even
using System;
 
public class GFG{
     
    static int findCount(int[] arr, int n)
    {
        int count = 0;
     
        for (int i = 0; i < n; i++)
            if (arr[i] % 2 == 1)
             
                /* counts only odd numbers */
                count++;
                 
        /* if the counter is even return 0
        otherwise return 1 */
        if (count % 2 == 0)
            return 0;
        else
            return 1;
    }
     
    // Driver code
    static public void Main ()
    {
        int[] arr = {1, 2, 4, 5, 1};
        int n = arr.Length;
         
        Console.WriteLine(findCount(arr, n));
    }
}
 
// This code is contributed by Ajit.


PHP


Javascript


输出:

1

时间复杂度:O(n),其中 n 是数组中元素的数量。