Minimize Sum of an Array by at most K reductions
Last Updated :
07 Jan, 2024
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++
// 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>
Time Complexity: O(Klog(N))
Auxiliary Space: O(N)
Other approach: By using a queue
- make a empty double ended queue
- sort the array
- pick max element from comparing front element from queue and last element from array
- insert (popped element) / 2 into the end of the queue
- repeat the process k times
Examples :
Input: N = 4, a[] = {20, 7, 5, 4}, K = 3
Output: 17
Explanation:
sorted array =a[] = {4 , 5, 7 ,20}
queue = []
Operation 1: pop max from array and insert in queue then array becomes , a[] = { 4 , 5,7} , queue = [10]
Operation 2: compare max from array and rear from queue which is greater and append at the end of the queue max of array = 7 font of queue = 10 rear of queue is greater append element at the queue and remove the rear element. a[] = { 4 , 5, ,7} , queue = [5]
Operation 3:compare max from array and rear from queue which is greater and append at the end of the queue max of array = 7 font of queue = 5 , max element from the array is greater and remove the max element from array and append to the queue. a[] = { 4 , 5 } , queue = [5 , 3]
No further operation can be performed. Therefore, sum of the array and queue = 17
Input: N = 4, a[] = {10, 4, 6, 16}, K = 2
Output: 23
C++
#include <bits/stdc++.h>
using namespace std;
// Function to obtain the minimum possible
// sum from the array by K reductions
int minSum(vector<int>& a, int n, int k)
{
// Sort the array in ascending order
sort(a.begin(), a.end());
// Create a queue
queue<int> queue;
while (k > 0) {
// If queue is empty, append the max element of
// array
if (queue.empty()) {
int temp = a.back();
// Delete the max element from array
a.pop_back();
// Append the max element to queue
queue.push(temp / 2);
// Decrement k
k = k - 1;
}
else {
int temp = a.back();
// If rear is greater than max, append the rear
// element
if (queue.front() > temp) {
// Pop the rear element from queue
temp = queue.front();
queue.pop();
queue.push(temp / 2);
}
else {
a.pop_back();
queue.push(temp / 2);
}
k = k - 1;
}
}
// Stores the sum of remaining elements
int sum = 0;
while (!queue.empty()) {
sum += queue.front();
queue.pop();
}
for (int i = 0; i < a.size(); i++) {
sum += a[i];
}
return sum;
}
// Driver code
int main()
{
int n = 4;
int k = 3;
vector<int> a{ 20, 7, 5, 4 };
cout << minSum(a, n, k) << endl;
int N = 4;
vector<int> A{ 10, 4, 6, 16 };
int K = 2;
cout << minSum(A, N, K) << endl;
return 0;
}
// This code is contributed by sarojmcy2e
Java
// java program to implement the above approach
import java.util.*;
public class Main {
public static int minSum(List<Integer> a, int k)
{
// Sort the array in ascending order
Collections.sort(a);
// Create a queue
Queue<Integer> queue = new LinkedList<>();
while (k > 0) {
// If queue is empty, append the max element of
// array
if (queue.isEmpty()) {
int temp = a.get(a.size() - 1);
// Delete the max element from array
a.remove(a.size() - 1);
// Append the max element to queue
queue.offer(temp / 2);
// Decrement k
k = k - 1;
}
else {
int temp = a.get(a.size() - 1);
// If rear is greater than max, append the
// rear element
if (queue.peek() > temp) {
// Pop the rear element from queue
temp = queue.poll();
queue.offer(temp / 2);
}
else {
a.remove(a.size() - 1);
queue.offer(temp / 2);
}
k = k - 1;
}
}
// Stores the sum of remaining elements
int sum = 0;
while (!queue.isEmpty()) {
sum += queue.poll();
}
for (int i = 0; i < a.size(); i++) {
sum += a.get(i);
}
return sum;
}
public static void main(String[] args)
{
List<Integer> a = new ArrayList<>();
a.add(20);
a.add(7);
a.add(5);
a.add(4);
int k = 3;
System.out.println(minSum(a, k));
List<Integer> A = new ArrayList<>();
A.add(10);
A.add(4);
A.add(6);
A.add(16);
int K = 2;
System.out.println(minSum(A, K));
}
}
// this code is implemented by Chetan Bargal
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):
a.sort()
# Create a queue
queue = []
while k > 0:
# if queue is empty append the max element of array
if len(queue) == 0:
temp = a[-1]
# del the max element from array
del a[-1]
# append the max element to queue
queue.append(temp // 2)
# decrement the k
k = k - 1
# Insert maximum / 2
else:
# store max element to temp
temp = a[-1]
# if rear is greater than max
# append the rear element
if queue[0] > temp:
# pop the rear element from queue
temp = queue.pop(0)
queue.append(temp // 2)
else:
del a[-1]
queue.append(temp // 2)
k = k - 1
# Stores the sum of remaining elements
return sum(queue) + sum(a)
# Driver code
if __name__ == '__main__':
n = 4
k = 3
a = [20, 7, 5, 4]
print(minSum(a, n, k))
N = 4
A = [10, 4, 6, 16 ]
K = 2
print(minSum(A, N, K))
# This code is Contributed by Shushant Kumar
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
// Function to obtain the minimum possible
// sum from the array by K reductions
static int MinSum(List<int> a, int n, int k)
{
// Sort the array in ascending order
a.Sort();
// Create a queue
Queue<int> queue = new Queue<int>();
while (k > 0) {
// If queue is empty, append the max element of
// array
if (queue.Count == 0) {
int temp = a.Last();
// Delete the max element from array
a.RemoveAt(a.Count - 1);
// Append the max element to queue
queue.Enqueue(temp / 2);
// Decrement k
k = k - 1;
}
else {
int temp = a.Last();
// If rear is greater than max, append the
// rear element
if (queue.Peek() > temp) {
// Pop the rear element from queue
temp = queue.Dequeue();
queue.Enqueue(temp / 2);
}
else {
a.RemoveAt(a.Count - 1);
queue.Enqueue(temp / 2);
}
k = k - 1;
}
}
// Stores the sum of remaining elements
int sum = 0;
while (queue.Count != 0) {
sum += queue.Dequeue();
}
for (int i = 0; i < a.Count; i++) {
sum += a[i];
}
return sum;
}
static void Main(string[] args)
{
int n = 4;
int k = 3;
List<int> a = new List<int>{ 20, 7, 5, 4 };
Console.WriteLine(MinSum(a, n, k));
int N = 4;
List<int> A = new List<int>{ 10, 4, 6, 16 };
int K = 2;
Console.WriteLine(MinSum(A, N, K));
}
}
// This code is contributed by user_dtewbxkn77n
JavaScript
// Function to obtain the minimum possible
// sum from the array by K reductions
function minSum(a, n, k)
{
// Sort the array in ascending order
a.sort((x, y) => x - y);
// Create a queue
const queue = [];
while (k > 0) {
// If queue is empty, append the max element of array
if (queue.length === 0) {
const temp = a.pop();
// Append the max element to queue
queue.push(Math.floor(temp / 2));
// Decrement k
k = k - 1;
} else {
const temp = a.pop();
// If rear is greater than max, append the rear element
if (queue[0] > temp) {
// Pop the rear element from queue
const rear = queue.shift();
queue.push(Math.floor(temp / 2));
a.push(rear);
} else {
queue.push(Math.floor(temp / 2));
}
k = k - 1;
}
}
// Stores the sum of remaining elements
let sum = 0;
for (let i = 0; i < a.length; i++) {
sum += a[i];
}
while (queue.length > 0) {
sum += queue.shift();
}
return sum;
}
// Driver code
const n = 4;
const k = 3;
const a = [20, 7, 5, 4];
console.log(minSum(a, n, k)); // Output: 17
const N = 4;
const A = [10, 4, 6, 16];
const K = 2;
console.log(minSum(A, N, K)); // Output: 23
Time Complexity: O(k + Nlog(N)) : Nlog(N) for sorting
Auxiliary Space: O(N) : for making a queue
Similar Reads
Minimum length of X[] after removing at most K sub-arrays Given an array X[] of length N and an integer K. Then your task is to output minimum length of X[] after removing at most K number of sub-arrays, which satisfies the following conditions: Size of the sub-array must be greater than 1.First and last element of sub-array must be same.Note: The sub-arra
10 min read
Minimize the sum of MEX by removing all elements of array Given an array of integers arr[] of size N. You can perform the following operation N times: Pick any index i, and remove arr[i] from the array and add MEX(arr[]) i.e., Minimum Excluded of the array arr[] to your total score. Your task is to minimize the total score. Examples: Input: N = 8, arr[] =
7 min read
Minimize the cost of partitioning an array into K groups Given an array arr[] and an integer K, the task is to partition the array into K non-empty groups where each group is a subarray of the given array and each element of the array is part of only one group. All the elements in a given group must have the same value. You can perform the following opera
15+ min read
Minimum operations to make GCD of array a multiple of k Given an array and k, we need to find the minimum operations needed to make GCD of the array equal or multiple of k. Here an operation means either increment or decrements an array element by 1. Examples: Input : a = { 4, 5, 6 }, k = 5 Output : 2 Explanation : We can increase 4 by 1 so that it becom
8 min read
Maximize the cost of reducing array elements Given an array arr[] of N positive integers. We can choose any one index(say K) of the array and reduce all the elements of the array from index 0 to K - 1 by 1. The cost of this operation is K. If at any index(say idx) element is reduced to 0 then we can't perform this operation in the range [idx,
6 min read
Minimize cost for reducing array by replacing two elements with sum at most K times for any index Given an array arr[] of size N and an integer K. The task is to find the minimum cost required to collect the sum of the array. The sum of the array is collected by picking any element and adding it to an element of any index in the array. The addition of elements at the same index is allowed for at
11 min read
Maximize the minimum value of Array by performing given operations at most K times Given array A[] of size N and integer K, the task for this problem is to maximize the minimum value of the array by performing given operations at most K times. In one operation choose any index and increase that array element by 1. Examples: Input: A[] = {3, 1, 2, 4, 6, 2, 5}, K = 8Output: 4Explana
10 min read
Minimum sum after subtracting multiples of k from the elements of the array Given an integer K and an integer array, the task is to find the minimum possible sum of all the elements of the array after they are reduced by subtracting a multiple of K from each element (the result must be positive and every element of the array must be equal after this reduction). If the array
15 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
Minimum sum possible by removing all occurrences of any array element Given an array arr[] consisting of N integers, the task is to find the minimum possible sum of the array by removing all occurrences of any single array element. Examples: Input: N = 4, arr[] = {4, 5, 6, 6}Output: 9Explanation: All distinct array elements are {4, 5, 6}. Removing all occurrences of 4
6 min read