Minimum operations to make all Array elements 0 by MEX replacement Last Updated : 31 Oct, 2023 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Given an array of N integers. You can perform an operation that selects a contiguous subarray and replaces all its elements with the MEX (smallest non-negative integer that does not appear in that subarray), the task is to find the minimum number of operations required to make all the elements of the array 0. Examples: Input: N = 4, arr = {3, 0, 4, 5}Output: 2Explanation: First, you have to choose l = 0, r = 0, and in the second operation, you have to choose l = 2, r = 3. Input: N = 4, arr = {0, 0, 0, 0}Output: 0 Explanation: All the elements are already 0. Approach: To solve the problem follow the below observations: We can do the question by just some observation, and the observation is given below: Think if there is not any 0 present then the MEX of the complete array will be 0. So we can choose the complete array and make it as 0 in just one operation.If all elements are 0 then no need to do any operation.Now if 0 is present at only corners then what we can do, we can choose all the elements leaving the corner 0 and the MEX of them will be 0 and make them 0 in one operation.Now the last case occur when there can by 0's random in the array. So in that can what we can do we can select the complete array and MEX of the complete array will be some non-zero number as 0 is present in the array. So we will change whole array to that MEX in one operation and after that we will again select the complete array and now the MEX will be 0. So we will replace every element with 0 in second operation. Follow the steps to solve the problem: So we will find out the subarrays which do not contain zero's.If the subarrays are 0 it means all the elements are 0 so, the answer will be 0.If we found only 1 subarray it means either no 0 is present or 0 is present at only the ends of the array then the operation will be 1.Otherwise, the answer will be 2.Below is the implementation for the above approach: C++ // C++ code for the above approach: #include <bits/stdc++.h> using namespace std; int arrayOperations(int N, vector<int>& arr) { // Variable to count subarrays int subarray = 0; bool hai = false; // Loop to count subarrays for (int i = 0; i < N; i++) { // If non zero element occur // and hai==true it means // previous element was 0 // so increse the subarrays count if (arr[i] != 0) { if (!hai) subarray++; hai = true; } else { hai = false; } } if (subarray == 0 or subarray == 1) return subarray; return 2; } // Drivers code int main() { int N = 4; vector<int> arr = { 3, 0, 4, 5 }; // Function Call cout << arrayOperations(N, arr); return 0; } Java // Java code for the above approach: import java.util.ArrayList; import java.util.List; public class GFG { public static int arrayOperations(int N, List<Integer> arr) { // Variable to count subarrays int subarray = 0; boolean hai = false; // Loop to count subarrays for (int i = 0; i < N; i++) { // If non-zero element occurs // and hai==true it means // the previous element was 0 // so increase the subarrays count if (arr.get(i) != 0) { if (!hai) subarray++; hai = true; } else { hai = false; } } if (subarray == 0 || subarray == 1) return subarray; return 2; } // Driver code public static void main(String[] args) { int N = 4; List<Integer> arr = new ArrayList<>(List.of(3, 0, 4, 5)); // Function call System.out.println(arrayOperations(N, arr)); } } // This code is contributed by rambabuguphka Python3 # Python code for the above approach: def arrayOperations(N, arr): # Variable to count subarrays subarray = 0 hai = False # Loop to count subarrays for i in range(N): # If non-zero element occurs # and hai is False, it means # the previous element was 0 # so increase the subarrays count if arr[i] != 0: if not hai: subarray += 1 hai = True else: hai = False if subarray == 0 or subarray == 1: return subarray return 2 # Driver code N = 4 # Array with elements 3, 0, 4, 5 arr = [3, 0, 4, 5] # Function call print(arrayOperations(N, arr)) # This code is contributed by Adi C# using System; using System.Collections.Generic; class GFG { static int ArrayOperations(int N, List<int> arr) { int subarray = 0; bool hai = false; for (int i = 0; i < N; i++) { if (arr[i] != 0) { if (!hai) subarray++; hai = true; } else { hai = false; } } if (subarray == 0 || subarray == 1) return subarray; return 2; } static void Main(string[] args) { int N = 4; List<int> arr = new List<int> { 3, 0, 4, 5 }; Console.WriteLine(ArrayOperations(N, arr)); } } // This code is contributed by Aditi Tyagi JavaScript function arrayOperations(N, arr) { let subarray = 0; let hai = false; // Nikunj Sonigara for (let i = 0; i < N; i++) { if (arr[i] !== 0) { if (!hai) { subarray++; } hai = true; } else { hai = false; } } if (subarray === 0 || subarray === 1) { return subarray; } return 2; } const N = 4; const arr = [3, 0, 4, 5]; // Function Call console.log(arrayOperations(N, arr)); Output2Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Minimum operations to make the MEX of the given set equal to x shubhamrajput6156 Follow Improve Article Tags : DSA Arrays subarray Practice Tags : Arrays Similar Reads MEX (Minimum Excluded) in Competitive Programming MEX of a sequence or an array is the smallest non-negative integer that is not present in the sequence.Note: The MEX of an array of size N cannot be greater than N since the MEX of an array is the smallest non-negative integer not present in the array and array having size N can only cover integers 15+ min read Minimum operations to make all Array elements 0 by MEX replacement Given an array of N integers. You can perform an operation that selects a contiguous subarray and replaces all its elements with the MEX (smallest non-negative integer that does not appear in that subarray), the task is to find the minimum number of operations required to make all the elements of th 5 min read Minimum operations to make the MEX of the given set equal to x Given a set of n integers, perform minimum number of operations (you can insert/delete elements into/from the set) to make the MEX of the set equal to x (that is given). Note:- The MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set 6 min read Find the Prefix-MEX Array for given Array Given an array A[] of N elements, the task is to create a Prefix-MEX array for this given array. Prefix-MEX array B[] of an array A[] is created such that MEX of A[0] till A[i] is B[i]. MEX of an array refers to the smallest missing non-negative integer of the array. Examples: Input: A[] = {1, 0, 2, 13 min read Rearrange array elements to maximize the sum of MEX of all prefix arrays Given an array arr[] of size N, the task is to rearrange the array elements such that the sum of MEX of all prefix arrays is the maximum possible. Note: MEX of a sequence is the minimum non-negative number not present in the sequence. Examples: Input: arr[] = {2, 0, 1}Output: 0, 1, 2Explanation:Sum 7 min read Maximum MEX from all subarrays of length K Given an array arr[] consisting of N distinct integers and an integer K, the task is to find the maximum MEX from all subarrays of length K. The MEX is the smallest positive integer that is not present in the array. Examples: Input: arr[] = {3, 2, 1, 4}, K = 2Output: 3Explanation:All subarrays havin 8 min read Minimum operations for same MEX Given an array 'arr' consisting of N arrays, each of size M, the task is to find the minimum number of operations required to make the Minimum Excluded Element (MEX) the same for all N arrays. You can perform the following task zero or more times: Choose one of the N arrays.Choose some non-negative 8 min read Maximize MEX by adding or subtracting K from Array elements Given an arr[] of size N and an integer, K, the task is to find the maximum possible value of MEX by adding or subtracting K any number of times from the array elements. MEX is the minimum non-negative integer that is not present in the array Examples: Input: arr[]={1, 3, 4}, K = 2Output: 2Explanati 7 min read MEX of generated sequence of N+1 integers where ith integer is XOR of (i-1) and K Given two integers N and K, generate a sequence of size N+1 where the ith element is (i-1)âK, the task is to find the MEX of this sequence. Here, the MEX of a sequence is the smallest non-negative integer that does not occur in the sequence. Examples: Input: N = 7, K=3Output: 8Explanation: Sequence 12 min read Maximize sum of MEX values of each node in an N-ary Tree Given an N-ary tree rooted at 1, the task is to assign values from the range [0, N - 1] to each node in any order such that the sum of MEX values of each node in the tree is maximized and print the maximum possible sum of MEX values of each node in the tree. The MEX value of node V is defined as the 9 min read Like