Open In App

Minimum Cost to Remove All Array Elements

Last Updated : 14 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array A[] of length N. Then your task is to find minimum cost to vanish all elements by performing any of the two types of operations given below along with cost:

  • Remove first element let say X and remove next X number of elements as well, this operation costs nothing.
    • Note: This operation can only be performed if (X + 1) elements exist.
  • Remove first element at cost 1.

Examples:

Input: N = 7, A[] = {3, 3, 4, 5, 2, 6, 1}
Output: 0
Explanation: Initially, A[] = {3, 3, 4, 5, 2, 6, 1}

  • Performing first operation: Remove A1 = 3 and also remove next 3 elements with cost 0. Updated A[] becomes: {2, 6, 1}
  • Performing first operation: Remove A1 = 2 and also remove next 2 elements with cost 0. Updated A[] becomes: {}

It can be seen that A[] has no elements left. Then total cost is 0.

Input: N= 5, A[] = {1, 2, 3, 4, 5}
Output: 2
Explanation: Initially, A[] = {1, 2, 3, 4, 5}

  • Performing second operation: Remove A1 = 1 with cost 1. Updated A[] is: {2, 3, 4, 5}
  • Performing first operation: Remove A1 = 2 and also remove next 2 elements with cost 0. Updated A[] becomes: {5}
  • Performing second operation: Remove A1 = 5 with cost 1. Updated A[] becomes: {}

Total cost to remove all the elements is 2.

Approach: Implement the idea below to solve the problem.

We have number of choices in performing operation. Thus, the problem can be solved using Dynamic Programming approach. In DP[] array following facts will take place:

  • DP[i] = j represents minimum cost of removing all array elements from Nth index to ith index.
  • Transition: DP[i] = min(DP[i + 1] + 1, DP[i + arr[i] + 1])

Steps were taken to solve the problem:

  • Declaring DP[N + 1] array.
  • Run a for loop from i = N – 1 to 0 and follow below mentioned steps under the scope of loop:
    • DP[i] = DP[i + 1] + 1
    • If (i + A[i] < N)
      • DP[i] = min(DP[i], DP[i + A[i] + 1])
  • Return DP[0].

Code to implement the approach:

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to Minimum cost for removing
// all array elements by performing following operations
int minimumCost(int A[], int N)
{
 
    // Dp array initalized with 0
    vector<int> dp(N + 1, 0);
 
    // iterating on states
    for (int i = N - 1; i >= 0; i--) {
 
        // Performing 2nd operation
        dp[i] = dp[i + 1] + 1;
 
        // performing 1st operation
        if (i + A[i] < N)
            dp[i] = min(dp[i + A[i] + 1], dp[i]);
    }
 
    // returing answer
    return dp[0];
}
 
// Driver Code
int32_t main()
{
 
    // Input 1
    int N = 7;
    int A[] = { 3, 3, 4, 5, 2, 6, 1 };
 
    // Function Call
    cout << minimumCost(A, N) << endl;
    return 0;
}


Java




import java.util.Arrays;
 
public class MinimumCostRemoval {
 
    // Function to find minimum cost for removing all array elements
    // by performing the given operations
    static int minimumCost(int[] A, int N) {
 
        // Dp array initialized with 0
        int[] dp = new int[N + 1];
 
        // Iterating on states
        for (int i = N - 1; i >= 0; i--) {
 
            // Performing the 2nd operation
            dp[i] = dp[i + 1] + 1;
 
            // Performing the 1st operation
            if (i + A[i] < N)
                dp[i] = Math.min(dp[i + A[i] + 1], dp[i]);
        }
 
        // Returning the answer
        return dp[0];
    }
 
    // Driver Code
    public static void main(String[] args) {
 
        // Input 1
        int N = 7;
        int[] A = {3, 3, 4, 5, 2, 6, 1};
 
        // Function Call
        System.out.println(minimumCost(A, N));
    }
}
 
 
// This code is contributed by akshitaguprzj3


Python3




def minimum_cost(A, N):
    # Dp array initialized with 0
    dp = [0] * (N + 1)
 
    # Iterating on states
    for i in range(N - 1, -1, -1):
        # Performing 2nd operation
        dp[i] = dp[i + 1] + 1
 
        # Performing 1st operation
        if i + A[i] < N:
            dp[i] = min(dp[i + A[i] + 1], dp[i])
 
    # Returning answer
    return dp[0]
 
 
# Driver Code
if __name__ == "__main__":
    # Input 1
    N = 7
    A = [3, 3, 4, 5, 2, 6, 1]
 
    # Function Call
    print(minimum_cost(A, N))


C#




using System;
 
class Program
{
    // Function to Minimum cost for removing
    // all array elements by performing following operations
    static int MinimumCost(int[] A, int N)
    {
        // Dp array initialized with 0
        int[] dp = new int[N + 1];
 
        // iterating on states
        for (int i = N - 1; i >= 0; i--)
        {
            // Performing 2nd operation
            dp[i] = dp[i + 1] + 1;
 
            // performing 1st operation
            if (i + A[i] < N)
                dp[i] = Math.Min(dp[i + A[i] + 1], dp[i]);
        }
 
        // returning answer
        return dp[0];
    }
 
    // Driver Code
    static void Main(string[] args)
    {
        // Input 1
        int N = 7;
        int[] A = { 3, 3, 4, 5, 2, 6, 1 };
 
        // Function Call
        Console.WriteLine(MinimumCost(A, N));
    }
}


Javascript




// Function to Minimum cost for removing
// all array elements by performing following operations
function minimumCost(A, N) {
    // Dp array initialized with 0
    let dp = new Array(N + 1).fill(0);
 
    // iterating on states
    for (let i = N - 1; i >= 0; i--) {
        // Performing 2nd operation
        dp[i] = dp[i + 1] + 1;
 
        // performing 1st operation
        if (i + A[i] < N)
            dp[i] = Math.min(dp[i + A[i] + 1], dp[i]);
    }
 
    // returing answer
    return dp[0];
}
 
// Driver Code
// Input
let N = 7;
let A = [3, 3, 4, 5, 2, 6, 1];
 
// Function Call
console.log(minimumCost(A, N));


Output

0

Time Complexity: O(N)
Auxiliary Space: O(N)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads