Open In App

Minimize division by 2 to make at least K even numbers in given Array

Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum number of
// divide by 2 operations required
int minOperations(int arr[], int n, int k)
{
    if (k > n)
        return -1;
 
    int evenCount = 0, odd1 = 0;
 
    // Loop to count even elements and
    // odd elements of (4*x + 1) type
    for (int i = 0; i < n; i++) {
        if (arr[i] % 2 == 0)
            evenCount++;
        else if (arr[i] % 4 == 1)
            odd1++;
    }
 
    // Return the total number of operations required
    // calculated based on the mentioned formulae
    if (evenCount >= k)
        return 0;
    if (k - evenCount <= odd1)
        return k - evenCount;
    return odd1 + (k - evenCount - odd1) * 2;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 3, 4, 5, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 4;
 
    // Function call
    cout << minOperations(arr, N, K);
    return 0;
}


Java




// Java code for the above approach
import java.io.*;
 
class GFG {
 
    // Function to find the minimum number of
    // divide by 2 operations required
    static int minOperations(int[] arr, int n, int k)
    {
        if (k > n) {
            return -1;
        }
        int evenCount = 0, odd1 = 0;
 
        // Loop to count even elements and
        // odd elements of (4*x + 1) type
        for (int i = 0; i < n; i++) {
            if (arr[i] % 2 == 0) {
                evenCount++;
            }
            else if (arr[i] % 4 == 1) {
                odd1++;
            }
        }
 
        // Return the total number of operations required
        // calculated based on the mentioned formulae
        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;
 
        // Function call
        System.out.print(minOperations(arr, N, K));
    }
}
 
// This code is contributed by lokesh


Python3




# Python code to implement the approach
 
# Function to find the minimum number of
# divide by 2 operations required
def minOperations(arr, n, k):
     
    if (k > n):
        return -1
 
    evenCount = 0
    odd1 = 0
 
    # Loop to count even elements and
    # odd elements of (4*x + 1) type
    for i in range(0, n):
        if (arr[i] % 2 == 0):
            evenCount+=1
        elif(arr[i] % 4 == 1):
            odd1+=1
 
    # Return the total number of operations required
    # calculated based on the mentioned formulae
    if (evenCount >= k):
        return 0
    if (k - evenCount <= odd1):
        return k - evenCount
    return odd1 + (k - evenCount - odd1) * 2
 
# Driver Code
arr = [ 2, 3, 4, 5, 6 ]
N = len(arr)
K = 4
 
# Function call
print(minOperations(arr, N, K))
 
# This code is contributed by Samim Hossain Mondal.


C#




// C# implementation
using System;
 
public class GFG{
 
  // Function to find the minimum number of
  // divide by 2 operations required
  public static int minOperations(int[] arr,int n,int k)
  {
    if (k > n)
      return -1;
 
    int evenCount = 0, odd1 = 0;
 
    // Loop to count even elements and
    // odd elements of (4*x + 1) type
    for (int i = 0; i < n; i++) {
      if (arr[i] % 2 == 0)
        evenCount++;
      else if (arr[i] % 4 == 1)
        odd1++;
    }
 
    // Return the total number of operations required
    // calculated based on the mentioned formulae
    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;
 
    // Function call
    Console.WriteLine(minOperations(arr, N, K));
 
  }
}
 
// This code is contributed by ksam24000


Javascript




// JavaScript code for the above approach
 
// Function to find the minimum number of
// divide by 2 operations required
function minOperations(arr, n, k)
{
    if (k > n)
        return -1;
 
    let evenCount = 0, odd1 = 0;
 
    // Loop to count even elements and
    // odd elements of (4*x + 1) type
    for (let i = 0; i < n; i++) {
        if (arr[i] % 2 == 0)
            evenCount++;
        else if (arr[i] % 4 == 1)
            odd1++;
    }
 
    // Return the total number of operations required
    // calculated based on the mentioned formulae
    if (evenCount >= k)
        return 0;
    if (k - evenCount <= odd1)
        return k - evenCount;
    return odd1 + (k - evenCount - odd1) * 2;
}
 
// Driver Code
 
let arr = [ 2, 3, 4, 5, 6 ];
let N = arr.length;
let K = 4;
 
// Function call
console.log(minOperations(arr, N, K));
 
// This code is contributed by Potta Lokesh


Output

1

Time Complexity: O(N)
Auxiliary Space: O(1), as no extra space is required.



Last Updated : 16 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads