Minimize division by 2 to make at least K even numbers in given Array
Given an array arr[] having positive integers of size N, and a non-negative integer K, the task is to find the minimum number of division by 2 operations to be performed such that the array has at least K even numbers.
Examples:
Input: arr = [2, 7, 4, 5, 6], K = 4
Output: 1
Explanation: Replace element at index 3 by its half.
So array becomes [2, 3, 4, 2, 6] which contains atleast K(i.e 4) even numbers.
Input: arr = [2, 7, 4, 5, 6], K = 6
Output: -1
Explanation: Array can contain atmost 5 even numbers after performing given operation.
Approach: To solve the problem use the following idea:
Any odd number can be written in the form of (4*x + 1) or (4*x + 3). For the odd numbers of form (4*x + 1) dividing once give an even number and for (4*x + 3) we need to half it twice to get an even number.
So based on this get count of even numbers and odd numbers of these two formats. Now to optimally half elements, first change odd numbers of type (4*x + 1) to an even number and then of the type (4*x + 3).
Follow the steps to solve the given problem:
- Declare and initialize variables evenCount with 0 to count the number of even integers present in the original array.
- Initialize two other variables odd1 to store the odd numbers of type (4*x + 1).
- If evenCount is greater than or equal to K print 0 as the array already contains at least K even numbers.
- Else if the value of K is greater than the size of arr print -1.
- Otherwise, do the following:
- If (K – evenCount) is less than or equal to odd1, return this value as the required answer.
- Else return odd1 + (K – evenCount – odd1)*2 as the required answer [as (K-evenCount-odd1) odd elements are of type (4*x + 3)].
Below is the implementation for the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int minOperations( int arr[], int n, int k)
{
if (k > n)
return -1;
int evenCount = 0, odd1 = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 == 0)
evenCount++;
else if (arr[i] % 4 == 1)
odd1++;
}
if (evenCount >= k)
return 0;
if (k - evenCount <= odd1)
return k - evenCount;
return odd1 + (k - evenCount - odd1) * 2;
}
int main()
{
int arr[] = { 2, 3, 4, 5, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 4;
cout << minOperations(arr, N, K);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int minOperations( int [] arr, int n, int k)
{
if (k > n) {
return - 1 ;
}
int evenCount = 0 , odd1 = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] % 2 == 0 ) {
evenCount++;
}
else if (arr[i] % 4 == 1 ) {
odd1++;
}
}
if (evenCount >= k) {
return 0 ;
}
if (k - evenCount <= odd1) {
return k - evenCount;
}
return odd1 + (k - evenCount - odd1) * 2 ;
}
public static void main(String[] args)
{
int [] arr = { 2 , 3 , 4 , 5 , 6 };
int N = arr.length;
int K = 4 ;
System.out.print(minOperations(arr, N, K));
}
}
|
Python3
def minOperations(arr, n, k):
if (k > n):
return - 1
evenCount = 0
odd1 = 0
for i in range ( 0 , n):
if (arr[i] % 2 = = 0 ):
evenCount + = 1
elif (arr[i] % 4 = = 1 ):
odd1 + = 1
if (evenCount > = k):
return 0
if (k - evenCount < = odd1):
return k - evenCount
return odd1 + (k - evenCount - odd1) * 2
arr = [ 2 , 3 , 4 , 5 , 6 ]
N = len (arr)
K = 4
print (minOperations(arr, N, K))
|
C#
using System;
public class GFG{
public static int minOperations( int [] arr, int n, int k)
{
if (k > n)
return -1;
int evenCount = 0, odd1 = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 == 0)
evenCount++;
else if (arr[i] % 4 == 1)
odd1++;
}
if (evenCount >= k)
return 0;
if (k - evenCount <= odd1)
return k - evenCount;
return odd1 + (k - evenCount - odd1) * 2;
}
static public void Main (){
int []arr = { 2, 3, 4, 5, 6 };
int N = arr.Length;
int K = 4;
Console.WriteLine(minOperations(arr, N, K));
}
}
|
Javascript
function minOperations(arr, n, k)
{
if (k > n)
return -1;
let evenCount = 0, odd1 = 0;
for (let i = 0; i < n; i++) {
if (arr[i] % 2 == 0)
evenCount++;
else if (arr[i] % 4 == 1)
odd1++;
}
if (evenCount >= k)
return 0;
if (k - evenCount <= odd1)
return k - evenCount;
return odd1 + (k - evenCount - odd1) * 2;
}
let arr = [ 2, 3, 4, 5, 6 ];
let N = arr.length;
let K = 4;
console.log(minOperations(arr, N, K));
|
Time Complexity: O(N)
Auxiliary Space: O(1), as no extra space is required.
Last Updated :
16 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...