原文:
给定两个不同元素的排序数组,我们需要从两个不常见的数组中打印那些元素。 输出应按排序顺序打印。
示例:
input : arr1[] = {10, 20, 30}
arr2[] = {20, 25, 30, 40, 50}
output : 10 25 40 50
we do not print 20 and 30 as these
elements are present in both arrays.
input : arr1[] = {10, 20, 30}
arr2[] = {40, 50}
output : 10 20 30 40 50
这个想法基于的合并过程。 我们遍历两个数组并跳过公共元素。
c
// c program to find uncommon elements of
// two sorted arrays
#include
using namespace std;
void printuncommon(int arr1[], int arr2[],
int n1, int n2)
{
int i = 0, j = 0, k = 0;
while (i < n1 && j < n2) {
// if not common, print smaller
if (arr1[i] < arr2[j]) {
cout << arr1[i] << " ";
i ;
k ;
}
else if (arr2[j] < arr1[i]) {
cout << arr2[j] << " ";
k ;
j ;
}
// skip common element
else {
i ;
j ;
}
}
// printing remaining elements
while (i < n1) {
cout << arr1[i] << " ";
i ;
k ;
}
while (j < n2) {
cout << arr2[j] << " ";
j ;
k ;
}
}
// driver code
int main()
{
int arr1[] = {10, 20, 30};
int arr2[] = {20, 25, 30, 40, 50};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int n2 = sizeof(arr2) / sizeof(arr2[0]);
printuncommon(arr1, arr2, n1, n2);
return 0;
}
java
// java program to find uncommon elements
// of two sorted arrays
import java.io.*;
class gfg {
static void printuncommon(int arr1[],
int arr2[], int n1, int n2)
{
int i = 0, j = 0, k = 0;
while (i < n1 && j < n2) {
// if not common, print smaller
if (arr1[i] < arr2[j]) {
system.out.print(arr1[i] " ");
i ;
k ;
}
else if (arr2[j] < arr1[i]) {
system.out.print(arr2[j] " ");
k ;
j ;
}
// skip common element
else {
i ;
j ;
}
}
// printing remaining elements
while (i < n1) {
system.out.print(arr1[i] " ");
i ;
k ;
}
while (j < n2) {
system.out.print(arr2[j] " ");
j ;
k ;
}
}
// driver code
public static void main(string[] args)
{
int arr1[] = { 10, 20, 30 };
int arr2[] = { 20, 25, 30, 40, 50 };
int n1 = arr1.length;
int n2 = arr2.length;
printuncommon(arr1, arr2, n1, n2);
}
}
// this code is contributed by vt_m
python3
# python 3 program to find uncommon
# elements of two sorted arrays
def printuncommon(arr1, arr2, n1, n2) :
i = 0
j = 0
k = 0
while (i < n1 and j < n2) :
# if not common, print smaller
if (arr1[i] < arr2[j]) :
print( arr1[i] , end= " ")
i = i 1
k = k 1
elif (arr2[j] < arr1[i]) :
print( arr2[j] , end= " ")
k = k 1
j = j 1
# skip common element
else :
i = i 1
j = j 1
# printing remaining elements
while (i < n1) :
print( arr1[i] , end= " ")
i = i 1
k = k 1
while (j < n2) :
print( arr2[j] , end= " ")
j = j 1
k = k 1
# driver code
arr1 = [10, 20, 30]
arr2 = [20, 25, 30, 40, 50]
n1 = len(arr1)
n2 = len(arr2)
printuncommon(arr1, arr2, n1, n2)
# this code is contributed
# by nikita tiwari.
c#
// c# program to find uncommon elements
// of two sorted arrays
using system;
class gfg {
static void printuncommon(int []arr1,
int []arr2,
int n1,
int n2)
{
int i = 0, j = 0, k = 0;
while (i < n1 && j < n2)
{
// if not common, print smaller
if (arr1[i] < arr2[j])
{
console.write(arr1[i] " ");
i ;
k ;
}
else if (arr2[j] < arr1[i])
{
console.write(arr2[j] " ");
k ;
j ;
}
// skip common element
else
{
i ;
j ;
}
}
// printing remaining elements
while (i < n1)
{
console.write(arr1[i] " ");
i ;
k ;
}
while (j < n2)
{
console.write(arr2[j] " ");
j ;
k ;
}
}
// driver code
public static void main()
{
int []arr1 = {10, 20, 30};
int []arr2 = {20, 25, 30, 40, 50};
int n1 = arr1.length;
int n2 = arr2.length;
printuncommon(arr1, arr2, n1, n2);
}
}
// this code is contributed by sam007
php
输出:
10 25 40 50
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处