Given an array **arr[] **consisting of **N** non-negative integers, the task is to find the minimum number of subarrays that needs to be reduced by 1 such that all the array elements are equal to 0.

**Example:**

Input:arr[] = {1, 2, 3, 2, 1}Output:3Explanation:

Operation 1: {1, 2, 3, 2, 1} -> {0, 1, 2, 1, 0}

Operation 2: {0, 1, 2, 1, 0} -> {0, 0, 1, 0, 0}

Operation 3: {0, 0, 1, 0, 0} -> {0, 0, 0, 0, 0}

Input:arr[] = {5, 4, 3, 4, 4}Output:6Explanation:

{5, 4, 3, 4, 4} -> {4, 3, 2, 3, 3} -> {3, 2, 1, 2, 2} -> {2, 1, 0, 1, 1} -> {2, 1, 0, 0, 0} -> {1, 0, 0, 0, 0} -> {0, 0, 0, 0, 0}

**Approach:**

This can be optimally done by traversing the given array from index **0**, finding the answer up to index **i**, where **0 ≤ i < N**. If **arr[i] ≥ arr[i+1]**, then **(i + 1) ^{th}** element can eb included in every subarray operation of

**i**element, thus requiring no extra operations. If

^{th}**arr[i] < arr[i + 1]**, then

**(i + 1)**element can be included in every subarray operation of

^{th}**i**element and after all operations,

^{th}**arr[i+1]**becomes

**arr[i+1]-arr[i]**. Therefore, we need

**arr[i+1]-arr[i]**extra operations to reduce it zero.

Follow the below steps to solve the problem:

- Add the first element
**arr[0]**to**answer**as we need at least**arr[0]**to make the given array**0**. - Traverse over indices
**[1, N-1]**and for every element, check if it is greater than the previous element. If found to be true, add their difference to the**answer**. - Minimize subarray increments/decrements required to reduce all array elements to 0
- Minimum steps required to reduce all the elements of the array to zero
- Minimum decrements required such that sum of all adjacent pairs in an Array does not exceed K
- Find K that requires minimum increments or decrements of array elements to obtain a sequence of increasing powers of K
- Minimum no. of operations required to make all Array Elements Zero
- Minimum decrements to make integer A divisible by integer B
- Reduce every element of the array to it's half retaining the sum zero
- Maximums from array when the maximum decrements after every access
- Minimum number of given operations required to reduce the array to 0 element
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- Steps to reduce N to zero by subtracting its most significant digit at every step
- Minimize positive product of two given numbers by at most N decrements
- Minimum number of operations required to reduce N to 1
- Minimum number of operations required to reduce N to 0
- Reduce the array by deleting elements which are greater than all elements to its left
- Minimum steps to make sum and the product of all elements of array non-zero
- Minimum cost required to convert all Subarrays of size K to a single element
- Count subarrays with non-zero sum in the given Array
- Minimum count of increment of K size subarrays required to form a given Array
- Minimum increments of Non-Decreasing Subarrays required to make Array Non-Decreasing

Below is the implementation of above approach:

## C++

`// C++ Program to implement ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count the minimum ` `// number of subarrays that are ` `// required to be decremented by 1 ` `int` `min_operations(vector<` `int` `>& A) ` `{ ` ` ` `// Base Case ` ` ` `if` `(A.size() == 0) ` ` ` `return` `0; ` ` ` ` ` `// Initialize ans to first element ` ` ` `int` `ans = A[0]; ` ` ` ` ` `for` `(` `int` `i = 1; i < A.size(); i++) { ` ` ` ` ` `// For A[i] > A[i-1], operation ` ` ` `// (A[i] - A[i - 1]) is required ` ` ` `ans += max(A[i] - A[i - 1], 0); ` ` ` `} ` ` ` ` ` `// Return the answer ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `vector<` `int` `> A{ 1, 2, 3, 2, 1 }; ` ` ` ` ` `cout << min_operations(A) << ` `"\n"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to implement ` `// the above approach ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to count the minimum ` ` ` `// number of subarrays that are ` ` ` `// required to be decremented by 1 ` ` ` `static` `int` `min_operations(` `int` `A[], ` `int` `n) ` ` ` `{ ` ` ` `// Base Case ` ` ` `if` `(n == ` `0` `) ` ` ` `return` `0` `; ` ` ` ` ` `// Initializing ans to first element ` ` ` `int` `ans = A[` `0` `]; ` ` ` `for` `(` `int` `i = ` `1` `; i < n; i++) { ` ` ` ` ` `// For A[i] > A[i-1], operation ` ` ` `// (A[i] - A[i - 1]) is required ` ` ` `if` `(A[i] > A[i - ` `1` `]) { ` ` ` `ans += A[i] - A[i - ` `1` `]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the count ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `int` `A[] = { ` `1` `, ` `2` `, ` `3` `, ` `2` `, ` `1` `}; ` ` ` `System.out.println(min_operations(A, n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python

`# Python Program to implement ` `# the above approach ` ` ` `# Function to count the minimum ` `# number of subarrays that are ` `# required to be decremented by 1 ` `def` `min_operations(A): ` ` ` ` ` `# Base case ` ` ` `if` `len` `(A) ` `=` `=` `0` `: ` ` ` `return` `0` ` ` ` ` `# Initializing ans to first element ` ` ` `ans ` `=` `A[` `0` `] ` ` ` `for` `i ` `in` `range` `(` `1` `, ` `len` `(A)): ` ` ` ` ` `if` `A[i] > A[i` `-` `1` `]: ` ` ` `ans ` `+` `=` `A[i]` `-` `A[i` `-` `1` `] ` ` ` ` ` `return` `ans ` ` ` ` ` `# Driver Code ` `A ` `=` `[` `1` `, ` `2` `, ` `3` `, ` `2` `, ` `1` `] ` `print` `(min_operations(A)) ` |

*chevron_right*

*filter_none*

**Output:**

3

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.