

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
#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
#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
#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
- Related Questions & Answers
- Find position of an element in a sorted array of infinite numbers in C++
- Find start and ending index of an element in an unsorted array in C++
- Find the index of first 1 in an infinite sorted array of 0s and 1s in C++
- Find missing element in a sorted array of consecutive numbers in C++
- Find one extra character in a string using C++.
- Missing Element in Sorted Array in C++
- Find an integer X which is divisor of all except exactly one element in an array in C++
- C# program to find the index of an element in a List
- Single Element in a Sorted Array in C++
- Equilibrium index of an array in C++
- Finding the nth power of array element present at nth index using JavaScript
- Find First and Last Position of Element in Sorted Array in Python
- Find missing element in a sorted array of consecutive numbers in Python
- Find the only repeating element in a sorted array of size n using C++
- Total number of elements present in an array in C#