将给定数组分成两部分,使两部分的 MEX 相同
给定一个数组arr[]包含N个整数,其中0 ≤ A[ i ] ≤ N ,任务是将数组分成两个相等的部分,使得两个部分的MEX相同,否则打印 -1。
注意: 墨西哥 数组的(最小排除)是数组中不存在的最小非负整数。
例子:
Input: N = 6, arr[] = {1, 6, 2, 3, 5, 2}
Output:
0
1 2 5
2 3 6
Explanation: After dividing the given array into {1, 2, 5} and {2, 3, 6}, MEX of both array is equal( i. e MEX = 0)
Input: N = 4, arr[] = {0, 0, 1, 2}
Output: -1
Explanation: Not possible to divide an array into two different array
直觉:如前所述,MEX 是不属于数组的最小非负整数。因此,给定数组中不存在的任何非负最小数都可以成为两个输出数组的 MEX。
要为两个数组生成整数X, MEX,所有小于 X 的元素必须在两个输出数组中至少出现一次。
For Example:
Array = [1, 4, 2, 4, 2, 1, 1]
Then divide the array into [1, 1, 2, 4] and [1, 2, 4]
如果任何小于 X 的元素在给定数组中只出现一次,则不可能将给定数组分成两个 MEX 相等的数组。
Example:
Array = [1, 2, 3, 4, 4, 2, 1]
In this example, 3 is only present 1 time in given array so if we try to divide it into 2 array, then MEX of both array is not equal.
[1, 2, 3, 4] : MEX = 5
[1, 2, 4] : MEX = 3
方法:解决问题的方法是基于hashmap的概念。请按照以下步骤操作:
- 创建一个哈希映射来检查数组中是否存在任何唯一元素。
- 从0 迭代到 N并在每次迭代中检查
- 如果该元素的频率为 1,则不可能将数组分成两部分,使得它们的MEX应该相等
- 如果该元素的频率为 0,那么我们可以划分数组。
- 如果该元素的频率大于 1,则检查下一个元素。
- 对给定数组进行排序并根据其偶数或奇数索引划分数组,以便如果数组中存在任何元素 2 次或超过 2 次,则在划分两个输出数组后,至少包含 1 次元素以保持相等MEX 。
- 打印两个数组。
下面是代码实现:
C++
// C++ code to implement the above approach:
#include
using namespace std;
// Function to find the
// Least common missing no.
void MEX_arr(int* A, int N)
{
unordered_map mp;
bool status = true;
// Push all array elements
// Into unordered_map
for (int i = 0; i < N; i++) {
mp[A[i]]++;
}
// Check any unique element is present
// In array or any element
// Which is not array;
for (int i = 0; i <= N; i++) {
if (mp[i] == 0) {
cout << i << "\n";
break;
}
if (mp[i] == 1) {
status = false;
break;
}
}
if (status == true) {
// Sort the array
sort(A, A + N);
int A1[N / 2], A2[N / 2];
// Push all elements at even index
// in first array and at odd index
// into another array
for (int i = 0; i < N / 2; i++) {
A1[i] = A[2 * i];
A2[i] = A[2 * i + 1];
}
// Print both the arrays
for (int i = 0; i < N / 2; i++) {
cout << A1[i] << " ";
}
cout << "\n";
for (int i = 0; i < N / 2; i++) {
cout << A2[i] << " ";
}
}
// If not possible to divide
// into two array, print -1
else
cout << -1;
}
// Driver code
int main()
{
int N = 6;
int arr[N] = { 1, 6, 2, 3, 5, 2 };
unordered_map mp;
// Function call
MEX_arr(arr, N);
return 0;
}
Java
// Java code to implement the above approach
import java.io.*;
import java.util.*;
class GFG
{
// Function to find the
// Least common missing no.
public static void MEX_arr(int A[], int N)
{
HashMap mp = new HashMap<>();
boolean status = true;
// Push all array elements
// Into HashMap
for (int i = 0; i < N; i++) {
if (mp.get(A[i]) != null)
mp.put(A[i], mp.get(A[i]) + 1);
else
mp.put(A[i], 1);
}
// Check any unique element is present
// In array or any element
// Which is not array;
for (int i = 0; i <= N; i++) {
if (mp.get(i) == null) {
System.out.println(i);
break;
}
if (mp.get(i) == 1) {
status = false;
break;
}
}
if (status == true) {
// Sort the array
Arrays.sort(A);
int A1[] = new int[N / 2];
int A2[] = new int[N / 2];
// Push all elements at even index
// in first array and at odd index
// into another array
for (int i = 0; i < N / 2; i++) {
A1[i] = A[2 * i];
A2[i] = A[2 * i + 1];
}
// Print both the arrays
for (int i = 0; i < N / 2; i++) {
System.out.print(A1[i] + " ");
}
System.out.println();
for (int i = 0; i < N / 2; i++) {
System.out.print(A2[i] + " ");
}
}
// If not possible to divide
// into two array, print -1
else
System.out.print(-1);
}
public static void main(String[] args)
{
int N = 6;
int arr[] = { 1, 6, 2, 3, 5, 2 };
// Function call
MEX_arr(arr, N);
}
}
// This code is contributed by Rohit Pradhan.
Python3
# python3 code to implement the above approach:
# Function to find the
# Least common missing no.
def MEX_arr(A, N):
mp = {}
status = True
# Push all array elements
# Into unordered_map
for i in range(0, N):
mp[A[i]] = mp[A[i]]+1 if A[i] in mp else 1
# Check any unique element is present
# In array or any element
# Which is not array;
for i in range(0, N+1):
if (not i in mp):
print(i)
break
elif (mp[i] == 1):
status = False
break
if (status == True):
# Sort the array
A.sort()
A1, A2 = [0 for _ in range(N // 2)], [0 for _ in range(N // 2)]
# Push all elements at even index
# in first array and at odd index
# into another array
for i in range(0, N//2):
A1[i] = A[2 * i]
A2[i] = A[2 * i + 1]
# Print both the arrays
for i in range(0, N//2):
print(A1[i], end=" ")
print()
for i in range(0, N // 2):
print(A2[i], end=" ")
# If not possible to divide
# into two array, print -1
else:
print(-1)
# Driver code
if __name__ == "__main__":
N = 6
arr = [1, 6, 2, 3, 5, 2]
# unordered_map mp;
# Function call
MEX_arr(arr, N)
# This code is contributed by rakeshsahni
C#
// C# code to implement the above approach:
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the
// Least common missing no.
static void MEX_arr(int []A, int N)
{
Dictionary mp =
new Dictionary();
bool status = true;
// Push all array elements
// Into unordered_map
for (int i = 0; i < N; i++) {
if(mp.ContainsKey(A[i])) {
mp[A[i]] = mp[A[i]] + 1;
}
else {
mp.Add(A[i], 1);
}
}
// Check any unique element is present
// In array or any element
// Which is not array;
for (int i = 0; i <= N; i++) {
if (!mp.ContainsKey(i)) {
Console.WriteLine(i);
break;
}
if (mp[i] == 1) {
status = false;
break;
}
}
if (status == true) {
// Sort the array
Array.Sort(A);
int []A1 = new int[N / 2];
int []A2 = new int[N / 2];
// Push all elements at even index
// in first array and at odd index
// into another array
for (int i = 0; i < N / 2; i++) {
A1[i] = A[2 * i];
A2[i] = A[2 * i + 1];
}
// Print both the arrays
for (int i = 0; i < N / 2; i++) {
Console.Write(A1[i] + " ");
}
Console.WriteLine();
for (int i = 0; i < N / 2; i++) {
Console.Write(A2[i] + " ");
}
}
// If not possible to divide
// into two array, print -1
else
Console.Write(-1);
}
// Driver code
public static void Main()
{
int N = 6;
int []arr = { 1, 6, 2, 3, 5, 2 };
// Function call
MEX_arr(arr, N);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
0
1 2 5
2 3 6
时间复杂度: O(N * log(N))
辅助空间: 在)