Minimum cost to empty Array where cost of removing an element is 2^(removed_count) * arr[i]

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 = 25Input: 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++
// C++ implementation to find the// minimum cost of removing all// elements from the arrayÂ
#include <bits/stdc++.h>using namespace std;Â
#define ll long long int// Function to find the minimum// cost of removing elements from// the arrayint removeElements(ll arr[], int n){Â
    // Sorting in Increasing order    sort(arr, arr + n, greater<int>());    ll ans = 0;         // Loop to find the minimum    // cost of removing elements    for (int i = 0; i < n; i++) {        ans += arr[i] * pow(2, i);    }Â
    return ans;}Â
// Driver Codeint main(){Â Â Â Â int n = 4;Â Â Â Â ll arr[n] = { 3, 1, 2, 3 };Â
    // Function Call    cout << removeElements(arr, n);} |
Java
// Java implementation to find the// minimum cost of removing all// elements from the arrayimport java.util.*;Â
class GFG{Â
// Reverse array in decreasing orderstatic 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;}Â
// Function to find the minimum// cost of removing elements from// the arraystatic long removeElements(long arr[],                           int n){         // Sorting in Increasing order    Arrays.sort(arr);    arr = reverse(arr);Â
    long ans = 0;Â
    // Loop to find the minimum    // cost of removing elements    for(int i = 0; i < n; i++)    {        ans += arr[i] * Math.pow(2, i);    }    return ans;}Â
// Driver Codepublic static void main(String[] args){Â Â Â Â int n = 4;Â Â Â Â long arr[] = { 3, 1, 2, 3 };Â
    // Function call    System.out.print(removeElements(arr, n));}}Â
// This code is contributed by amal kumar choubey |
Python3
# Python3 implementation to find the# minimum cost of removing all# elements from the arrayÂ
# Function to find the minimum# cost of removing elements from# the arraydef removeElements(arr, n):Â
    # Sorting in Increasing order    arr.sort(reverse = True)    ans = 0Â
    # Loop to find the minimum    # cost of removing elements    for i in range(n):        ans += arr[i] * pow(2, i)Â
    return ansÂ
# Driver Codeif __name__ == "__main__":Â Â Â Â Â Â Â Â Â n = 4Â Â Â Â arr = [ 3, 1, 2, 3 ]Â
    # Function call    print(removeElements(arr, n))     # This code is contributed by chitranayal |
C#
// C# implementation to find the// minimum cost of removing all// elements from the arrayusing System;Â
class GFG{Â
// Reverse array in decreasing orderstatic 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;}Â
// Function to find the minimum// cost of removing elements from// the arraystatic long removeElements(long []arr,                           int n){         // Sorting in Increasing order    Array.Sort(arr);    arr = reverse(arr);Â
    long ans = 0;Â
    // Loop to find the minimum    // cost of removing elements    for(int i = 0; i < n; i++)    {        ans += (long)(arr[i] * Math.Pow(2, i));    }    return ans;}Â
// Driver Codepublic static void Main(String[] args){Â Â Â Â int n = 4;Â Â Â Â long []arr = { 3, 1, 2, 3 };Â
    // Function call    Console.Write(removeElements(arr, n));}}Â
// This code is contributed by amal kumar choubey |
Javascript
<script>      // JavaScript implementation to find the      // minimum cost of removing all      // elements from the array      // Function to find the minimum      // cost of removing elements from      // the array      function removeElements(arr, n) {        // Sorting in Increasing order        arr.sort((a, b) => b - a);Â
        var ans = 0;Â
        // Loop to find the minimum        // cost of removing elements        for (var i = 0; i < n; i++) {          ans += arr[i] * Math.pow(2, i);        }        return ans;      }Â
      // Driver Code      var n = 4;      var arr = [3, 1, 2, 3];Â
      // Function call      document.write(removeElements(arr, n));</script> |
25
Time Complexity: O(N * log N)Â
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



