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 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); } |
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 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 ?> |
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 neveropen!