Find index of an extra element present in one sorted array in C++


In this problem, we are given two sorted arrays arr1 and arr2 of size n  and n+1 with all elements the same except the extra element. Our task is to find index of an extra element present in one sorted array.

 Problem Description: We need to find the index of an element from the n+1 size array which is not present in an array of size n.

Let’s take an example to understand the problem,

Input:         arr1[n]  = {3, 5, 7, 8, 9, 12}
                   arr2[n+1] = {3, 4, 5, 7, 8, 9, 12}

Output: 1

Explanation: 

The element with value 4 is extra which is at index 1.

Solution Approach −

A simple solution to the problem is using the fact that both the arrays are sorted. And with only one element which is not equal, we can perform linear search and find the element in arr2 which is not present in arr1[].

Algorithm:

Step 1: Loop for i -> 0 to n+1,

Step 1.1: find odd elements, if arr1[i] != arr2[i], break loop.

 Step 2: return the value of i.

Program to illustrate the working of our solution,

Example

Live Demo

#include <iostream>
using namespace std;

int findExtraElement(int arr1[], int arr2[], int n) {
   
   int i;
   for (i = 0; i < n; i++)
      if (arr1[i] != arr2[i])
         break;
   return i;
}

int main()
{
   int arr1[] = {3, 5, 7, 8, 9, 12};
   int arr2[] = {3, 4, 5, 7, 8, 9, 12};
   int n = sizeof(arr1) / sizeof(arr1[0]);
   int extraIndex = findExtraElement(arr1, arr2, n);
   cout<<"The extra element is at index ("<<extraIndex<<") and the value is "<<arr2[extraIndex];
   return 0;
}

Output

The extra element is at index (1) and the value is 4

This solution can be made better by using more effective searching technique which is binary search instead of linear decrease the computation time of the algorithm:

Program to illustrate the working of our solution,

Example

Live Demo

#include <iostream>
using namespace std;

int findExtraElement(int arr1[], int arr2[], int n) {
   
   int extraIndex = n;
   int start = 0, end = n - 1;
   while (start <= end)
   {
      int mid = (start + end) / 2;
      if (arr2[mid] == arr1[mid])
         start = mid + 1;
      else
      {
         extraIndex = mid;
         end = mid - 1;
      }
   }
   return extraIndex;
}

int main()
{
   int arr1[] = {3, 5, 7, 8, 9, 12};
   int arr2[] = {3, 4, 5, 7, 8, 9, 12};
   int n = sizeof(arr1) / sizeof(arr1[0]);
   int extraIndex = findExtraElement(arr1, arr2, n);
   cout<<"The extra element is at index ("<<extraIndex<<") and the value is "<<arr2[extraIndex];
   return 0;
}

Output

The extra element is at index (1) and the value is 4

Another Approach: 

One more approach to solve the problem is by finding the absolute difference between the two arrays, which is the extra element. Then we need to find the index of this extra element in the array of size n+1. This can be done using a searching algorithm.

Program to illustrate the working of our solution,

Example

Live Demo

#include <iostream>
using namespace std;

int calcArraysum(int arr[], int n){
   int sum = 0;
   for(int i = 0; i < n; i++)
      sum += arr[i];
   return sum;
}

int findExtraElement(int arr1[], int arr2[], int n) {
   
   int extraValue = calcArraysum(arr2, n+1) - calcArraysum(arr1, n);

   for (int i = 0; i < n; i++)
   {
      if (arr2[i] == extraValue)
         return i;
   }
   return -1;
}

int main()
{
   int arr1[] = {3, 5, 7, 8, 9, 12};
   int arr2[] = {3, 4, 5, 7, 8, 9, 12};
   int n = sizeof(arr1) / sizeof(arr1[0]);
   int extraIndex = findExtraElement(arr1, arr2, n);
   cout<<"The extra element is at index ("<<extraIndex<<") and the value is "<<arr2[extraIndex];
   return 0;
}

Output

The extra element is at index (1) and the value is 4

Updated on: 22-Jan-2021

441 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements