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++
#include <bits/stdc++.h>
using namespace std;
int minSum( int a[], int n, int k)
{
priority_queue < int > q;
for ( int i = 0; i < n; i++)
{
q.push(a[i]);
}
while (!q.empty() && k > 0)
{
int top = q.top() / 2;
q.pop();
q.push(top);
k -= 1;
}
int sum = 0;
while (!q.empty())
{
sum += q.top();
q.pop();
}
return sum;
}
int main()
{
int n = 4;
int k = 3;
int a[] = { 20, 7, 5, 4 };
cout << (minSum(a, n, k));
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int minSum( int a[], int n, int k)
{
PriorityQueue<Integer> maxheap
= new PriorityQueue<>((one, two) -> two - one);
for ( int i = 0 ; i < n; i++)
maxheap.add(a[i]);
while (maxheap.size() > 0 && k > 0 ) {
int max_ele = maxheap.poll();
maxheap.add(max_ele / 2 );
k -= 1 ;
}
int sum = 0 ;
while (maxheap.size() > 0 )
sum += maxheap.poll();
return sum;
}
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
def minSum(a, n, k):
q = []
for i in range (n):
q.append(a[i])
q = sorted (q)
while ( len (q) > 0 and k > 0 ):
top = q[ - 1 ] / / 2
del q[ - 1 ]
q.append(top)
k - = 1
q = sorted (q)
sum = 0
while ( len (q) > 0 ):
sum + = q[ - 1 ]
del q[ - 1 ]
return sum
if __name__ = = '__main__' :
n = 4
k = 3
a = [ 20 , 7 , 5 , 4 ]
print (minSum(a, n, k))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int minSum( int [] a, int n, int k)
{
List< int > q = new List< int >();
for ( int i = 0; i < n; i++)
{
q.Add(a[i]);
}
q.Sort();
while (q.Count != 0 && k > 0)
{
int top = q[q.Count - 1] / 2;
q[q.Count - 1] = top;
k--;
q.Sort();
}
int sum = 0;
while (q.Count != 0)
{
sum += q[0];
q.RemoveAt(0);
}
return sum;
}
static public void Main()
{
int n = 4;
int k = 3;
int [] a = { 20, 7, 5, 4 };
Console.WriteLine(minSum(a, n, k));
}
}
|
Javascript
<script>
function minSum(a,n,k)
{
let 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) {
let max_ele = maxheap.pop();
maxheap.push(Math.floor(max_ele / 2));
k -= 1;
maxheap.sort( function (a,b){ return a-b;});
}
let sum = 0;
while (maxheap.length > 0)
sum += maxheap.shift();
return sum;
}
let n = 4;
let k = 3;
let a = [ 20, 7, 5, 4 ];
document.write(minSum(a, n, k));
</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;
int minSum(vector< int >& a, int n, int k)
{
sort(a.begin(), a.end());
queue< int > queue;
while (k > 0) {
if (queue.empty()) {
int temp = a.back();
a.pop_back();
queue.push(temp / 2);
k = k - 1;
}
else {
int temp = a.back();
if (queue.front() > temp) {
temp = queue.front();
queue.pop();
queue.push(temp / 2);
}
else {
a.pop_back();
queue.push(temp / 2);
}
k = k - 1;
}
}
int sum = 0;
while (!queue.empty()) {
sum += queue.front();
queue.pop();
}
for ( int i = 0; i < a.size(); i++) {
sum += a[i];
}
return sum;
}
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;
}
|
Java
import java.util.*;
public class Main {
public static int minSum(List<Integer> a, int k)
{
Collections.sort(a);
Queue<Integer> queue = new LinkedList<>();
while (k > 0 ) {
if (queue.isEmpty()) {
int temp = a.get(a.size() - 1 );
a.remove(a.size() - 1 );
queue.offer(temp / 2 );
k = k - 1 ;
}
else {
int temp = a.get(a.size() - 1 );
if (queue.peek() > temp) {
temp = queue.poll();
queue.offer(temp / 2 );
}
else {
a.remove(a.size() - 1 );
queue.offer(temp / 2 );
}
k = k - 1 ;
}
}
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));
}
}
|
Python3
def minSum(a, n, k):
a.sort()
queue = []
while k > 0 :
if len (queue) = = 0 :
temp = a[ - 1 ]
del a[ - 1 ]
queue.append(temp / / 2 )
k = k - 1
else :
temp = a[ - 1 ]
if queue[ 0 ] > temp:
temp = queue.pop( 0 )
queue.append(temp / / 2 )
else :
del a[ - 1 ]
queue.append(temp / / 2 )
k = k - 1
return sum (queue) + sum (a)
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))
|
Javascript
function minSum(a, n, k)
{
a.sort((x, y) => x - y);
const queue = [];
while (k > 0) {
if (queue.length === 0) {
const temp = a.pop();
queue.push(Math.floor(temp / 2));
k = k - 1;
} else {
const temp = a.pop();
if (queue[0] > temp) {
const rear = queue.shift();
queue.push(Math.floor(temp / 2));
a.push(rear);
} else {
queue.push(Math.floor(temp / 2));
}
k = k - 1;
}
}
let sum = 0;
for (let i = 0; i < a.length; i++) {
sum += a[i];
}
while (queue.length > 0) {
sum += queue.shift();
}
return sum;
}
const n = 4;
const k = 3;
const a = [20, 7, 5, 4];
console.log(minSum(a, n, k));
const N = 4;
const A = [10, 4, 6, 16];
const K = 2;
console.log(minSum(A, N, K));
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static int MinSum(List< int > a, int n, int k)
{
a.Sort();
Queue< int > queue = new Queue< int >();
while (k > 0) {
if (queue.Count == 0) {
int temp = a.Last();
a.RemoveAt(a.Count - 1);
queue.Enqueue(temp / 2);
k = k - 1;
}
else {
int temp = a.Last();
if (queue.Peek() > temp) {
temp = queue.Dequeue();
queue.Enqueue(temp / 2);
}
else {
a.RemoveAt(a.Count - 1);
queue.Enqueue(temp / 2);
}
k = k - 1;
}
}
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));
}
}
|
Time Complexity: O(k + Nlog(N)) : Nlog(N) for sorting
Auxiliary Space: O(N) : for making a queue
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
31 Mar, 2023
Like Article
Save Article