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:
- 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++
#include <bits/stdc++.h>
using namespace std;
int minCost( int n, int arr[], int cost)
{
int sum = 0, totalCost = 0;
for ( int i = 0; i < n - 1; i++)
sum += arr[i];
totalCost += cost * sum;
arr[n - 1] += sum;
totalCost += (2 * cost * arr[n - 1]);
return totalCost;
}
int main()
{
int arr[] = { 1, 2, 4, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
int cost = 1;
cout << minCost(n, arr, cost);
}
|
Java
public class GfG
{
static int minCost( int n, int arr[], int cost)
{
int sum = 0 , totalCost = 0 ;
for ( int i = 0 ; i < n - 1 ; i++)
sum += arr[i];
totalCost += cost * sum;
arr[n - 1 ] += sum;
totalCost += ( 2 * cost * arr[n - 1 ]);
return totalCost;
}
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));
}
}
|
Python3
def minCost(n, arr, cost):
Sum , totalCost = 0 , 0
for i in range ( 0 , n - 1 ):
Sum + = arr[i]
totalCost + = cost * Sum
arr[n - 1 ] + = Sum
totalCost + = ( 2 * cost * arr[n - 1 ])
return totalCost
if __name__ = = "__main__" :
arr = [ 1 , 2 , 4 , 5 ]
n = len (arr)
cost = 1
print (minCost(n, arr, cost))
|
C#
using System ;
class GfG
{
static int minCost( int n, int []arr, int cost)
{
int sum = 0, totalCost = 0;
for ( int i = 0; i < n - 1; i++)
sum += arr[i];
totalCost += cost * sum;
arr[n - 1] += sum;
totalCost += (2 * cost * arr[n - 1]);
return totalCost;
}
public static void Main()
{
int []arr = { 1, 2, 4, 5 };
int n = arr.Length;
int cost = 1;
Console.WriteLine(minCost(n, arr, cost));
}
}
|
PHP
<?php
function minCost( $n , $arr , $cost )
{
$sum = 0;
$totalCost = 0;
for ( $i = 0; $i < ( $n - 1); $i ++)
$sum += $arr [ $i ];
$totalCost += $cost * $sum ;
$arr [ $n - 1] += $sum ;
$totalCost += (2 * $cost * $arr [ $n - 1]);
return $totalCost ;
}
$arr = array ( 1, 2, 4, 5 );
$n = sizeof( $arr );
$cost = 1;
echo minCost( $n , $arr , $cost );
?>
|
Javascript
<script>
function minCost(n, arr, cost)
{
let sum = 0, totalCost = 0;
for (let i = 0; i < n - 1; i++)
sum += arr[i];
totalCost += cost * sum;
arr[n - 1] += sum;
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>
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Last Updated :
15 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...