给定一个n个正整数的数组“ arr1”。 arr1 []的内容被复制到另一个数组’arr2’,但是数字被重新排列,一个元素被删除。查找缺少的元素(不使用任何额外的空间,并且时间复杂度为O(n))。
例子 :
Input : arr1[] = {4, 8, 1, 3, 7},
arr2[] = {7, 4, 3, 1}
Output : 8
Input : arr1[] = {12, 10, 15, 23, 11, 30},
arr2[] = {15, 12, 23, 11, 30}
Output : 10
一个简单的解决方案是逐个考虑第一个数组的每个元素,然后在第二个数组中搜索。一旦找到缺少的元素,我们将返回。该解决方案的时间复杂度为O(n 2 )
一个有效的解决方案基于异或。每个元素的组合出现次数是两次,一个在“ arr1”中,另一个在“ arr2”中,除了一个元素在“ arr1”中仅出现一次。我们知道(a Xor a)=0。因此,只需对两个数组的元素进行XOR。结果将是缺少的数字。
C++
// C++ implementation to find the
// missing number in shuffled array
// C++ implementation to find the
// missing number in shuffled array
#include
using namespace std;
// Returns the missing number
// Size of arr2[] is n-1
int missingNumber(int arr1[], int arr2[],
int n)
{
// Missing number 'mnum'
int mnum = 0;
// 1st array is of size 'n'
for (int i = 0; i < n; i++)
mnum = mnum ^ arr1[i];
// 2nd array is of size 'n - 1'
for (int i = 0; i < n - 1; i++)
mnum = mnum ^ arr2[i];
// Required missing number
return mnum;
}
// Driver Code
int main()
{
int arr1[] = {4, 8, 1, 3, 7};
int arr2[] = {7, 4, 3, 1};
int n = sizeof(arr1) / sizeof(arr1[0]);
cout << "Missing number = "
<< missingNumber(arr1, arr2, n);
return 0;
}
Java
// Java implementation to find the
// missing number in shuffled array
class GFG
{
// Returns the missing number
// Size of arr2[] is n-1
static int missingNumber(int arr1[],
int arr2[],
int n)
{
// Missing number 'mnum'
int mnum = 0;
// 1st array is of size 'n'
for (int i = 0; i < n; i++)
mnum = mnum ^ arr1[i];
// 2nd array is of size 'n - 1'
for (int i = 0; i < n - 1; i++)
mnum = mnum ^ arr2[i];
// Required missing number
return mnum;
}
// Driver Code
public static void main (String[] args)
{
int arr1[] = {4, 8, 1, 3, 7};
int arr2[] = {7, 4, 3, 1};
int n = arr1.length;
System.out.println("Missing number = "
+ missingNumber(arr1, arr2, n));
}
}
Python3
# Python3 implementation to find the
# missing number in shuffled array
# Returns the missing number
# Size of arr2[] is n - 1
def missingNumber(arr1, arr2, n):
# missing number 'mnum'
mnum = 0
# 1st array is of size 'n'
for i in range(n):
mnum = mnum ^ arr1[i]
# 2nd array is of size 'n - 1'
for i in range(n - 1):
mnum = mnum ^ arr2[i]
# Required missing number
return mnum
# Driver Code
arr1 = [4, 8, 1, 3, 7]
arr2= [7, 4, 3, 1]
n = len(arr1)
print("Missing number = ",
missingNumber(arr1, arr2, n))
# This code is contributed by Anant Agarwal.
C#
// C# implementation to find the
// missing number in shuffled array
using System;
class GFG
{
// Returns the missing number
// Size of arr2[] is n-1
static int missingNumber(int []arr1,
int []arr2,
int n)
{
// Missing number 'mnum'
int mnum = 0;
// 1st array is of size 'n'
for (int i = 0; i < n; i++)
mnum = mnum ^ arr1[i];
// 2nd array is of size 'n - 1'
for (int i = 0; i < n - 1; i++)
mnum = mnum ^ arr2[i];
// Required missing number
return mnum;
}
// Driver Code
public static void Main ()
{
int []arr1 = {4, 8, 1, 3, 7};
int []arr2 = {7, 4, 3, 1};
int n = arr1.Length;
Console.Write("Missing number = "
+ missingNumber(arr1, arr2, n));
}
}
// This code is contributed by nitin mittal.
PHP
输出:
Missing number = 8
复杂度:O(n)时间复杂度和O(1)额外空间。