Find minimum length for Maximum Mex Subsequence
Last Updated :
06 Dec, 2023
Given an array A[] consisting of N integers. You have to choose a subsequence from the given array such that the mex of that chosen subsequence is maximum. The task is to return the minimum length of such subsequence.
Examples:
Input: N = 7, A[] = {1, 2, 1, 2, 1, 4, 0}
Output: 3
Explanation: We can choose {1, 2, 0} having mex 3 which is the maximum possible.
Input: N = 3, A[] = {1, 1, 2}
Output: 0
Explanation: The maximum mex we can get is 0 which is of empty sub-sequence.
Approach: This can be solved with the following idea:
Using Map data structure, we can start iterating from number 0. If present in array, iterate to next number, otherwise break and return the number.
Below are the steps involved:
- Initialise a map m.
- Add elements of an array into map m.
- Start iterating from i = 0.
- If i is present, iterate further by incrementing 1.
- If not, break and return i.
Below is the implementation of the code:
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int N, vector< int >& A)
{
unordered_map< int , int > m;
int i = 0;
while (i < A.size()) {
m[A[i]]++;
i = i + 1;
}
i = 0;
while ( true ) {
if (m.find(i) == m.end()) {
return i;
}
i = i + 1;
}
return -1;
}
int main()
{
int N = 5;
vector< int > A = { 0, 1, 3, 4, 2 };
cout << solve(N, A);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int solve( int N, Vector<Integer> A)
{
Map<Integer, Integer> m = new HashMap<>();
int i = 0 ;
while (i < A.size()) {
m.put(A.get(i),
m.getOrDefault(A.get(i), 0 ) + 1 );
i = i + 1 ;
}
i = 0 ;
while ( true ) {
if (!m.containsKey(i)) {
return i;
}
i = i + 1 ;
}
}
public static void main(String[] args)
{
int N = 5 ;
Vector<Integer> A = new Vector<>();
A.add( 0 );
A.add( 1 );
A.add( 3 );
A.add( 4 );
A.add( 2 );
System.out.println(solve(N, A));
}
}
|
Python3
def solve(N, A):
m = {}
i = 0
while i < len (A):
if A[i] in m:
m[A[i]] + = 1
else :
m[A[i]] = 1
i + = 1
i = 0
while True :
if i not in m:
return i
i + = 1
return - 1
if __name__ = = "__main__" :
N = 5
A = [ 0 , 1 , 3 , 4 , 2 ]
print (solve(N, A))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static int Solve( int N, List< int > A)
{
Dictionary< int , int > m = new Dictionary< int , int >();
int i = 0;
while (i < A.Count) {
if (m.ContainsKey(A[i]))
m[A[i]]++;
else
m[A[i]] = 1;
i = i + 1;
}
i = 0;
while ( true ) {
if (!m.ContainsKey(i)) {
return i;
}
i = i + 1;
}
}
public static void Main( string [] args)
{
int N = 5;
List< int > A = new List< int >{ 0, 1, 3, 4, 2 };
Console.WriteLine(Solve(N, A));
}
}
|
Javascript
function solve(N, A) {
const m = new Map();
let i = 0;
while (i < A.length) {
if (m.has(A[i])) {
m.set(A[i], m.get(A[i]) + 1);
} else {
m.set(A[i], 1);
}
i++;
}
i = 0;
while ( true ) {
if (!m.has(i)) {
return i;
}
i++;
}
return -1;
}
const N = 5;
const A = [0, 1, 3, 4, 2];
console.log(solve(N, A));
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...