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 = 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++
// 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
// 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 |
Python3
# 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 |
C#
// 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 |
PHP
<?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?> |
Javascript
<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
Complexity Analysis:
- Time Complexity: O(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!



