📜  将最小数添加到数组,以使总和成为偶数

📅  最后修改于: 2021-04-23 05:41:09             🧑  作者: Mango

给定一个数组,编写一个程序,将最小数量(应大于0 )添加到数组中,以使数组的总和变为偶数。

例子:

Input : 1 2 3 4 5 6 7 8
Output : 2
Explanation : Sum of array is 36, so we 
add minimum number 2 to make the sum even.

Input : 1 2 3 4 5 6 7 8 9
Output : 1

方法1(计算总和)。我们计算数组中所有元素的总和,然后可以检查总和是否等于最小数为2,否则最小数为1。如果总和超过允许的限制,则此方法可能导致溢出。

方法2。我们不计算数量之和,而是保留数组中元素的奇数个数。如果存在的奇数计数为偶数,则返回2,否则返回1。
例如–数组包含:1 2 3 4 5 6 7
奇数计数为4。我们知道奇数的偶数之和为偶数。而且偶数之和始终是偶数(这就是为什么我们不保留偶数的计数)。

C++
// CPP program to add minimum number
// so that the sum of array becomes even
#include 
using namespace std;
  
// Function to find out minimum number
int minNum(int arr[], int n)
{
    // Count odd number of terms in array
    int odd = 0;
    for (int i = 0; i < n; i++) 
        if (arr[i] % 2)
            odd += 1;
      
   return (odd % 2)? 1 : 2;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << minNum(arr, n) << "n";
  
    return 0;
}


Java
// Java program to add minimum number
// so that the sum of array becomes even
  
class GFG
{
    // Function to find out minimum number
    static int minNum(int arr[], int n)
    {
        // Count odd number of terms in array
        int odd = 0;
        for (int i = 0; i < n; i++)
            if (arr[i] % 2 != 0)
                odd += 1;
  
        return ((odd % 2) != 0)? 1 : 2;
    }
  
    // Driver method to test above function
    public static void main(String args[])
    {
        int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int n = arr.length;
  
        System.out.println(minNum(arr, n));
    }
}


Python
# Python program to add minimum number
# so that the sum of array becomes even
  
# Function to find out minimum number
def minNum(arr, n):
  
    # Count odd number of terms in array
    odd = 0
    for i in range(n):
        if (arr[i] % 2):
            odd += 1
      
    if (odd % 2):
        return 1
    return 2
  
  
# Driver code
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
n = len(arr)
print minNum(arr, n)


C#
// C# program to add minimum number
// so that the sum of array becomes even
using System;
  
class GFG
{
    // Function to find out minimum number
    static int minNum(int []arr, int n)
    {
        // Count odd number of terms in array
        int odd = 0;
        for (int i = 0; i < n; i++)
            if (arr[i] % 2 != 0)
                odd += 1;
  
        return ((odd % 2) != 0)? 1 : 2;
    }
  
    // Driver Code
    public static void Main()
    {
        int []arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int n = arr.Length;
  
        Console.Write(minNum(arr, n));
    }
}
  
// This code is contributed by Nitin Mittal.


PHP


C++
// CPP program to add minimum number
// so that the sum of array becomes even
  
#include 
using namespace std;
  
// Function to find out minimum number
int minNum(int arr[], int n)
{
    // Count odd number of terms in array
    bool odd = 0;
    for (int i = 0; i < n; i++) 
        if (arr[i] % 2)
            odd = !odd;
      
    if (odd)
        return 1;
    return 2;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << minNum(arr, n) << "n";
  
    return 0;
}


Java
// Java program to add minimum number
// so that the sum of array becomes even
  
class GFG
{
    // Function to find out minimum number
    static int minNum(int arr[], int n)
    {
        // Count odd number of terms in array
        Boolean odd = false;
        for (int i = 0; i < n; i++)
            if (arr[i] % 2 != 0)
                odd = !odd;
  
        if (odd)
            return 1;
        return 2;
    }
  
    //Driver method to test above function
    public static void main(String args[])
    {
        int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int n = arr.length;
  
        System.out.println(minNum(arr, n));
    }
}


Python
# Python program to add minimum number
# so that the sum of array becomes even
  
# Function to find out minimum number
def minNum(arr, n):
  
    # Count odd number of terms in array
    odd = False
    for i in range(n):
        if (arr[i] % 2):
            odd = not odd
    if (odd):
        return 1
    return 2
  
  
# Driver code
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
n = len(arr)
print minNum(arr, n)


C#
// C# program to add minimum number
// so that the sum of array becomes even
using System;
  
class GFG
{
      
    // Function to find out minimum number
    static int minNum(int []arr, int n)
    {
        // Count odd number of terms in array
        bool odd = false;
        for (int i = 0; i < n; i++)
            if (arr[i] % 2 != 0)
                odd = !odd;
  
        if (odd)
            return 1;
        return 2;
    }
  
    //Driver Code
    public static void Main()
    {
        int []arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int n = arr.Length;
  
        Console.Write(minNum(arr, n));
    }
}
  
// This code is contributed by Nitin Mittal.


PHP


输出:

1

方法3。我们还可以改进2方法,我们不需要保留存在的奇数个元素的数量。我们可以使用一个布尔变量(初始化为0)。每当我们在数组中找到奇数元素时,我们都会对布尔变量执行NOT(!)操作。此逻辑运算符将布尔变量的值取反(意味着如果它为0,则将变量转换为1,反之亦然)。
例如–数组包含:1 2 3 4 5
说明:变量初始化为0。
遍历数组
1是奇数,在变量中应用NOT运算,现在变量变为1。
2是偶数,无操作。
3是奇数,在变量中应用NOT运算,现在变量变为0。
4是偶数,无操作。
5是奇数,对变量应用NOT运算,现在变量变为1。

如果变量值为1,则表示存在奇数个奇数元素,使元素之和等于的最小数加1。
其他最小数字为2。

C++

// CPP program to add minimum number
// so that the sum of array becomes even
  
#include 
using namespace std;
  
// Function to find out minimum number
int minNum(int arr[], int n)
{
    // Count odd number of terms in array
    bool odd = 0;
    for (int i = 0; i < n; i++) 
        if (arr[i] % 2)
            odd = !odd;
      
    if (odd)
        return 1;
    return 2;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << minNum(arr, n) << "n";
  
    return 0;
}

Java

// Java program to add minimum number
// so that the sum of array becomes even
  
class GFG
{
    // Function to find out minimum number
    static int minNum(int arr[], int n)
    {
        // Count odd number of terms in array
        Boolean odd = false;
        for (int i = 0; i < n; i++)
            if (arr[i] % 2 != 0)
                odd = !odd;
  
        if (odd)
            return 1;
        return 2;
    }
  
    //Driver method to test above function
    public static void main(String args[])
    {
        int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int n = arr.length;
  
        System.out.println(minNum(arr, n));
    }
}

Python

# Python program to add minimum number
# so that the sum of array becomes even
  
# Function to find out minimum number
def minNum(arr, n):
  
    # Count odd number of terms in array
    odd = False
    for i in range(n):
        if (arr[i] % 2):
            odd = not odd
    if (odd):
        return 1
    return 2
  
  
# Driver code
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
n = len(arr)
print minNum(arr, n)

C#

// C# program to add minimum number
// so that the sum of array becomes even
using System;
  
class GFG
{
      
    // Function to find out minimum number
    static int minNum(int []arr, int n)
    {
        // Count odd number of terms in array
        bool odd = false;
        for (int i = 0; i < n; i++)
            if (arr[i] % 2 != 0)
                odd = !odd;
  
        if (odd)
            return 1;
        return 2;
    }
  
    //Driver Code
    public static void Main()
    {
        int []arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int n = arr.Length;
  
        Console.Write(minNum(arr, n));
    }
}
  
// This code is contributed by Nitin Mittal.

的PHP


输出 :

1

锻炼 :
找到使元素总数为奇数所需的最小数量。