![]() |
VOOZH | about |
Given an array Arr[] of N integers and an integer K, the task is to find if it is possible to make the count of even and odd elements equal by performing the following operations at most K times:
Examples:
Input: Arr[] = {1, 4, 8, 12}, K = 2
Output: Yes
Explanation: Count of Odd = 1, Count of Even = 3.
If we half 4 twice then 4 becomes 1 or if we half 12 twice then it becomes 3.
It is possible to make even and odd count equal by performing 2 operations.Input: Arr[] = {1, 2, 3, 4}, K = 0
Output: Yes
Approach: The idea to solve this problem is as follows:
Find the count of even and odd elements (say expressed as CE and CO respectively).
The number of elements needed to be modified = abs(CE - CO)/2.
An even character needed to be halved i times if its right most bit is at (i+1)th position from the right. And an odd element can be made even by multiplying it by 2 in a single operation.Use this criteria to find the number of operations required and if it is at most K or not.
Follow the below steps to solve the problem:
Below is the implementation of the above approach.
Yes
Time Complexity: O(N)
Auxiliary Space: O(1)