Minimize Sum of an Array by at most K reductions

Given an array of integers arr[] consisting of N integers, the task is to minimize the sum of the given array by performing at most K operations, where each operation involves reducing an array element arr[i] to floor(arr[i]/2).

Examples :

Input: N = 4, a[] = {20, 7, 5, 4}, K = 3 
Output: 17 
Explanation: 
Operation 1: {20, 7, 5, 4} -> {10, 7, 5, 4} 
Operation 2: {10, 7, 5, 4} -> {5, 7, 5, 4} 
Operation 3: {5, 7, 5, 4} -> {5, 3, 5, 4} 
No further operation can be performed. Therefore, sum of the array = 17.

Input: N = 4, a[] = {10, 4, 6, 16}, K = 2 
Output: 23

Approach : To obtain the minimum possible sum, the main idea for every operation is to reduce the maximum element in the array before each operation. This can be implemented using MaxHeap. Follow the steps below to solve the problem:



  • Insert all the array elements into MaxHeap.
  • Pop the root of the MaxHeap and insert (popped element) / 2 into the MaxHeap
  • After repeating the above step K times, pop the elements of the MaxHeap one by one and keep adding their values. Finally, print the sum.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement the 
// above approach 
#include <bits/stdc++.h>
using namespace std;
  
// Function to obtain the minimum possible 
// sum from the array by K reductions 
int minSum(int a[], int n, int k)
{
    priority_queue <int> q;
      
    // Insert elements into the MaxHeap 
    for(int i = 0; i < n; i++)
    {
        q.push(a[i]);
    }
      
    while(!q.empty() && k > 0)
    {
        int top = q.top() / 2;
          
        // Remove the maximum             
        q.pop();
          
        // Insert maximum / 2 
        q.push(top);
        k -= 1;
    }
      
    // Stores the sum of remaining elements
    int sum = 0;
    while(!q.empty())
    {
        sum += q.top();
        q.pop();
    }
    return sum;
}
  
// Driver code
int main() 
{
    int n = 4; 
    int k = 3; 
    int a[] = { 20, 7, 5, 4 }; 
          
    cout << (minSum(a, n, k));
      
    return 0;
}
  
// This code is contributed by jojo9911

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to implement the 
// above approach 
import java.io.*; 
import java.util.*; 
class GFG { 
  
    // Function to obtain the minimum possible 
    // sum from the array by K reductions 
    public static int minSum(int a[], int n, int k) 
    
        // Implements the MaxHeap 
        PriorityQueue<Integer> maxheap 
            = new PriorityQueue<>((one, two) -> two - one); 
  
        // Insert elements into the MaxHeap 
        for (int i = 0; i < n; i++) 
            maxheap.add(a[i]); 
  
        while (maxheap.size() > 0 && k > 0) { 
  
            // Remove the maximum 
            int max_ele = maxheap.poll(); 
  
            // Insert maximum / 2 
            maxheap.add(max_ele / 2); 
            k -= 1
        
  
        // Stores the sum of remaining elements 
  
        int sum = 0
        while (maxheap.size() > 0
            sum += maxheap.poll(); 
  
        return sum; 
    
  
    // Driver Code 
    public static void main(String[] args) 
    
        int n = 4
        int k = 3
        int a[] = { 20, 7, 5, 4 }; 
        System.out.println(minSum(a, n, k)); 
    

chevron_right


Output: 

17

Time Complexity: O(Klog(N)) 
Auxiliary Space: O(N)

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.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jojo9911