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: 3
Explanation:
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: 6
Explanation:
{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 be included in every subarray operation of ith element, thus requiring no extra operations. If arr[i] < arr[i + 1], then (i + 1)th element can be included in every subarray operation of ith element and after all operations, 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.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int min_operations(vector< int >& A)
{
if (A.size() == 0)
return 0;
int ans = A[0];
for ( int i = 1; i < A.size(); i++) {
ans += max(A[i] - A[i - 1], 0);
}
return ans;
}
int main()
{
vector< int > A{ 1, 2, 3, 2, 1 };
cout << min_operations(A) << "\n" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int min_operations( int A[], int n)
{
if (n == 0 )
return 0 ;
int ans = A[ 0 ];
for ( int i = 1 ; i < n; i++) {
if (A[i] > A[i - 1 ]) {
ans += A[i] - A[i - 1 ];
}
}
return ans;
}
public static void main(String[] args)
{
int n = 5 ;
int A[] = { 1 , 2 , 3 , 2 , 1 };
System.out.println(min_operations(A, n));
}
}
|
Python
def min_operations(A):
if len (A) = = 0 :
return 0
ans = A[ 0 ]
for i in range ( 1 , len (A)):
if A[i] > A[i - 1 ]:
ans + = A[i] - A[i - 1 ]
return ans
A = [ 1 , 2 , 3 , 2 , 1 ]
print (min_operations(A))
|
C#
using System;
class GFG{
static int min_operations( int [] A, int n)
{
if (n == 0)
return 0;
int ans = A[0];
for ( int i = 1; i < n; i++)
{
if (A[i] > A[i - 1])
{
ans += A[i] - A[i - 1];
}
}
return ans;
}
public static void Main()
{
int n = 5;
int [] A = { 1, 2, 3, 2, 1 };
Console.WriteLine(min_operations(A, n));
}
}
|
Javascript
<script>
function min_operations(A)
{
if (A.length == 0)
return 0;
let ans = A[0];
for (let i = 1; i < A.length; i++)
{
ans += Math.max(A[i] - A[i - 1], 0);
}
return ans;
}
let A = [ 1, 2, 3, 2, 1 ];
document.write(min_operations(A));
</script>
|
Output:
3
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Topic: Subarrays, Subsequences, and Subsets in Array