# 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 = 3Output:17Explanation:

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 = 2Output: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++

`// 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` |

## Java

`// 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));` ` ` `}` `}` |

## Python3

`# Python3 program to implement the` `# above approach` `# Function to obtain the minimum possible` `# sum from the array by K reductions` `def` `minSum(a, n, k):` ` ` ` ` `q ` `=` `[]` ` ` ` ` `# Insert elements into the MaxHeap` ` ` `for` `i ` `in` `range` `(n):` ` ` `q.append(a[i])` ` ` ` ` `q ` `=` `sorted` `(q) ` ` ` `while` `(` `len` `(q) > ` `0` `and` `k > ` `0` `):` ` ` `top ` `=` `q[` `-` `1` `] ` `/` `/` `2` ` ` `# Remove the maximum` ` ` `del` `q[` `-` `1` `]` ` ` `# Insert maximum / 2` ` ` `q.append(top)` ` ` `k ` `-` `=` `1` ` ` `q ` `=` `sorted` `(q)` ` ` `# Stores the sum of remaining elements` ` ` `sum` `=` `0` ` ` `while` `(` `len` `(q) > ` `0` `):` ` ` `sum` `+` `=` `q[` `-` `1` `]` ` ` `del` `q[` `-` `1` `]` ` ` ` ` `return` `sum` `# Driver code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `n ` `=` `4` ` ` `k ` `=` `3` ` ` ` ` `a ` `=` `[ ` `20` `, ` `7` `, ` `5` `, ` `4` `]` ` ` `print` `(minSum(a, n, k))` `# This code is contributed by mohit kumar 29` |

## C#

`// C# program to implement the` `// above approach` `using` `System;` `using` `System.Collections.Generic;` `class` `GFG{` ` ` `// Function to obtain the minimum possible` `// sum from the array by K reductions` `static` `int` `minSum(` `int` `[] a, ` `int` `n, ` `int` `k)` `{` ` ` ` ` `// Implements the MaxHeap` ` ` `List<` `int` `> q = ` `new` `List<` `int` `>();` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `{` ` ` ` ` `// Insert elements into the MaxHeap` ` ` `q.Add(a[i]);` ` ` `}` ` ` ` ` `q.Sort();` ` ` `while` `(q.Count != 0 && k > 0)` ` ` `{` ` ` `int` `top = q[q.Count - 1] / 2;` ` ` ` ` `// Remove the maximum` ` ` `// Insert maximum / 2` ` ` `q[q.Count - 1] = top;` ` ` ` ` `k--;` ` ` `q.Sort();` ` ` `}` ` ` ` ` `// Stores the sum of remaining elements` ` ` `int` `sum = 0;` ` ` `while` `(q.Count != 0)` ` ` `{` ` ` `sum += q[0];` ` ` `q.RemoveAt(0);` ` ` `}` ` ` `return` `sum;` `}` `// Driver Code` `static` `public` `void` `Main()` `{` ` ` `int` `n = 4;` ` ` `int` `k = 3;` ` ` `int` `[] a = { 20, 7, 5, 4 };` ` ` ` ` `Console.WriteLine(minSum(a, n, k));` `}` `}` `// This code is contributed by avanitrachhadiya2155` |

## Javascript

`<script>` `// Javascript Program to implement the` `// above approach` `// Function to obtain the minimum possible` ` ` `// sum from the array by K reductions` `function` `minSum(a,n,k)` `{` ` ` `// Implements the MaxHeap` ` ` `let maxheap = [];` ` ` ` ` `// Insert elements into the MaxHeap` ` ` `for` `(let i = 0; i < n; i++)` ` ` `maxheap.push(a[i]);` ` ` ` ` `maxheap.sort(` `function` `(a,b){` `return` `a-b;});` ` ` ` ` `while` `(maxheap.length > 0 && k > 0) {` ` ` ` ` `// Remove the maximum` ` ` `let max_ele = maxheap.pop();` ` ` ` ` `// Insert maximum / 2` ` ` `maxheap.push(Math.floor(max_ele / 2));` ` ` `k -= 1;` ` ` `maxheap.sort(` `function` `(a,b){` `return` `a-b;});` ` ` `}` ` ` ` ` `// Stores the sum of remaining elements` ` ` ` ` `let sum = 0;` ` ` `while` `(maxheap.length > 0)` ` ` `sum += maxheap.shift();` ` ` ` ` `return` `sum;` `}` `// Driver Code` `let n = 4;` `let k = 3;` `let a = [ 20, 7, 5, 4 ];` `document.write(minSum(a, n, k));` `// This code is contributed by unknown2108` `</script>` |

**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. 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**.