Find missing number in another array which is shuffled copy
Last Updated :
11 Jul, 2022
Given an array 'arr1' of n positive integers. Contents of arr1[] are copied to another array 'arr2', but numbers are shuffled and one element is removed. Find the missing element(without using any extra space and in O(n) time complexity).
Examples :
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
A simple solution is to one by one consider every element of first array and search in second array. As soon as we find a missing element, we return. Time complexity of this solution is O(n2)
An efficient solution is based on XOR. The combined occurrence of each element is twice, one in 'arr1' and other in 'arr2', except one element which only has a single occurrence in 'arr1'. We know that (a Xor a) = 0. So, simply XOR the elements of both the arrays. The result will be the missing number.
Implementation:
C++
// C++ implementation to find the
// missing number in shuffled array
// C++ implementation to find the
// missing number in shuffled array
#include <bits/stdc++.h>
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
<?php
// PHP implementation to find the
// missing number in shuffled array
// PHP implementation to find the
// missing number in shuffled array
// Returns the missing number
// Size of arr2[] is n-1
function missingNumber($arr1, $arr2,
$n)
{
// Missing number 'mnum'
$mnum = 0;
// 1st array is of size 'n'
for ($i = 0; $i < $n; $i++)
$mnum = $mnum ^ $arr1[$i];
// 2nd array is of size 'n - 1'
for ($i = 0; $i < $n - 1; $i++)
$mnum = $mnum ^ $arr2[$i];
// Required missing number
return $mnum;
}
// Driver Code
$arr1 = array(4, 8, 1, 3, 7);
$arr2 = array(7, 4, 3, 1);
$n = count($arr1);
echo "Missing number = "
, missingNumber($arr1, $arr2, $n);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// Javascript implementation to find the
// missing number in shuffled array
// Javascript implementation to find the
// missing number in shuffled array
// Returns the missing number
// Size of arr2[] is n-1
function missingNumber(arr1, arr2, n)
{
// Missing number 'mnum'
let mnum = 0;
// 1st array is of size 'n'
for (let i = 0; i < n; i++)
mnum = mnum ^ arr1[i];
// 2nd array is of size 'n - 1'
for (let i = 0; i < n - 1; i++)
mnum = mnum ^ arr2[i];
// Required missing number
return mnum;
}
// Driver Code
let arr1 = [4, 8, 1, 3, 7];
let arr2 = [7, 4, 3, 1];
let n = arr1.length;
document.write("Missing number = "
+ missingNumber(arr1, arr2, n));
</script>
Time Complexity: O(n).
Space Complexity: O(1).
Similar Reads
Find the Target number in an Array Finding a number within an array is an operation, in the field of computer science and data analysis. In this article, we will discuss the steps involved and analyze their time and space complexities. Examples: Input: Array: {10, 20, 30, 40, 50} , Target: 30Output: "Target found at index 2" Input: A
13 min read
Find all duplicate and missing numbers in given permutation array of 1 to N Given an array arr[] of size N consisting of the first N natural numbers, the task is to find all the repeating and missing numbers over the range [1, N] in the given array. Examples: Input: arr[] = {1, 1, 2, 3, 3, 5}Output: Missing Numbers: [4, 6]Duplicate Numbers: [1, 3]Explanation:As 4 and 6 are
8 min read
Missing in a Sorted Array of Natural Numbers Given a sorted array arr[] of n-1 integers, these integers are in the range of 1 to n. There are no duplicates in the array. One of the integers is missing in the array. Write an efficient code to find the missing integer. Examples: Input : arr[] = [1, 2, 3, 4, 6, 7, 8]Output : 5Explanation: The mis
12 min read
Find the Missing Number Given an array arr[] of size n-1 with distinct integers in the range of [1, n]. This array represents a permutation of the integers from 1 to n with one element missing. Find the missing element in the array.Examples: Input: arr[] = [8, 2, 4, 5, 3, 7, 1]Output: 6Explanation: All the numbers from 1 t
12 min read
Find indices in a sorted Matrix where a new number can be replaced Given a matrix arr[][] which is sorted by the increasing number of elements and a number X, the task is to find the position where the input integer can be replaced with an existing element without disturbing the order of the sorted elements. The matrix is sorted in such a manner that: Every row is
11 min read
Find original array from given array which is obtained after P prefix reversals | Set-2 Given an array arr[] of size N and an integer P, the task is to find the original array from the array obtained by P prefix reversals where in ith reversal the prefix of size i of the array containing indices in range [0, i-1] was reversed. Note: P is less than or equal to N Examples: Input: arr[] =
7 min read
Restore the original array from another array generated by given operations Given an array b. The array b is obtained initially by array a, by doing the following operations. Choose a fixed point x from array a.Cyclically rotate the array a to the left exactly x times.Fixed point x is that point where (a[i] = i ). These operations are performed on the array a k times, deter
8 min read
Find the missing value from Array B formed by adding some value X to Array A Given two arrays arr1[] and arr2[] of size N and N - 1 respectively. Each value in arr2[] is obtained by adding a hidden value say X to any arr1[i] for N-1 times, which means for any i, arr1[i]+X is not included in arr2[] that is the missing value in arr2[]. The task is to find both the hidden value
10 min read
Find the Array element after Q queries based on the given conditions Given an array arr[] of length N and Q queries of 3 types (1, 2, 3) whose operations are as follows: Type 1: query has input as 1 and the task is to reverse the array.Type 2: query has input as (2 x) and the task to find the index of x in the result array.Type 3: query has input as (3 x y) and the t
10 min read
Minimum value by which each Array element must be added as per given conditions Given 2 arrays A[] and B[] and an integer M. The task is to find the minimum value of X such that after changing all the elements of the array to (arr[i] + X)%M frequency of all the elements of A[] is same as the frequency of all the elements of B[]. Print "-1" if it is not possible to find any valu
8 min read