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

11 May, 2021
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 = 31Input: 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:  
Store the sum of the elements from arr[0] to arr[n – 2] in sum then update totalCost = cost * sum and arr[n – 1] = arr[n – 1] + sum.
Now the cost of making all the elements 0 except the last one has been calculated. And the cost of making the last element 0 can be calculated as totalCost = totalCost + (2 * cost * arr[n – 1]).
Below is the implementation of the above approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the minimum costint 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 codeint main(){    int arr[] = { 1, 2, 4, 5 };    int n = sizeof(arr) / sizeof(arr[0]);    int cost = 1;    cout << minCost(n, arr, cost);}
// Java implementation of the approachpublic 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
# Python3 implementation of the approach # Function to return the minimum costdef 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 codeif __name__ == "__main__":     arr = [1, 2, 4, 5]    n = len(arr)    cost = 1    print(minCost(n, arr, cost)) # This code is contributed by Rituraj Jain
// C# implementation of the approachusing 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
<?php// PHP implementation of the approach // Function to return the minimum costfunction 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?>
<script>    // Javascript implementation of the approach         // Function to return the minimum cost    function minCost(n, arr, cost)    {        let sum = 0, totalCost = 0;               // Sum of all the array elements        // except the last element        for (let 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;    }         let arr = [ 1, 2, 4, 5 ];    let n = arr.length;    let cost = 1;    document.write(minCost(n, arr, cost));     </script>
31
 
Time Complexity: O(n) 
rituraj_jain
ankthon
jit_t
suresh07
array-traversal-question
Algorithms
Arrays
Arrays
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
SDE SHEET - A Complete Guide for SDE Preparation
What is Hashing | A Complete Tutorial
Understanding Time Complexity with Simple Examples
CPU Scheduling in Operating Systems
Arrays in Java
Write a program to reverse an array or string
Maximum and minimum of an array using minimum number of comparisons
Largest Sum Contiguous Subarray
Arrays in C/C++