Maximize count of 1s in an array by repeated division of array elements by 2 at most K times
Last Updated :
10 Nov, 2021
Given an array arr[] of size N and an integer K, the task to find the maximum number of array elements that can be reduced to 1 by repeatedly dividing any element by 2 at most K times.
Note: For odd array elements, take its ceil value of division.
Examples:
Input: arr[] = {5, 8, 4, 7}, K = 5
Output: 2
Explanation:
5 needs 3 operations(5?3?2?1).
8 needs 3 operations(8?4?2?1).
4 needs 2 operations(4?2?1).
7 needs 3 operations(7?4?2?1)
Therefore, in 5 operations, the maximum number of array elements that can be reduced to 1 are 2, either (4, 5), (4, 8) or (4, 7).
Input: arr[] = {5, 8, 5, 7}, K = 5
Output: 1
Approach: To maximize the number of elements, the idea is to sort the array in ascending order and start reducing the elements from the first index and decrement K by the number of operations required to reduce the ith element. Follow the steps below to solve the problem:
- Initialize a variable, say cnt, to store the required number of elements.
- Sort the array arr[] in increasing order.
- Traverse the array, arr[] using the variable i, and perform the following steps:
- Store the number of operations required to reduce arr[i] to 1 is opr = ceil(log2(arr[i])).
- Decrement K by opr.
- If the value of K becomes less than 0, break out of the loop. Otherwise, increment cnt by 1.
- After completing the above steps, print the value of cnt as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMaxNumbers( int arr[], int n, int k)
{
sort(arr, arr + n);
int cnt = 0;
for ( int i = 0; i < n; i++) {
int opr = ceil (log2(arr[i]));
k -= opr;
if (k < 0) {
break ;
}
cnt++;
}
cout << cnt;
}
int main()
{
int arr[] = { 5, 8, 4, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 5;
findMaxNumbers(arr, N, K);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static void findMaxNumbers( int arr[], int n, int k)
{
Arrays.sort(arr);
int cnt = 0 ;
for ( int i = 0 ; i < n; i++)
{
int opr = ( int )Math.ceil((Math.log(arr[i]) / Math.log( 2 )));
k -= opr;
if (k < 0 ) {
break ;
}
cnt++;
}
System.out.println(cnt);
}
public static void main(String args[])
{
int arr[] = { 5 , 8 , 4 , 7 };
int N = arr.length;
int K = 5 ;
findMaxNumbers(arr, N, K);
}
}
|
Python3
import math
def findMaxNumbers(arr, n, k) :
arr.sort()
cnt = 0
for i in range (n):
opr = math.ceil(math.log2(arr[i]))
k - = opr
if (k < 0 ) :
break
cnt + = 1
print (cnt)
arr = [ 5 , 8 , 4 , 7 ]
N = len (arr)
K = 5
findMaxNumbers(arr, N, K)
|
C#
using System;
public class GFG
{
static void findMaxNumbers( int [] arr, int n, int k)
{
Array.Sort(arr);
int cnt = 0;
for ( int i = 0; i < n; i++)
{
int opr = ( int )Math.Ceiling((Math.Log(arr[i]) / Math.Log(2)));
k -= opr;
if (k < 0) {
break ;
}
cnt++;
}
Console.Write(cnt);
}
public static void Main(String[] args)
{
int [] arr = { 5, 8, 4, 7 };
int N = arr.Length;
int K = 5;
findMaxNumbers(arr, N, K);
}
}
|
Javascript
<script>
function findMaxNumbers( arr, n, k)
{
arr.sort();
let cnt = 0;
for (let i = 0; i < n; i++) {
let opr = Math.ceil(Math.log2(arr[i]));
k -= opr;
if (k < 0) {
break ;
}
cnt++;
}
document.write(cnt);
}
let arr = [ 5, 8, 4, 7 ];
let N = arr.length;
let K = 5;
findMaxNumbers(arr, N, K);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...