-
Notifications
You must be signed in to change notification settings - Fork 1k
/
MEX_array.dart
75 lines (64 loc) · 1.45 KB
/
MEX_array.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
The function find the MEX of an n integer array.
MEX is the minimum excluded element in an array, i.e, the minimum number which
is not present in the given array.
*/
import 'dart:io';
import 'dart:core';
int get_MEX_of_array(List arr, int n) {
// Creating a set to store the unique elements of the array
var mex_find = <int>{};
for (int i = 0; i < n; i++) {
mex_find.add(arr[i]);
}
// Iterating through the array and checking if integers from 0 to n are not in
// the given array
int mex = 1;
for (int i in mex_find) {
// Checking if the mex chosen is in the set mex_find
if (mex_find.contains(mex)) {
mex++;
}
// Indicating that we have already found the mex of the array
else if (i > mex) {
break;
}
}
return mex;
}
main() {
// Driver code to take user input
var array = [];
var n, ele, x;
print('Enter the number of Elements: ');
n = stdin.readLineSync();
n = int.parse(n);
for (int i = 1; i <= n; i++) {
print('Enter Element $i: ');
ele = stdin.readLineSync();
ele = int.parse(ele);
array.add(ele);
}
int arr_mex = get_MEX_of_array(array, n);
print("Minimum excluded element of the Array is : $arr_mex");
}
/*
TIME COMPLEXITY: O(log N)
SPACE COMPLEXITY: O(N)
Standard I/O:
Enter the number of Elements:
6
Enter Element 1:
5
Enter Element 2:
2
Enter Element 3:
9
Enter Element 4:
1
Enter Element 5:
1
Enter Element 6:
3
Minimum excluded element of the Array is : 4
*/