Minimize the sum calculated by repeatedly removing any two elements and inserting their sum to the Array
Given N elements, you can remove any two elements from the list, note their sum, and add the sum to the list. Repeat these steps while there is more than a single element in the list. The task is to minimize the sum of these chosen sums in the end.
Examples:
Input: arr[] = {1, 4, 7, 10}
Output: 39
Choose 1 and 4, Sum = 5, arr[] = {5, 7, 10}
Choose 5 and 7, Sum = 17, arr[] = {12, 10}
Choose 12 and 10, Sum = 39, arr[] = {22}
Input: arr[] = {1, 3, 7, 5, 6}
Output: 48
Approach: In order to minimize the sum, the elements that get chosen at every step must the minimum elements from the list. In order to do that efficiently, a priority queue can be used. At every step, while there is more than a single element in the list, choose the minimum and the second minimum, remove them from the list add their sum to the list after updating the running sum.
Steps to solve the problem:
- initialize two variable i and sum to store the minimized sum.
- initialize the priority queue with min heap.
- iterate through the array and push all elements in the queue.
- while size of queue is greater than one:
- initialize the min variable and store the top element in the queue.
- pop the top element from the queue.
- initialize the secondMin variable store the top element in the queue.
- update the sum variable with min and secondMin.
- push the sum of min and secondMin in the queue.
5. return the sum.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int getMinSum( int arr[], int n)
{
int i, sum = 0;
priority_queue< int , vector< int >, greater< int > > pq;
for (i = 0; i < n; i++)
pq.push(arr[i]);
while (pq.size() > 1)
{
int min = pq.top();
pq.pop();
int secondMin = pq.top();
pq.pop();
sum += (min + secondMin);
pq.push(min + secondMin);
}
return sum;
}
int main()
{
int arr[] = { 1, 3, 7, 5, 6 };
int n = sizeof (arr)/ sizeof (arr[0]);
cout << (getMinSum(arr, n));
}
|
Java
import java.util.PriorityQueue;
class GFG
{
static int getMinSum( int arr[], int n)
{
int i, sum = 0 ;
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (i = 0 ; i < n; i++)
pq.add(arr[i]);
while (pq.size() > 1 )
{
int min = pq.poll();
int secondMin = pq.poll();
sum += (min + secondMin);
pq.add(min + secondMin);
}
return sum;
}
public static void main(String[] args)
{
int arr[] = { 1 , 3 , 7 , 5 , 6 };
int n = arr.length;
System.out.print(getMinSum(arr, n));
}
}
|
Python3
import heapq
def getMinSum(arr, n):
summ = 0
pq = arr
heapq.heapify(pq)
while ( len (pq) > 1 ):
minn = pq[ 0 ]
pq[ 0 ] = pq[ - 1 ]
pq.pop()
heapq.heapify(pq)
secondMin = pq[ 0 ]
pq[ 0 ] = pq[ - 1 ]
pq.pop()
summ + = (minn + secondMin)
pq.append(minn + secondMin)
heapq.heapify(pq)
return summ
if __name__ = = "__main__" :
arr = [ 1 , 3 , 7 , 5 , 6 ]
n = len (arr)
print (getMinSum(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int getMinSum( int [] arr, int n)
{
int i, sum = 0;
List< int > pq = new List< int >();
for (i = 0; i < n; i++)
{
pq.Add(arr[i]);
}
while (pq.Count > 1)
{
pq.Sort();
int min = pq[0];
pq.RemoveAt(0);
int secondMin = pq[0];
pq.RemoveAt(0);
sum += (min + secondMin);
pq.Add(min + secondMin);
}
return sum;
}
static public void Main ()
{
int [] arr = { 1, 3, 7, 5, 6 };
int n = arr.Length;
Console.WriteLine(getMinSum(arr, n));
}
}
|
Javascript
<script>
function getMinSum(arr,n)
{
let i, sum = 0;
let pq = [];
for (i = 0; i < n; i++)
pq.push(arr[i]);
while (pq.length > 1)
{
let min = pq.shift();
let secondMin = pq.shift();
sum += (min + secondMin);
pq.push(min + secondMin);
}
return sum;
}
let arr=[1, 3, 7, 5, 6];
let n = arr.length;
document.write(getMinSum(arr, n));
</script>
|
Time Complexity : O(N * log(N))
Auxiliary Space: O(N)
Last Updated :
19 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...