两个排序数组的对称差
有两个排序数组arr1和arr2 。我们必须找到 Aarr1 和 arr2 的对称差。对称差分基本上包含两个数组的所有元素,除了公共元素。
Symmetric difference of two array is the all
array elements of both array except the elements
that are presents in both array.
SymmDiff = (arr1 - arr2) UNION (arr2 - arr1).
OR
SymmDiff = (arr1 UNION arr2) - (arr1 INTERSECTION arr2).
例子:
Input : arr1[] = {2, 4, 5, 7, 8, 10, 12, 15}.
arr2[] = {5, 8, 11, 12, 14, 15}.
Output : 2 4 7 10 11 14
arr1[] - arr2[] = {2, 4, 7, 10}.
arr[2] - arr1[] = {11, 14}.
SymmDiff = (arr1[] - arr2[]) UNION
(arr2[] - arr1[]).
= {2, 4, 7, 10, 11, 14}.
Input : arr1[] = {1, 3, 5, 8, 15, 27, 35}.
arr2[] = {5, 7, 8, 11, 15, 18, 35}.
Output : 1 3 7 11 18 27
arr1[] - arr2[] = {1, 3, 27}.
arr[2] - arr1[] = {7, 11, 18}.
SymmDiff = (arr1[] - arr2[]) UNION
(arr2[] - arr1[]).
= {1, 3, 7, 11, 18, 27}.
一个简单的解决方案是一个一个地遍历两个数组。对于一个数组的每个元素,检查它是否存在于另一个数组中。如果是,则忽略它,否则打印它。该解决方案的时间复杂度为 O(n*n)
找到两个排序数组的对称差异的有效解决方案类似于归并排序的合并过程。如果当前两个元素不匹配,我们同时遍历两个数组并打印较小的元素,并在具有较小元素的数组中向前移动。否则,我们将忽略元素并在两个数组中继续前进。
C++
// C++ program to find the symmetric difference
// of two sorted array.
#include
using namespace std;
void symmDiff(int arr1[], int arr2[], int n, int m)
{
// Traverse both arrays simultaneously.
int i = 0, j = 0;
while (i < n && j < m) {
// Print smaller element and move
// ahead in array with smaller element
if (arr1[i] < arr2[j]) {
cout << arr1[i] << " ";
i++;
}
else if (arr2[j] < arr1[i]) {
cout << arr2[j] << " ";
j++;
}
// If both elements same, move ahead
// in both arrays.
else {
i++;
j++;
}
}
while (i < n) {
cout << arr1[i] << " ";
i++;
}
while (j < m) {
cout << arr2[j] << " ";
j++;
}
}
// Driver code
int main()
{
int arr1[] = { 2, 4, 5, 7, 8, 10, 12, 15 };
int arr2[] = { 5, 8, 11, 12, 14, 15 };
int n = sizeof(arr1) / sizeof(arr1[0]);
int m = sizeof(arr2) / sizeof(arr2[0]);
symmDiff(arr1, arr2, n, m);
return 0;
}
Java
// Java program to find the symmetric
// difference of two sorted array.
import java.util.*;
class GFG {
static void symmDiff(int[] arr1, int[] arr2, int n,
int m)
{
// Traverse both arrays simultaneously.
int i = 0, j = 0;
while (i < n && j < m) {
// Print smaller element and move
// ahead in array with smaller element
if (arr1[i] < arr2[j]) {
System.out.print(arr1[i] + " ");
i++;
}
else if (arr2[j] < arr1[i]) {
System.out.print(arr2[j] + " ");
j++;
}
// If both elements same, move ahead
// in both arrays.
else {
i++;
j++;
}
}
while (i < n) {
System.out.print(arr1[i] + " ");
i++;
}
while (j < m) {
System.out.print(arr2[j] + " ");
j++;
}
}
// Driver code
public static void main(String[] args)
{
int[] arr1 = { 2, 4, 5, 7, 8, 10, 12, 15 };
int[] arr2 = { 5, 8, 11, 12, 14, 15 };
int n = arr1.length;
int m = arr2.length;
symmDiff(arr1, arr2, n, m);
}
}
/* This code is contributed by Kriti Shukla */
Python3
# Python3 program to
# find the symmetric
# difference of two
# sorted array.
def symmDiff(arr1, arr2, n, m):
# Traverse both arrays
# simultaneously.
i = 0
j = 0
while (i < n and j < m):
# Print smaller element
# and move ahead in
# array with smaller
# element
if (arr1[i] < arr2[j]):
print(arr1[i], end=" ")
i += 1
elif (arr2[j] < arr1[i]):
print(arr2[j], end=" ")
j += 1
# If both elements
# same, move ahead
# in both arrays.
else:
i += 1
j += 1
while i < n:
print(arr1[i], end=' ')
i += 1
while j < m:
print(arr2[j], end=' ')
j += 1
# Driver code
arr1 = [2, 4, 5, 7, 8, 10, 12, 15]
arr2 = [5, 8, 11, 12, 14, 15]
n = len(arr1)
m = len(arr2)
symmDiff(arr1, arr2, n, m)
# This code is contributed by Smitha Dinesh Semwal
C#
// C# program to find the symmetric
// difference of two sorted array.
using System;
class GFG {
static void symmDiff(int[] arr1, int[] arr2, int n,
int m)
{
// Traverse both arrays simultaneously.
int i = 0, j = 0;
while (i < n && j < m) {
// Print smaller element and move
// ahead in array with smaller element
if (arr1[i] < arr2[j]) {
Console.Write(arr1[i] + " ");
i++;
}
else if (arr2[j] < arr1[i]) {
Console.Write(arr2[j] + " ");
j++;
}
// If both elements same, move ahead
// in both arrays.
else {
i++;
j++;
}
}
while (i < n) {
Console.Write(arr1[i] + " ");
i++;
}
while (j < m) {
Console.Write(arr2[j] + " ");
j++;
}
}
// Driver code
public static void Main()
{
int[] arr1 = { 2, 4, 5, 7, 8, 10, 12, 15 };
int[] arr2 = { 5, 8, 11, 12, 14, 15 };
int n = arr1.Length;
int m = arr2.Length;
symmDiff(arr1, arr2, n, m);
}
}
/* This code is contributed by vt_m*/
PHP
Javascript
输出:
2 4 7 10 11 14