Skip to content
Related Articles

Related Articles

Improve Article

Minimize maximum array element by splitting array elements into powers of two at most K times

  • Last Updated : 24 Jun, 2021

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 = 2
Output: 2
Explanation:
Below are the operations performed on array elements at most K(= 2) times:
Operation 1: Remove the element at index 2, i.e., arr[2] = 11 and replace it with 11 numbers of 1s in it. Now the array arr[] modifies to {2, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2}.
Operation 2: Remove the element at index 1, i.e., arr[1] = 4 and replace it with 4 numbers of 1s in it. Now the array arr[] modifies to {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2}.

After performing the above operations, the maximum value of the array is 2, which is minimum possible value.

Input: arr[]= {9}, K = 2
Output: 1



Approach: The given problem can be solved by using the fact that every number can be expressed the sum of 1 which is a power of 2. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum value
// of the maximum element of the array
// by splitting at most K array element
// into perfect powers of 2
void minimumSize(int arr[], int N, int K)
{
    // Sort the array element in
    // the ascending order
    sort(arr, arr + N);
 
    // Reverse the array
    reverse(arr, arr + N);
 
    // If count of 0 is equal to N
    if (count(arr, arr + N, 0) == N)
        cout << 0;
 
    // Otherwise, if K is greater
    // than or equal to N
    else if (K >= N)
        cout << 1 << endl;
 
    // Otherwise
    else
        cout << arr[K] << endl;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 4, 8, 2 };
    int K = 2;
    int N = sizeof(arr) / sizeof(arr[0]);
    minimumSize(arr, N, K);
 
    return 0;
}

Java




// Java program for the above approach
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to find the minimum value
// of the maximum element of the array
// by splitting at most K array element
// into perfect powers of 2
static void minimumSize(int arr[], int N, int K)
{
     
    // Sort the array element in
    // the ascending order
    Arrays.sort(arr);
 
    // Reverse the array
    reverse(arr);
 
    // If count of 0 is equal to N
    if (count(arr, 0) == N)
        System.out.println(0);
 
    // Otherwise, if K is greater
    // than or equal to N
    else if (K >= N)
        System.out.println(1);
 
    // Otherwise
    else
        System.out.println(arr[K]);
}
 
static void reverse(int[] a)
{
    int i, k, t;
    int n = a.length;
     
    for(i = 0; i < n / 2; i++)
    {
        t = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = t;
    }
}
 
static int count(int[] a, int n)
{
    int freq = 0;
     
    for(int i = 0; i < a.length; i++)
    {
        if (a[i] == n)
            freq++;
    }
    return freq;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 2, 4, 8, 2 };
    int K = 2;
    int N = arr.length;
     
    minimumSize(arr, N, K);
}
}
 
// This code is contributed by offbeat

Python




# Python program for the above approach
 
# Function to find the minimum value
# of the maximum element of the array
# by splitting at most K array element
# into perfect powers of 2
def minimumSize(arr, N, K):
   
    # Sort the array element in
    # the ascending order
    arr.sort()
     
    # Reverse the array
    arr.reverse()
     
    # If count of 0 is equal to N
    zero = arr.count(0)
    if zero == N:
        print(0)
         
    # Otherwise, if K is greater
    # than or equal to N
    elif K >= N:
        print(1)
         
    # Otherwise
    else:
        print(arr[K])
 
# Driver Code
arr = [2, 4, 8, 2]
K = 2
N = len(arr)
minimumSize(arr, N, K)
 
# This code is contributed by sudhanshugupta2019a.

C#




// C#program for the above approach
using System;
class GFG
{
 
    // Function to find the minimum value
    // of the maximum element of the array
    // by splitting at most K array element
    // into perfect powers of 2
    static void minimumSize(int[] arr, int N, int K)
    {
 
        // Sort the array element in
        // the ascending order
        Array.Sort(arr);
 
        // Reverse the array
        Array.Reverse(arr);
 
        // If count of 0 is equal to N
        if (count(arr, 0) == N)
            Console.WriteLine(0);
 
        // Otherwise, if K is greater
        // than or equal to N
        else if (K >= N)
            Console.WriteLine(1);
 
        // Otherwise
        else
            Console.WriteLine(arr[K]);
    }
 
    static void reverse(int[] a)
    {
        int i, t;
        int n = a.Length;
 
        for (i = 0; i < n / 2; i++) {
            t = a[i];
            a[i] = a[n - i - 1];
            a[n - i - 1] = t;
        }
    }
 
    static int count(int[] a, int n)
    {
        int freq = 0;
 
        for (int i = 0; i < a.Length; i++) {
            if (a[i] == n)
                freq++;
        }
        return freq;
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        int[] arr = { 2, 4, 8, 2 };
        int K = 2;
        int N = arr.Length;
 
        minimumSize(arr, N, K);
    }
}
 
// This code is contributed by ukasp.

Javascript




<script>
 
// JavaScript program for the above approach
 
// Function to find the minimum value
// of the maximum element of the array
// by splitting at most K array element
// into perfect powers of 2
function minimumSize(arr,N,K)
{
    // Sort the array element in
    // the ascending order
    (arr).sort(function(a,b){return a-b;});
  
    // Reverse the array
    reverse(arr);
  
    // If count of 0 is equal to N
    if (count(arr, 0) == N)
        document.write(0);
  
    // Otherwise, if K is greater
    // than or equal to N
    else if (K >= N)
        document.write(1);
  
    // Otherwise
    else
        document.write(arr[K]);
}
 
function reverse(a)
{
    let i, k, t;
    let n = a.length;
      
    for(i = 0; i < n / 2; i++)
    {
        t = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = t;
    }
}
 
function count(a,n)
{
    let freq = 0;
      
    for(let i = 0; i < a.length; i++)
    {
        if (a[i] == n)
            freq++;
    }
    return freq;
}
 
// Driver code
let arr=[2, 4, 8, 2];
let K = 2;
let N = arr.length;
minimumSize(arr, N, K);
 
 
// This code is contributed by avanitrachhadiya2155
 
</script>
Output: 
2

 

Time Complexity: O(N * log N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :