Given an integer array **arr** of size **N**. The task is to find the maximum possible elements in the array which are divisible by 2 after modifying the array. One can perform below operation an arbitrary number of times(possibly zero times).

Replace any two elements in the array with their sum.

**Examples:**

Input :arr = [1, 2, 3, 1, 3]

Output :3

After adding elements at index 0 and 2, and index 3 and 4, array becomes arr=[4, 2, 4].

Input :arr = [1, 2, 3, 4, 5]

Output :3

After adding 1 and 3, array becomes arr=[4, 2, 4, 5].

**Approach **:

First, observation is that we don’t need to modify elements which are divisible by 2(i.e., even numbers). Then we left with odd numbers. Addition of two numbers will give an even number which is divisible by 2.

So finally, the result will be:

count_even+count_odd/2.

Below is the implementation of the above approach:

## CPP

`// CPP program to find maximum possible ` `// elements which divisible by 2 ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

**Output:**

3

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find permutation of n which is divisible by 3 but not divisible by 6
- Possible cuts of a number such that maximum parts are divisible by 3
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Maximum and Minimum element of a linked list which is divisible by a given number k
- Minimum and Maximum element of an array which is divisible by a given number k
- Count the number of elements in an array which are divisible by k
- Count of elements on the left which are divisible by current element
- Count of elements on the left which are divisible by current element | Set 2
- Count of pairs of Array elements which are divisible by K when concatenated
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Find the element before which all the elements are smaller than it, and after which all are greater
- Split an array into groups of 3 such that X3 is divisible by X2 and X2 is divisible by X1
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Find the maximum number of elements divisible by 3
- Maximum count of elements divisible on the left for any element
- Maximum sum of elements divisible by K from the given array
- Find set of m-elements with difference of any two elements is divisible by k
- Minimum elements to be added in a range so that count of elements is divisible by K
- Count of elements not divisible by any other elements of Array
- Possible to make a divisible by 3 number using all digits in an array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.