Minimize maximum array element by splitting array elements into powers of two at most K times
Last Updated :
22 Feb, 2023
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++
#include <bits/stdc++.h>
using namespace std;
void minimumSize( int arr[], int N, int K)
{
sort(arr, arr + N);
reverse(arr, arr + N);
if (count(arr, arr + N, 0) == N)
cout << 0;
else if (K >= N)
cout << 1 << endl;
else
cout << arr[K] << endl;
}
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
import java.lang.*;
import java.util.*;
class GFG{
static void minimumSize( int arr[], int N, int K)
{
Arrays.sort(arr);
reverse(arr);
if (count(arr, 0 ) == N)
System.out.println( 0 );
else if (K >= N)
System.out.println( 1 );
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;
}
public static void main(String[] args)
{
int arr[] = { 2 , 4 , 8 , 2 };
int K = 2 ;
int N = arr.length;
minimumSize(arr, N, K);
}
}
|
Python
def minimumSize(arr, N, K):
arr.sort()
arr.reverse()
zero = arr.count( 0 )
if zero = = N:
print ( 0 )
elif K > = N:
print ( 1 )
else :
print (arr[K])
arr = [ 2 , 4 , 8 , 2 ]
K = 2
N = len (arr)
minimumSize(arr, N, K)
|
C#
using System;
class GFG
{
static void minimumSize( int [] arr, int N, int K)
{
Array.Sort(arr);
Array.Reverse(arr);
if (count(arr, 0) == N)
Console.WriteLine(0);
else if (K >= N)
Console.WriteLine(1);
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;
}
public static void Main( string [] args)
{
int [] arr = { 2, 4, 8, 2 };
int K = 2;
int N = arr.Length;
minimumSize(arr, N, K);
}
}
|
Javascript
<script>
function minimumSize(arr,N,K)
{
(arr).sort( function (a,b){ return a-b;});
reverse(arr);
if (count(arr, 0) == N)
document.write(0);
else if (K >= N)
document.write(1);
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;
}
let arr=[2, 4, 8, 2];
let K = 2;
let N = arr.length;
minimumSize(arr, N, K);
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(1)
Another efficient approach: In the previous approach, we are sorting the array and found the (K+1)th maximum element for the K<N. Instead of sorting the array, we can use a priority queue to find the (K+1)th maximum element.
The time complexity for this approach in the worst case is O(N*log(K)) for k<N otherwise, the time complexity is O(1). Hence the given approach is much better than the previous approach for a smaller value of k.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minimumSize( int arr[], int N, int K)
{
if (count(arr, arr + N, 0) == N)
cout << 0;
else if (K >= N)
cout << 1 << endl;
else
{
priority_queue< int , vector< int >, greater< int > >pq;
for ( int i = 0; i < N; ++i) {
pq.push(arr[i]);
if (pq.size() > (K+1)) {
pq.pop();
}
}
cout<<pq.top()<<endl;
}
}
int main()
{
int arr[] = { 2, 4, 8, 2 };
int K = 2;
int N = sizeof (arr) / sizeof (arr[0]);
minimumSize(arr, N, K);
return 0;
}
|
Python3
import bisect
def minimumSize(arr,N,K):
if (arr.count( 0 ) = = N):
print ( 0 )
elif (K> = N):
print ( 1 )
else :
pq = []
for i in range (N):
bisect.insort(pq,arr[i])
if ( len (pq)>(K + 1 )):
pq.pop()
print (pq[ 0 ])
arr = [ 2 , 4 , 8 , 2 ]
K = 2
N = len (arr)
minimumSize(arr,N,K)
|
Java
import java.util.PriorityQueue;
public class Main {
public static void minimumSize( int [] arr, int N, int K)
{
if (isZeroArray(arr, N)) {
System.out.println( 0 );
}
else if (K >= N) {
System.out.println( 1 );
}
else {
PriorityQueue<Integer> pq
= new PriorityQueue<>();
for ( int i = 0 ; i < N; ++i) {
pq.add(arr[i]);
if (pq.size() > (K + 1 )) {
pq.poll();
}
}
System.out.println(pq.peek());
}
}
private static boolean isZeroArray( int [] arr, int N)
{
for ( int i = 0 ; i < N; i++) {
if (arr[i] != 0 ) {
return false ;
}
}
return true ;
}
public static void main(String[] args)
{
int [] arr = { 2 , 4 , 8 , 2 };
int K = 2 ;
int N = arr.length;
minimumSize(arr, N, K);
}
}
|
Javascript
function minimumSize(arr, N, K)
{
if (isZeroArray(arr, N)) {
console.log(0);
}
else if (K >= N) {
console.log(1);
}
else
{
const pq = [];
for (let i = 0; i < N; ++i)
{
pq.push(arr[i]);
pq.sort((a, b) => b - a);
if (pq.length > (K + 1))
{
pq.pop();
}
}
console.log(pq[K]);
}
}
function isZeroArray(arr, N) {
for (let i = 0; i < N; i++) {
if (arr[i] !== 0) {
return false ;
}
}
return true ;
}
const arr = [2, 4, 8, 2];
const K = 2;
const N = arr.length;
minimumSize(arr, N, K);
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void minimumSize( int [] arr, int N, int K)
{
if (arr.Count(x => x == 0) == N)
{
Console.WriteLine(0);
}
else if (K >= N)
{
Console.WriteLine(1);
}
else
{
var pq = new SortedSet< int >();
foreach ( var x in arr)
{
pq.Add(x);
if (pq.Count > (K + 1))
{
pq.Remove(pq.Max);
}
}
Console.WriteLine(pq.Min);
}
}
public static void Main()
{
int [] arr = { 2, 4, 8, 2 };
int K = 2;
int N = arr.Length;
minimumSize(arr, N, K);
}
}
|
Time Complexity: O(N*log(K))
Auxiliary Space: O(K)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...