Given an array arr[] of integers, find the minimum number of operations required to make all its elements 0.
In one operation,
- Choose a contiguous subarray containing only non-zero elements and replace all its elements with 0.
- The operation can be performed only if the array contains at least one 0.
Examples:
Input: arr[] = [3, 0, 4, 5]
Output: 2
Explanation: There are two contiguous groups of non-zero elements: [3] and [4, 5]. Each group can be replaced with 0 in one operation.Input: arr[] = [10, 4, 9, 6, 10, 10, 4, 4]
Output: -1
Explanation: The array does not contain 0, so no operation can be performed.
Table of Content
[Naive Approach] Repeatedly Find Non-Zero Subarrays - O(n ^ 2) Time and O(1) Space
The idea is to repeatedly find a contiguous subarray of non-zero elements and replace it with 0. If the array has no zero, return -1 because no operation can be performed.
Working of Approach:
- Check whether the array contains at least one zero. If not, return -1.
- Find the first non-zero element in the array.
- Replace the consecutive non-zero elements starting from that index with 0.
- Increment the operation count after processing each subarray.
- Repeat until all elements become zero and return the count.
#include <iostream>
#include <vector>
using namespace std;
// Count operations by repeatedly removing non-zero groups.
int arrayOperations(vector<int> &arr)
{
int n = arr.size();
// Check whether the array contains at least one zero.
bool hasZero = false;
for (int i = 0; i < n; i++)
{
if (arr[i] == 0)
{
hasZero = true;
break;
}
}
// Return -1 if no zero is present.
if (!hasZero)
return -1;
int cnt = 0;
// Repeat until all elements become zero.
while (true)
{
// Find the first non-zero element.
int start = -1;
for (int i = 0; i < n; i++)
{
if (arr[i] != 0)
{
start = i;
break;
}
}
// Stop if all elements are zero.
if (start == -1)
break;
// Replace consecutive non-zero elements with zero.
for (int i = start; i < n && arr[i] != 0; i++)
{
arr[i] = 0;
}
// Count the current operation.
cnt++;
}
return cnt;
}
int main()
{
vector<int> arr = {3, 0, 4, 5};
cout << arrayOperations(arr);
return 0;
}
import java.util.Arrays;
public class GFG {
// Count operations by repeatedly removing non-zero
// groups.
public static int arrayOperations(int[] arr)
{
int n = arr.length;
// Check whether the array contains at least one
// zero.
boolean hasZero = false;
for (int i = 0; i < n; i++) {
if (arr[i] == 0) {
hasZero = true;
break;
}
}
// Return -1 if no zero is present.
if (!hasZero)
return -1;
int cnt = 0;
// Repeat until all elements become zero.
while (true) {
// Find the first non-zero element.
int start = -1;
for (int i = 0; i < n; i++) {
if (arr[i] != 0) {
start = i;
break;
}
}
// Stop if all elements are zero.
if (start == -1)
break;
// Replace consecutive non-zero elements with
// zero.
for (int i = start; i < n && arr[i] != 0; i++) {
arr[i] = 0;
}
// Count the current operation.
cnt++;
}
return cnt;
}
public static void main(String[] args)
{
int[] arr = { 3, 0, 4, 5 };
System.out.println(arrayOperations(arr));
}
}
def arrayOperations(arr):
n = len(arr)
# Check whether the array contains at least one zero.
hasZero = False
for i in range(n):
if arr[i] == 0:
hasZero = True
break
# Return -1 if no zero is present.
if not hasZero:
return -1
cnt = 0
# Repeat until all elements become zero.
while True:
# Find the first non-zero element.
start = -1
for i in range(n):
if arr[i]!= 0:
start = i
break
# Stop if all elements are zero.
if start == -1:
break
# Replace consecutive non-zero elements with zero.
for i in range(start, n):
if arr[i] == 0:
break
arr[i] = 0
# Count the current operation.
cnt += 1
return cnt
if __name__ == '__main__':
arr = [3, 0, 4, 5]
print(arrayOperations(arr))
using System;
public class GFG {
// Count operations by repeatedly removing non-zero
// groups.
public static int arrayOperations(int[] arr)
{
int n = arr.Length;
// Check whether the array contains at least one
// zero.
bool hasZero = false;
for (int i = 0; i < n; i++) {
if (arr[i] == 0) {
hasZero = true;
break;
}
}
// Return -1 if no zero is present.
if (!hasZero)
return -1;
int cnt = 0;
// Repeat until all elements become zero.
while (true) {
// Find the first non-zero element.
int start = -1;
for (int i = 0; i < n; i++) {
if (arr[i] != 0) {
start = i;
break;
}
}
// Stop if all elements are zero.
if (start == -1)
break;
// Replace consecutive non-zero elements with
// zero.
for (int i = start; i < n && arr[i] != 0; i++) {
arr[i] = 0;
}
// Count the current operation.
cnt++;
}
return cnt;
}
public static void Main()
{
int[] arr = { 3, 0, 4, 5 };
Console.WriteLine(arrayOperations(arr));
}
}
function arrayOperations(arr)
{
let n = arr.length;
// Check whether the array contains at least one zero.
let hasZero = false;
for (let i = 0; i < n; i++) {
if (arr[i] === 0) {
hasZero = true;
break;
}
}
// Return -1 if no zero is present.
if (!hasZero)
return -1;
let cnt = 0;
// Repeat until all elements become zero.
while (true) {
// Find the first non-zero element.
let start = -1;
for (let i = 0; i < n; i++) {
if (arr[i] !== 0) {
start = i;
break;
}
}
// Stop if all elements are zero.
if (start === -1)
break;
// Replace consecutive non-zero elements with zero.
for (let i = start; i < n && arr[i] !== 0; i++) {
arr[i] = 0;
}
// Count the current operation.
cnt++;
}
return cnt;
}
// Driver Code
let arr = [ 3, 0, 4, 5 ];
console.log(arrayOperations(arr));
Output
2
[Expected Approach] Count Contiguous Non-Zero Groups - O(n) Time and O(1) Space
The idea is to count every contiguous group of non-zero elements as one operation. If the array does not contain any zero, return -1 because no operation is allowed.
Working of Approach:
- Traverse the array and check whether it contains at least one zero.
- Count a new group when the current element is non-zero and starts a new contiguous segment.
- A new group starts at index 0 or immediately after a zero.
- Continue the traversal without modifying the array.
- Return -1 if no zero exists; otherwise, return the total number of groups.
Let us understand with an example:
Input: arr[] = [3, 0, 4, 5]
- Initialize cnt = 0 and hasZero = false.
- At index 0, arr[0] = 3 is non-zero and starts a group, so cnt = 1.
- At index 1, arr[1] = 0, so set hasZero = true.
- At index 2, arr[2] = 4 is non-zero and follows zero, so cnt = 2.
- At index 3, arr[3] = 5 is non-zero but belongs to the same group, so cnt remains 2. Since a zero is present, return 2.
#include <iostream>
#include <vector>
using namespace std;
// Count each contiguous group of non-zero elements as one operation.
int arrayOperations(vector<int> &arr)
{
int cnt = 0;
bool hasZero = false;
for (int i = 0; i < arr.size(); i++)
{
if (arr[i] == 0)
{
hasZero = true;
}
else if (i == 0 || arr[i - 1] == 0)
{
cnt++;
}
}
// Return -1 if no zero is present, as no operation can be performed.
if (!hasZero)
{
return -1;
}
return cnt;
}
int main()
{
vector<int> arr = {3, 0, 4, 5};
cout << arrayOperations(arr);
return 0;
}
public class GFG {
// Count each contiguous group of non-zero elements as
// one operation.
public static int arrayOperations(int[] arr)
{
int cnt = 0;
boolean hasZero = false;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) {
hasZero = true;
}
else if (i == 0 || arr[i - 1] == 0) {
cnt++;
}
}
// Return -1 if no zero is present, as no operation
// can be performed.
if (!hasZero) {
return -1;
}
return cnt;
}
public static void main(String[] args)
{
int[] arr = { 3, 0, 4, 5 };
System.out.println(arrayOperations(arr));
}
}
def arrayOperations(arr):
# Count each contiguous group of non-zero elements as one operation.
cnt = 0
hasZero = False
for i in range(len(arr)):
if arr[i] == 0:
hasZero = True
elif i == 0 or arr[i - 1] == 0:
cnt += 1
# Return -1 if no zero is present, as no operation can be performed.
if not hasZero:
return -1
return cnt
if __name__ == '__main__':
arr = [3, 0, 4, 5]
print(arrayOperations(arr))
using System;
public class GFG {
// Count each contiguous group of non-zero elements as
// one operation.
public static int arrayOperations(int[] arr)
{
int cnt = 0;
bool hasZero = false;
for (int i = 0; i < arr.Length; i++) {
if (arr[i] == 0) {
hasZero = true;
}
else if (i == 0 || arr[i - 1] == 0) {
cnt++;
}
}
// Return -1 if no zero is present, as no operation
// can be performed.
if (!hasZero) {
return -1;
}
return cnt;
}
public static void Main()
{
int[] arr = { 3, 0, 4, 5 };
Console.WriteLine(arrayOperations(arr));
}
}
function arrayOperations(arr)
{
// Count each contiguous group of non-zero elements as
// one operation.
let cnt = 0;
let hasZero = false;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === 0) {
hasZero = true;
}
else if (i === 0 || arr[i - 1] === 0) {
cnt++;
}
}
// Return -1 if no zero is present, as no operation can
// be performed.
if (!hasZero) {
return -1;
}
return cnt;
}
// Driver Code
const arr = [ 3, 0, 4, 5 ];
console.log(arrayOperations(arr));
Output
2