Minimum operations to make the MEX of the given set equal to x
Last Updated :
09 Nov, 2023
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 {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0.
Examples :
Input : n = 5, x = 3
0 4 5 6 7
Output : 2
The MEX of the set {0, 4, 5, 6, 7} is 1 which is
not equal to 3. So, we should add 1 and 2 to the
set. After adding 1 and 2, the set becomes
{0, 1, 2, 4, 5, 6, 7} and 3 is the minimum
non-negative integer that doesn't exist in it.
So, the MEX of this set is 3 which is equal to
x i.e. 3. So, the output of this example is 2
as we inserted 1 and 2 in the set.Input : n = 1, x = 0
1
Output : 0
In this example, the MEX of the given set {1}
is already 0. So, we do not need to perform
any operation. So, the output is 0.
Approach: The approach is to see that in the final set all the elements less than x should exist, x shouldn't exist and any element greater than x doesn't matter. So, we will count the number of elements less than x that don't exist in the initial set and add this to the answer. If x exists we will add 1 to the answer because x should be removed.
Below is the implementation of above approach:
C++
// CPP program to perform minimal number
// of operations to make the MEX of the
// set equal to the given number x.
#include <bits/stdc++.h>
using namespace std;
// function to find minimum number of
// operations required
int minOpeartions(int arr[], int n, int x)
{
int k = x, i = 0;
while (n--) {
// if the element is less than x.
if (arr[n] < x)
k--;
// if the element equals to x.
if (arr[n] == x)
k++;
}
return k;
}
// driver function
int main()
{
int arr[] = { 0, 4, 5, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 3;
// output
cout << minOpeartions(arr, n, x) << endl;
}
Java
// Java program to perform minimal number
// of operations to make the MEX of the
// set equal to the given number x.
import java.io.*;
class GFG {
// function to find minimum number of
// operations required
static int minOpeartions(int arr[], int n, int x)
{
int k = x, i = 0;
n--;
while (n > -1) {
// if the element is less than x.
if (arr[n] < x)
k--;
// if the element equals to x.
if (arr[n] == x)
k++;
n--;
}
return k;
}
// driver function
public static void main(String args[])
{
int arr[] = { 0, 4, 5, 6, 7 };
int n = arr.length;
int x = 3;
// output
System.out.println(minOpeartions(arr, n, x));
}
}
/* This code is contributed by Nikita Tiwari.*/
Python
# Python 3 program to perform minimal number
# of operations to make the MEX of the
# set equal to the given number x.
# function to find minimum number of
# operations required
def minOpeartions(arr, n, x) :
k = x
i = 0
n = n-1
while (n>-1) :
# if the element is less than x.
if (arr[n] < x) :
k = k - 1
# if the element equals to x.
if (arr[n] == x) :
k = k + 1
n = n - 1
return k
# driver function
arr = [ 0, 4, 5, 6, 7 ]
n = len(arr)
x = 3
# output
print( minOpeartions(arr, n, x))
# This code is contributed by Nikita Tiwari.
C#
// C# program to perform minimal number
// of operations to make the MEX of the
// set equal to the given number x.
using System;
class GFG {
// function to find minimum number
// of operations required
static int minOpeartions(int[] arr,
int n, int x)
{
int k = x;
n--;
while (n > -1) {
// if the element is less
// than x.
if (arr[n] < x)
k--;
// if the element equals
// to x.
if (arr[n] == x)
k++;
n--;
}
return k;
}
// driver function
public static void Main()
{
int[] arr = { 0, 4, 5, 6, 7 };
int n = arr.Length;
int x = 3;
// output
Console.WriteLine(
minOpeartions(arr, n, x));
}
}
// This code is contributed by vt_m.
JavaScript
<script>
// Javascript program to perform minimal number
// of operations to make the MEX of the
// set equal to the given number x.
// function to find minimum number of
// operations required
function minOpeartions(arr, n, x)
{
let k = x, i = 0;
while (n-- > 0) {
// if the element is less than x.
if (arr[n] < x)
k--;
// if the element equals to x.
if (arr[n] == x)
k++;
}
return k;
}
let arr = [ 0, 4, 5, 6, 7 ];
let n = arr.length;
let x = 3;
// output
document.write(minOpeartions(arr, n, x));
</script>
PHP
<?php
// PHP program to perform minimal
// number of operations to make
// the MEX of the set equal to
// the given number x.
// function to find minimum number
// of operations required
function minOpeartions( $arr, $n, $x)
{
$k = $x; $i = 0;
while ($n--)
{
// if the element is
// less than x.
if ($arr[$n] < $x)
$k--;
// if the element equals to x.
if ($arr[$n] == $x)
$k++;
}
return $k;
}
// Driver Code
$arr = array(0, 4, 5, 6, 7);
$n = count($arr);
$x = 3;
echo minOpeartions($arr, $n, $x) ;
// This code is contributed by anuj_67.
?>
Time Complexity: O(n)
Auxiliary Space: O(1)
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