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:
- Find the maximum number of elements divisible by 3
- Minimum elements to be added in a range so that count of elements is divisible by K
- Find set of m-elements with difference of any two elements is divisible by k
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Maximum positive integer divisible by C and is in the range [A, B]
- Possible cuts of a number such that maximum parts are divisible by 3
- Sum of all the elements in an array divisible by a given number K
- Print array elements that are divisible by at-least one other
- Minimum and Maximum element of an array which is divisible by a given number k
- Find an array element such that all elements are divisible by it
- Count the number of elements in an array which are divisible by k
- Product of all the elements in an array divisible by a given number K
- Maximum distinct elements after removing k elements
- Maximum and Minimum element of a linked list which is divisible by a given number k
- Smallest perfect square divisible by all elements of 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 email@example.com. 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.