Minimum cost to empty Array where cost of removing an element is 2^(removed_count) * arr[i]
Last Updated :
31 May, 2021
Given an array arr[], the task is to find the minimum cost to remove all elements from the array where the cost of removing an element is 2^j * arr[i]. Here, j is the number of elements that have already been removed.
Examples:
Input: arr[] = {3, 1, 3, 2}
Output: 25
Explanation:
First remove 3. Cost = 2^(0)*3 = 3
Then remove 3. Cost = 2^(1)*3 = 6
Then remove 2. Cost = 2^(2)*2 = 8
At last, remove 1. Cost = 2^(3)*1 = 8
Total Cost = 3 + 6 + 8 + 8 = 25
Input: arr[] = {1, 2}
Output: 4
Explanation:
First remove 2. Cost = 2^(0)*2 = 2
Then remove 1. Cost = 2^(1)*1 = 2
Total Cost = 2 + 2 = 4
Approach: The idea is to use a greedy programming paradigm to solve this problem.
We have to minimize the expression ( 2^j * arr[i] ). This can be done by:
- Sort the Array in Decreasing order.
- Multiply pow(2, i) with every element i, starting from 0 to the size of the array.
Therefore, the total cost of removing elements from the array is given as:
when the array is in decreasing order.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int removeElements(ll arr[], int n)
{
sort(arr, arr + n, greater< int >());
ll ans = 0;
for ( int i = 0; i < n; i++) {
ans += arr[i] * pow (2, i);
}
return ans;
}
int main()
{
int n = 4;
ll arr[n] = { 3, 1, 2, 3 };
cout << removeElements(arr, n);
}
|
Java
import java.util.*;
class GFG{
static long [] reverse( long a[])
{
int i, n = a.length;
long t;
for (i = 0 ; i < n / 2 ; i++)
{
t = a[i];
a[i] = a[n - i - 1 ];
a[n - i - 1 ] = t;
}
return a;
}
static long removeElements( long arr[],
int n)
{
Arrays.sort(arr);
arr = reverse(arr);
long ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
ans += arr[i] * Math.pow( 2 , i);
}
return ans;
}
public static void main(String[] args)
{
int n = 4 ;
long arr[] = { 3 , 1 , 2 , 3 };
System.out.print(removeElements(arr, n));
}
}
|
Python3
def removeElements(arr, n):
arr.sort(reverse = True )
ans = 0
for i in range (n):
ans + = arr[i] * pow ( 2 , i)
return ans
if __name__ = = "__main__" :
n = 4
arr = [ 3 , 1 , 2 , 3 ]
print (removeElements(arr, n))
|
C#
using System;
class GFG{
static long [] reverse( long []a)
{
int i, n = a.Length;
long t;
for (i = 0; i < n / 2; i++)
{
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
return a;
}
static long removeElements( long []arr,
int n)
{
Array.Sort(arr);
arr = reverse(arr);
long ans = 0;
for ( int i = 0; i < n; i++)
{
ans += ( long )(arr[i] * Math.Pow(2, i));
}
return ans;
}
public static void Main(String[] args)
{
int n = 4;
long []arr = { 3, 1, 2, 3 };
Console.Write(removeElements(arr, n));
}
}
|
Javascript
<script>
function removeElements(arr, n) {
arr.sort((a, b) => b - a);
var ans = 0;
for ( var i = 0; i < n; i++) {
ans += arr[i] * Math.pow(2, i);
}
return ans;
}
var n = 4;
var arr = [3, 1, 2, 3];
document.write(removeElements(arr, n));
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...