Minimum Cost to Remove All Array Elements
Last Updated :
14 Jan, 2024
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));
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Minimum Value X to Delete All Arrays Given 2d array A[][] of size N*M. Your Task is to choose the minimum possible number X to perform the following operation. In one operation choose any array i from N arrays and delete the first element, if it is less than X, and increase X by 1. Keep performing this operation until the array becomes
12 min read
Minimum delete operations to make all elements of array same Given an array of n elements such that elements may repeat. We can delete any number of elements from the array. The task is to find a minimum number of elements to be deleted from the array to make it equal.Examples: Input: arr[] = {4, 3, 4, 4, 2, 4} Output: 2 After deleting 2 and 3 from array, arr
10 min read
Minimize the sum of MEX by removing all elements of array Given an array of integers arr[] of size N. You can perform the following operation N times: Pick any index i, and remove arr[i] from the array and add MEX(arr[]) i.e., Minimum Excluded of the array arr[] to your total score. Your task is to minimize the total score. Examples: Input: N = 8, arr[] =
7 min read
Minimum cost to make array size 1 by removing larger of pairs Given an array of n integers. We need to reduce size of array to one. We are allowed to select a pair of integers and remove the larger one of these two. This decreases the array size by 1. Cost of this operation is equal to value of smallest one. Find out minimum sum of costs of operations needed t
3 min read
Remove minimum elements from array so that max <= 2 * min Given an array arr, the task is to remove minimum number of elements such that after their removal, max(arr) <= 2 * min(arr). Examples: Input: arr[] = {4, 5, 3, 8, 3} Output: 1 Remove 8 from the array. Input: arr[] = {1, 2, 3, 4} Output: 1 Remove 1 from the array. Approach: Let us fix each value
6 min read