📌  相关文章
📜  即使仅删除一个元素也可以使数组求和的方法数量

📅  最后修改于: 2021-05-17 23:15:45             🧑  作者: Mango

给定一个数组arr []正整数,即使我们只允许删除一个元素,任务也是要找到多种转换数组和的方法。
例子:

方法:对以上问题陈述的主要观察是:

  1. 如果我们有奇数个奇数元素,那么总和总是奇数,那么我们必须从数组arr []中删除一个奇数,以使总和为偶数。由于我们必须删除一个元素,因此,使总和为偶数的总数就是数组arr []中奇数元素数量
  2. 如果我们有偶数个奇数元素,那么总和总是偶数。由于我们必须删除一个元素才能使和成为偶数,因此,使和为偶数的总数就是数组arr []中偶数元素数量。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
// Function to find a number of ways
// to make array element sum even by
// removing one element
int find_num_of_ways(int arr[], int N)
{
    int count_even = 0, count_odd = 0;
 
    // Finding the count of even
    // and odd elements
    for (int i = 0; i < N; i++) {
        if (arr[i] % 2) {
            count_odd++;
        }
        else {
            count_even++;
        }
    }
 
    // If count_odd is odd then
    // no. of ways is count_odd
    if (count_odd % 2) {
        return count_odd;
    }
 
    // Else no. of ways is count_even
    else {
        return count_even;
    }
}
 
// Driver Code
int main()
{
 
    // Given array arr[]
    int arr[] = { 1, 3, 3, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << find_num_of_ways(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to find a number of ways
// to make array element sum even by
// removing one element
static int find_num_of_ways(int arr[], int N)
{
    int count_even = 0, count_odd = 0;
 
    // Finding the count of even
    // and odd elements
    for(int i = 0; i < N; i++)
    {
        if (arr[i] % 2 == 1)
        {
            count_odd++;
        }
        else
        {
            count_even++;
        }
    }
 
    // If count_odd is odd then
    // no. of ways is count_odd
    if (count_odd % 2 == 1)
    {
        return count_odd;
    }
 
    // Else no. of ways is count_even
    else
    {
        return count_even;
    }
}
 
// Driver Code
public static void main (String[] args)
{
 
    // Given array arr[]
    int arr[] = { 1, 3, 3, 2 };
    int N = 4;
 
    // Function call
    System.out.print(find_num_of_ways(arr, N));
}
}
 
// This code is contributed by Ritik Bansal


Python3
# Python3 program for the above approach
 
# Function to find a number of ways
# to make array element sum even by
# removing one element
def find_num_of_ways(arr, N):
 
    count_even = 0
    count_odd = 0
 
    # Finding the count of even
    # and odd elements
    for i in range(N):
        if (arr[i] % 2):
            count_odd += 1
        else:
            count_even += 1
 
    # If count_odd is odd then
    # no. of ways is count_odd
    if (count_odd % 2):
        return count_odd
 
    # Else no. of ways is count_even
    else:
        return count_even
 
# Driver Code
if __name__ == '__main__':
     
    # Given array arr[]
    arr = [ 1, 3, 3, 2 ]
    N = len(arr)
 
    # Function call
    print(find_num_of_ways(arr, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG{
      
// Function to find a number of ways
// to make array element sum even by
// removing one element
static int find_num_of_ways(int []arr, int N)
{
    int count_even = 0, count_odd = 0;
  
    // Finding the count of even
    // and odd elements
    for(int i = 0; i < N; i++)
    {
        if (arr[i] % 2 == 1)
        {
            count_odd++;
        }
        else
        {
            count_even++;
        }
    }
  
    // If count_odd is odd then
    // no. of ways is count_odd
    if (count_odd % 2 == 1)
    {
        return count_odd;
    }
  
    // Else no. of ways is count_even
    else
    {
        return count_even;
    }
}
  
// Driver Code
public static void Main(string[] args)
{
  
    // Given array arr[]
    int []arr = { 1, 3, 3, 2 };
    int N = 4;
  
    // Function call
    Console.Write(find_num_of_ways(arr, N));
}
}
 
// This code is contributed by Rutvik


Javascript


输出:
3

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