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.
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].
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:
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.