📌  相关文章
📜  可被2整除的最大可能元素

📅  最后修改于: 2021-05-04 22:07:43             🧑  作者: Mango

给定一个大小为N的整数数组arr 。任务是在修改数组后找到可以被2整除的数组中最大可能的元素。一个人可以执行下面的操作任意次(可能为零次)。

例子:

方法
首先,观察到我们不需要修改可被2整除的元素(即,偶数)。然后我们离开了奇数。两个数字相加将得到一个偶数,该偶数可被2整除。

所以最后,结果将是:

下面是上述方法的实现:

CPP
// CPP program to find maximum possible
// elements which divisible by 2
#include 
using namespace std;
  
// Function to find maximum possible
// elements which divisible by 2
int Divisible(int arr[], int n)
{
    // To store count of even numbers
    int count_even = 0;
  
    for (int i = 0; i < n; i++)
        if (arr[i] % 2 == 0)
            count_even++;
  
    // All even numbers and half of odd numbers
    return count_even + (n - count_even) / 2;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Function call
    cout << Divisible(arr, n);
  
    return 0;
}


Java
// Java program to find maximum possible 
// elements which divisible by 2 
class GFG 
{
  
    // Function to find maximum possible 
    // elements which divisible by 2 
    static int Divisible(int arr[], int n) 
    { 
        // To store count of even numbers 
        int count_even = 0; 
      
        for (int i = 0; i < n; i++) 
            if (arr[i] % 2 == 0) 
                count_even++; 
      
        // All even numbers and half of odd numbers 
        return count_even + (n - count_even) / 2; 
    } 
      
    // Driver code 
    public static void main (String[] args) 
    {
        int arr[] = { 1, 2, 3, 4, 5 }; 
      
        int n = arr.length; 
      
        // Function call 
        System.out.println(Divisible(arr, n)); 
    }
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 program to find maximum possible
# elements which divisible by 2
  
# Function to find maximum possible
# elements which divisible by 2
def Divisible(arr, n):
    # To store count of even numbers
    count_even = 0
  
    for i in range(n):
        if (arr[i] % 2 == 0):
            count_even+=1
  
    # All even numbers and half of odd numbers
    return count_even + (n - count_even) // 2
  
# Driver code
  
arr=[1, 2, 3, 4, 5]
  
n = len(arr)
  
# Function call
print(Divisible(arr, n))
  
# This code is contribute by mohit kumar 29


C#
// C# program to find maximum possible 
// elements which divisible by 2 
using System;
  
class GFG
{
      
    // Function to find maximum possible 
    // elements which divisible by 2 
    static int Divisible(int []arr, int n) 
    { 
        // To store count of even numbers 
        int count_even = 0; 
      
        for (int i = 0; i < n; i++) 
            if (arr[i] % 2 == 0) 
                count_even++; 
      
        // All even numbers and half of odd numbers 
        return count_even + (n - count_even) / 2; 
    } 
      
    // Driver code 
    static public void Main ()
    {
          
        int []arr = { 1, 2, 3, 4, 5 }; 
        int n = arr.Length; 
          
        // Function call 
        Console.Write(Divisible(arr, n)); 
    }
}
  
// This code is contributed by ajit.


输出:
3