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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count the maximum number of
// array elements that can be reduced to 1
// by repeatedly dividing array elements by 2
void findMaxNumbers(int arr[], int n, int k)
{
// Sort the array in ascending order
sort(arr, arr + n);
// Store the count of array elements
int cnt = 0;
// Traverse the array
for (int i = 0; i < n; i++) {
// Store the number of operations
// required to reduce arr[i] to 1
int opr = ceil(log2(arr[i]));
// Decrement k by opr
k -= opr;
// If k becomes less than 0,
// then break out of the loop
if (k < 0) {
break;
}
// Increment cnt by 1
cnt++;
}
// Print the answer
cout << cnt;
}
// Driver Code
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
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to count the maximum number of
// array elements that can be reduced to 1
// by repeatedly dividing array elements by 2
static void findMaxNumbers(int arr[], int n, int k)
{
// Sort the array in ascending order
Arrays.sort(arr);
// Store the count of array elements
int cnt = 0;
// Traverse the array
for (int i = 0; i < n; i++)
{
// Store the number of operations
// required to reduce arr[i] to 1
int opr = (int)Math.ceil((Math.log(arr[i]) / Math.log(2)));
// Decrement k by opr
k -= opr;
// If k becomes less than 0,
// then break out of the loop
if (k < 0) {
break;
}
// Increment cnt by 1
cnt++;
}
// Print the answer
System.out.println(cnt);
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 5, 8, 4, 7 };
int N = arr.length;
int K = 5;
findMaxNumbers(arr, N, K);
}
}
// This code is contributed by jana_sayantan.
Python3
# Python3 program to implement
# the above approach
import math
# Function to count the maximum number of
# array elements that can be reduced to 1
# by repeatedly dividing array elements by 2
def findMaxNumbers(arr, n, k) :
# Sort the array in ascending order
arr.sort()
# Store the count of array elements
cnt = 0
# Traverse the array
for i in range(n):
# Store the number of operations
# required to reduce arr[i] to 1
opr = math.ceil(math.log2(arr[i]))
# Decrement k by opr
k -= opr
# If k becomes less than 0,
# then break out of the loop
if (k < 0) :
break
# Increment cnt by 1
cnt += 1
# Print the answer
print(cnt)
# Driver Code
arr = [ 5, 8, 4, 7 ]
N = len(arr)
K = 5
findMaxNumbers(arr, N, K)
# This code is contributed by splevel62.
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to count the maximum number of
// array elements that can be reduced to 1
// by repeatedly dividing array elements by 2
static void findMaxNumbers(int[] arr, int n, int k)
{
// Sort the array in ascending order
Array.Sort(arr);
// Store the count of array elements
int cnt = 0;
// Traverse the array
for (int i = 0; i < n; i++)
{
// Store the number of operations
// required to reduce arr[i] to 1
int opr = (int)Math.Ceiling((Math.Log(arr[i]) / Math.Log(2)));
// Decrement k by opr
k -= opr;
// If k becomes less than 0,
// then break out of the loop
if (k < 0) {
break;
}
// Increment cnt by 1
cnt++;
}
// Print the answer
Console.Write(cnt);
}
// Driver Code
public static void Main(String[] args)
{
int[] arr = { 5, 8, 4, 7 };
int N = arr.Length;
int K = 5;
findMaxNumbers(arr, N, K);
}
}
// This code is contributed by susmitakundugoaldanga.
JavaScript
<script>
// Javascript program for the above approach
// Function to count the maximum number of
// array elements that can be reduced to 1
// by repeatedly dividing array elements by 2
function findMaxNumbers( arr, n, k)
{
// Sort the array in ascending order
arr.sort();
// Store the count of array elements
let cnt = 0;
// Traverse the array
for (let i = 0; i < n; i++) {
// Store the number of operations
// required to reduce arr[i] to 1
let opr = Math.ceil(Math.log2(arr[i]));
// Decrement k by opr
k -= opr;
// If k becomes less than 0,
// then break out of the loop
if (k < 0) {
break;
}
// Increment cnt by 1
cnt++;
}
// Print the answer
document.write(cnt);
}
// Driver Code
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)
Similar Reads
Minimize the max of Array by breaking array elements at most K times Given an integer array arr[] of size N and a positive integer K, the task is to minimize the maximum of the array by replacing any element arr[i] into two positive elements (X, Y) at most K times such that arr[i] = X + Y. Examples: Input: arr = {9}, K = 2Output: 3Explanation: Operation 1: Replace el
15+ min read
Minimize maximum array element by splitting array elements into powers of two at most K times Given an array arr[] consisting of N positive integers and an integer K, the task is to minimize the maximum value of the array by splitting the array element into powers of 2 at most K times. Examples: Input: arr[] = {2, 4, 11, 2}, K = 2Output: 2Explanation:Below are the operations performed on arr
12 min read
Minimize the maximum of Array by replacing any element with other element at most K times Given an array arr[] of size N and an integer K, the task is to minimize the value of the maximum element of the array arr[] after replacing any element of the array with any other element of that array at most K times.Examples:Input: arr[] = {5, 3, 3, 2, 1}, K = 3Output: 2Explanation: Replace the e
8 min read
Maximize count of unique array elements by incrementing array elements by K Given an array arr[] consisting of N integers and an integer K, the task is to find the maximum number of unique elements possible by increasing any array element by K only once. Examples: Input: arr[] = {0, 2, 4, 3, 4}, K = 1Output: 5Explanation:Increase arr[2] ( = 4) by K ( = 1). Therefore, new ar
8 min read
Minimize maximum array element possible by at most K splits on the given array Given an array arr[] consisting of N positive integers and a positive integer K, the task is to minimize the maximum element present in the array by splitting at most K array elements into two numbers equal to their value. Examples: Input: arr[] = {2, 4, 8, 2}, K = 4Output: 2Explanation:Following se
9 min read
Maximum number of consecutive 1's in binary representation of all the array elements Given an array arr[] of N elements, the task is to find the maximum number of consecutive 1's in the binary representation of an element among all the elements of the given array. Examples: Input: arr[] = {1, 2, 3, 4} Output: 2 Binary(1) = 01 Binary(2) = 10 Binary(3) = 11 Binary(4) = 100 Input: arr[
6 min read