Minimum number of operations on an array to make all elements 0

Given an array arr[] of N integers and an integer cost, the task is to calculate the cost of making all the elements of the array 0 with the given operation. In a single operation, an index 0 ≤ i < N and an integer X > 0 can be chosen such that 0 ≤ i + X < N then elements can be updated as arr[i] = arr[i] – 1 and arr[i + X] = arr[i + X] + 1. If i + X ≥ N then only arr[i] will be updated but with twice the regular cost. Print the minimum cost required.

Examples:

Input: arr[] = {1, 2, 4, 5}, cost = 1
Output: 31
Move 1: i = 0, X = 3, arr[] = {0, 2, 4, 6} (cost = 1)
Moves 2 and 3: i = 1, X = 2, arr[] = {0, 0, 4, 8} (cost = 2)
Moves 4, 5, 6 and 7: i = 2, X = 1, arr[] = {0, 0, 0, 12} (cost = 4)
Move 8: i = 3, X > 0, arr[] = {0, 0, 0, 0} (cost = 24)
Total cost = 1 + 2 + 4 + 24 = 31



Input: arr[] = {1, 1, 0, 5}, cost = 2
Output: 32

Approach: To minimize the cost, for every index i always choose X such that i + X = N – 1 i.e. the last element then minimum cost can be calculated as:

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the minimum cost
int minCost(int n, int arr[], int cost)
{
    int sum = 0, totalCost = 0;
  
    // Sum of all the array elements
    // except the last element
    for (int i = 0; i < n - 1; i++)
        sum += arr[i];
  
    // Cost of making all the array elements 0
    // except the last element
    totalCost += cost * sum;
  
    // Update the last element
    arr[n - 1] += sum;
  
    // Cost of making the last element 0
    totalCost += (2 * cost * arr[n - 1]);
  
    return totalCost;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int cost = 1;
    cout << minCost(n, arr, cost);
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
public class GfG
{
  
    // Function to return the minimum cost 
    static int minCost(int n, int arr[], int cost) 
    
        int sum = 0, totalCost = 0
      
        // Sum of all the array elements 
        // except the last element 
        for (int i = 0; i < n - 1; i++) 
            sum += arr[i]; 
      
        // Cost of making all the array elements 0 
        // except the last element 
        totalCost += cost * sum; 
      
        // Update the last element 
        arr[n - 1] += sum; 
      
        // Cost of making the last element 0 
        totalCost += (2 * cost * arr[n - 1]); 
      
        return totalCost; 
    
  
    // Driver code 
    public static void main(String []args)
    {
          
        int arr[] = { 1, 2, 4, 5 }; 
        int n = arr.length; 
        int cost = 1
        System.out.println(minCost(n, arr, cost));
    }
}
  
// This code is contributed by Rituraj Jain
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to return the minimum cost 
def minCost(n, arr, cost): 
  
    Sum, totalCost = 0, 0
  
    # Sum of all the array elements 
    # except the last element 
    for i in range(0, n - 1): 
        Sum += arr[i] 
  
    # Cost of making all the array elements 0 
    # except the last element 
    totalCost += cost * Sum
  
    # Update the last element 
    arr[n - 1] += Sum
  
    # Cost of making the last element 0 
    totalCost += (2 * cost * arr[n - 1]) 
  
    return totalCost 
  
# Driver code 
if __name__ == "__main__":
  
    arr = [1, 2, 4, 5
    n = len(arr) 
    cost = 1
    print(minCost(n, arr, cost)) 
  
# This code is contributed by Rituraj Jain
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System ;
  
class GfG 
  
    // Function to return the minimum cost 
    static int minCost(int n, int []arr, int cost) 
    
        int sum = 0, totalCost = 0; 
      
        // Sum of all the array elements 
        // except the last element 
        for (int i = 0; i < n - 1; i++) 
            sum += arr[i]; 
      
        // Cost of making all the array elements 0 
        // except the last element 
        totalCost += cost * sum; 
      
        // Update the last element 
        arr[n - 1] += sum; 
      
        // Cost of making the last element 0 
        totalCost += (2 * cost * arr[n - 1]); 
      
        return totalCost; 
    
  
    // Driver code 
    public static void Main() 
    
          
        int []arr = { 1, 2, 4, 5 }; 
        int n = arr.Length; 
        int cost = 1; 
        Console.WriteLine(minCost(n, arr, cost)); 
    
  
// This code is contributed by Ryuga
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach
  
// Function to return the minimum cost
function minCost($n, $arr, $cost)
{
    $sum = 0;
    $totalCost = 0;
  
    // Sum of all the array elements
    // except the last element
    for ($i = 0; $i < ($n - 1); $i++)
        $sum += $arr[$i];
  
    // Cost of making all the array 
    // elements 0 except the last element
    $totalCost += $cost * $sum;
  
    // Update the last element
    $arr[$n - 1] += $sum;
  
    // Cost of making the last element 0
    $totalCost += (2 * $cost * $arr[$n - 1]);
  
    return $totalCost;
}
  
// Driver code
$arr = array( 1, 2, 4, 5 );
$n = sizeof($arr);
$cost = 1;
echo minCost($n, $arr, $cost);
  
// This code is contributed by ajit
?>
chevron_right

Output:
31

Time Complexity: O(n)





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : rituraj_jain, AnkitRai01, jit_t

Article Tags :
Practice Tags :