Find lost element from a duplicated array in C++

C++Server Side ProgrammingProgramming

Concept

With respect of given two arrays which are duplicates of each other except one element, that means one element from one of the array is missing, our task to determine that missing element.

Input

arr1[] = {2, 5, 6, 8, 10}
arr2[] = {5, 6, 8, 10}

Output

2

2 is missing from second array.

Input

arr1[] = {3, 4, 5, 6}
arr2[] = {3, 4, 5, 6, 7}

Output

7

7 is missing from first array.

Method

Here, we apply one simple solution where we iterate over arrays and verify element by element and mark the missing element when an unmatched is detected. But the drawback of this solution is that it requires linear time over size of array.

We can implement another efficient solution which is based on binary search approach. We follow the below algorithm which is explained step by step −

  • Begin binary search in bigger array and get mid as (low + high) / 2
  • It has been seen that if value from both array is same then missing element must be in right part so mark low as mid
  • Else mark high as mid as missing element must be in left part of bigger array if mid elements is not same.
  • We have to handle special case separately because for single element and zero element array, single element itself will be the missing element.

It has been observed that if first element itself is not equal then that element will be the missing element.

Example

 Live Demo

// C++ program to find missing element from same
// arrays (except one missing element)
#include <bits/stdc++.h>
using namespace std;
// Shows function to determine missing element based on binary
// search approach. arrA[] is of larger size and
// Q is size of it. arrA[] and arrB[] are assumed
// to be in same order.
int findMissingUtil(int arrA[], int arrB[], int Q){
   // Considers special case, for only element which is
   // missing in second array
   if (Q == 1)
      return arrA[0];
   // Considers special case, for first element missing
   if (arrA[0] != arrB[0])
      return arrA[0];
   // Used to initialize current corner points
   int low = 0, high = Q - 1;
   // Iterate until low < high
   while (low < high){
      int mid = (low + high) / 2;
      // It has been observed that if element at mid indices are equal
      // then go to right subarray
      if (arrA[mid] == arrB[mid])
         low = mid;
      else
         high = mid;
         // So if low, high becomes contiguous, break
      if (low == high - 1)
      break;
   }
   // Now missing element will be at high index of
   // bigger array
   return arrA[high];
}
// So this function mainly does basic error checking
// and calls findMissingUtil
void findMissing(int arrA[], int arrB[], int P, int Q){
   if (Q == P-1)
      cout << "Missing Element is "
      << findMissingUtil(arrA, arrB, P) << endl;
   else if (P == Q-1)
      cout << "Missing Element is "
      << findMissingUtil(arrB, arrA, Q) << endl;
   else
   cout << "Invalid Input";
}
// Driver Code
int main(){
   int arrA[] = {2, 5, 6, 8, 10};
   int arrB[] = {5, 6, 8, 10};
   int P = sizeof(arrA) / sizeof(int);
   int Q = sizeof(arrB) / sizeof(int);
   findMissing(arrA, arrB, P, Q);
   return 0;
}

Output

Missing Element is 2
raja
Published on 24-Jul-2020 12:01:29
Advertisements