Open In App

Find missing number in another array which is shuffled copy

Last Updated : 11 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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>


Output

Missing number = 8

Time Complexity:  O(n).
Space Complexity: O(1).

 



Similar Reads

Check if the given string is shuffled substring of another string
Given strings str1 and str2. The task is to find if str1 is a substring in the shuffled form of str2 or not. Print "YES" if str1 is a substring in shuffled form of str2 else print "NO". Example Input: str1 = "onetwofour", str2 = "hellofourtwooneworld" Output: YES Explanation: str1 is substring in shuffled form of str2 as str2 = "hello" + "fourtwoon
15 min read
Find minimum area of rectangle formed from given shuffled coordinates
Given an array arr[] of size N, the array represents N / 2 coordinates of a rectangle randomly shuffled with its X and Y coordinates. The task for this problem is to construct N / 2 pairs of (X, Y) coordinates by choosing X and Y from array arr[] in such a way that a rectangle that contains all these points has a minimum area. Examples: Input: arr[
8 min read
Largest area square in an array when elements can be shuffled
Given an array arr[] of N integers where arr[i] is the height of the ith chocolate and all the chocolates are 1 unit wide, the task is to find the maximum area for any square made from the chocolates when the chocolates can be arranged in any order.Examples: Input: arr[] = {1, 3, 4, 5, 5} Output: 9 Square with side = 3 can be obtained from either {
7 min read
Restore a shuffled Queue as per given Conditions
Given N people standing in a queue and two arrays A[] and B[]. The array A[] represent the name of the person and array B[] represents how many people are taller than a particular person standing in front of that. Now the queue is shuffled. The task is to print the original sequence of the queue following the above property. Examples: Input: N = 4,
8 min read
Minimum operations for which all integers from [0, N] appears as smallest positive missing number (MEX)
Given an array arr[], of size N the task is to find the minimum operations on the array such that in each operation any element in the array can be chosen and incremented by 1 so that the MEX is i for all i in the range [0, n]. If for any i if the MEX is not i print -1. Examples : Input : arr[] = {3, 0, 1, 0, 3}Output: MEX for i = 0 is 2MEX for i =
13 min read
Longest subarray of an array which is a subsequence in another array
Given two arrays arr1[] and arr2[], the task is to find the longest subarray of arr1[] which is a subsequence of arr2[]. Examples: Input: arr1[] = {4, 2, 3, 1, 5, 6}, arr2[] = {3, 1, 4, 6, 5, 2}Output: 3Explanation: The longest subarray of arr1[] which is a subsequence in arr2[] is {3, 1, 5} Input: arr1[] = {3, 2, 4, 7, 1, 5, 6, 8, 10, 9}, arr2[] =
13 min read
Find the smallest positive number missing from an unsorted array : Hashing Implementation
Given an unsorted array with both positive and negative elements including 0. The task is to find the smallest positive number missing from the array in O(N) time. Examples: Input: arr[] = {-5, 2, 0, -1, -10, 15} Output: 1 Input: arr[] = {0, 1, 2, 3, 4, 5} Output: 6 Input: arr[] = {1, 1, 1, 0, -1, -2} Output: 2 We can use hashing to solve this prob
5 min read
Find the smallest positive number missing from an unsorted array | Set 3
Given an unsorted array with both positive and negative elements. The task is to find the smallest positive number missing from the array. Examples: Input: arr[] = {2, 3, 7, 6, 8, -1, -10, 15} Output: 1 Input: arr[] = { 2, 3, -7, 6, 8, 1, -10, 15 } Output: 4 Input: arr[] = {1, 1, 0, -1, -2} Output: 2 Approach : We have already discussed some of the
6 min read
Find the smallest positive number missing from an unsorted array | Set 2
Given an unsorted array with both positive and negative elements. Find the smallest positive number missing from the array in O(n) time using constant extra space. It is allowed to modify the original array. Examples: Input: {2, 3, 7, 6, 8, -1, -10, 15} Output: 1 Input: { 2, 3, -7, 6, 8, 1, -10, 15 } Output: 4 Input: {1, 1, 0, -1, -2} Output: 2 Rec
12 min read
Find the missing number in a sorted array of limited range
Given a sorted array of size n and given that there are numbers from 1 to n+1 with one missing, the missing number is to be found. It may be assumed that array has distinct elements. Examples: Input : 1 3 4 5 6Output : 2 Input : 1 2 3 4 5 7 8 9 10Output : 6 Recommended: Please try your approach on {IDE} first, before moving on to the solution.We tr
10 min read
Article Tags :
Practice Tags :