Find index of an extra element present in one sorted array
Last Updated :
12 Jul, 2022
Given two sorted arrays. There is only 1 difference between the arrays. The first array has one element extra added in between. Find the index of the extra element.
Examples:
Input: {2, 4, 6, 8, 9, 10, 12};
{2, 4, 6, 8, 10, 12};
Output: 4
Explanation: The first array has an extra element 9.
The extra element is present at index 4.
Input: {3, 5, 7, 9, 11, 13}
{3, 5, 7, 11, 13}
Output: 3
Explanation: The first array has an extra element 9.
The extra element is present at index 3.
Method 1: This includes the basic approach to solve this particular problem.
Approach: The basic method is to iterate through the whole second array and check element by element if they are different. As the array is sorted, checking the adjacent position of two arrays should be similar until and unless the missing element is found.
Algorithm:
- Traverse through the array from start to end.
- Check if the element at i’th element of the two arrays is similar or not.
- If the elements are not similar then print the index and break
Implementation:
C++
#include <iostream>
using namespace std;
int findExtra( int arr1[],
int arr2[], int n)
{
for ( int i = 0; i < n; i++)
if (arr1[i] != arr2[i])
return i;
return n;
}
int main()
{
int arr1[] = {2, 4, 6, 8,
10, 12, 13};
int arr2[] = {2, 4, 6,
8, 10, 12};
int n = sizeof (arr2) / sizeof (arr2[0]);
cout << findExtra(arr1, arr2, n);
return 0;
}
|
Java
class GFG
{
static int findExtra( int arr1[],
int arr2[], int n)
{
for ( int i = 0 ; i < n; i++)
if (arr1[i] != arr2[i])
return i;
return n;
}
public static void main (String[] args)
{
int arr1[] = { 2 , 4 , 6 , 8 ,
10 , 12 , 13 };
int arr2[] = { 2 , 4 , 6 ,
8 , 10 , 12 };
int n = arr2.length;
System.out.println(findExtra(arr1,
arr2, n));
}
}
|
Python3
def findExtra(arr1, arr2, n) :
for i in range ( 0 , n) :
if (arr1[i] ! = arr2[i]) :
return i
return n
arr1 = [ 2 , 4 , 6 , 8 , 10 , 12 , 13 ]
arr2 = [ 2 , 4 , 6 , 8 , 10 , 12 ]
n = len (arr2)
print (findExtra(arr1, arr2, n))
|
C#
using System;
class GfG
{
static int findExtra( int []arr1,
int []arr2, int n)
{
for ( int i = 0; i < n; i++)
if (arr1[i] != arr2[i])
return i;
return n;
}
public static void Main ()
{
int []arr1 = {2, 4, 6, 8,
10, 12, 13};
int []arr2 = {2, 4, 6,
8, 10, 12};
int n = arr2.Length;
Console.Write(findExtra(arr1, arr2, n));
}
}
|
PHP
<?php
function findExtra( $arr1 ,
$arr2 , $n )
{
for ( $i = 0; $i < $n ; $i ++)
if ( $arr1 [ $i ] != $arr2 [ $i ])
return $i ;
return $n ;
}
$arr1 = array (2, 4, 6, 8,
10, 12, 13);
$arr2 = array (2, 4, 6,
8, 10, 12);
$n = sizeof( $arr2 );
echo findExtra( $arr1 , $arr2 , $n );
?>
|
Javascript
<script>
function findExtra(arr1, arr2, n)
{
for (let i = 0; i < n; i++)
if (arr1[i] != arr2[i])
return i;
return n;
}
let arr1 = [2, 4, 6, 8,
10, 12, 13];
let arr2 = [2, 4, 6,
8, 10, 12];
let n = arr2.length;
document.write(findExtra(arr1, arr2, n));
</script>
|
Complexity Analysis:
- Time complexity: O(n).
As one traversal through the array is needed, so the time complexity is linear.
- Space complexity: O(1).
Since no extra space is required, the time complexity is constant.
Method 2: This method is a better way to solve the above problem and uses the concept of binary search.
Approach:To find the index of the missing element in less than linear time, binary search can be used, the idea is all the indices greater than or equal to the index of the missing element will have different elements in both the arrays and all the indices less than that index will have the similar elements in both arrays.
Algorithm:
- Create three variables, low = 0, high = n-1, mid, ans = n
- Run a loop until low is less than or equal to high, i.e till our search range is less than zero.
- If the mid element, i.e (low + high)/2, of both arrays is similar then update the search to second half of the search range, i.e low = mid + 1
- Else update the search to the first half of the search range, i.e high = mid – 1, and update the answer to the current index, ans = mid
- Print the index.
Implementation:
C++
#include <iostream>
using namespace std;
int findExtra( int arr1[],
int arr2[], int n)
{
int index = n;
int left = 0, right = n - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (arr2[mid] == arr1[mid])
left = mid + 1;
else
{
index = mid;
right = mid - 1;
}
}
return index;
}
int main()
{
int arr1[] = {2, 4, 6, 8, 10, 12, 13};
int arr2[] = {2, 4, 6, 8, 10, 12};
int n = sizeof (arr2) / sizeof (arr2[0]);
cout << findExtra(arr1, arr2, n);
return 0;
}
|
Java
class GFG
{
static int findExtra( int arr1[],
int arr2[], int n)
{
int index = n;
int left = 0 , right = n - 1 ;
while (left <= right)
{
int mid = (left+right) / 2 ;
if (arr2[mid] == arr1[mid])
left = mid + 1 ;
else
{
index = mid;
right = mid - 1 ;
}
}
return index;
}
public static void main (String[] args)
{
int arr1[] = { 2 , 4 , 6 , 8 , 10 , 12 , 13 };
int arr2[] = { 2 , 4 , 6 , 8 , 10 , 12 };
int n = arr2.length;
System.out.println(findExtra(arr1, arr2, n));
}
}
|
Python3
def findExtra(arr1, arr2, n) :
index = n
left = 0
right = n - 1
while (left < = right) :
mid = ( int )((left + right) / 2 )
if (arr2[mid] = = arr1[mid]) :
left = mid + 1
else :
index = mid
right = mid - 1
return index
arr1 = [ 2 , 4 , 6 , 8 , 10 , 12 , 13 ]
arr2 = [ 2 , 4 , 6 , 8 , 10 , 12 ]
n = len (arr2)
print (findExtra(arr1, arr2, n))
|
C#
using System;
class GFG {
static int findExtra( int []arr1,
int []arr2,
int n)
{
int index = n;
int left = 0, right = n - 1;
while (left <= right)
{
int mid = (left+right) / 2;
if (arr2[mid] == arr1[mid])
left = mid + 1;
else
{
index = mid;
right = mid - 1;
}
}
return index;
}
public static void Main ()
{
int []arr1 = {2, 4, 6, 8, 10, 12,13};
int []arr2 = {2, 4, 6, 8, 10, 12};
int n = arr2.Length;
Console.Write(findExtra(arr1, arr2, n));
}
}
|
PHP
<?php
function findExtra( $arr1 , $arr2 , $n )
{
$index = $n ;
$left = 0; $right = $n - 1;
while ( $left <= $right )
{
$mid = ( $left + $right ) / 2;
if ( $arr2 [ $mid ] == $arr1 [ $mid ])
$left = $mid + 1;
else
{
$index = $mid ;
$right = $mid - 1;
}
}
return $index ;
}
{
$arr1 = array (2, 4, 6, 8,
10, 12, 13);
$arr2 = array (2, 4, 6,
8, 10, 12);
$n = sizeof( $arr2 ) / sizeof( $arr2 [0]);
echo findExtra( $arr1 , $arr2 , $n );
return 0;
}
?>
|
Javascript
<script>
function findExtra( arr1, arr2, n)
{
let index = n;
let left = 0, right = n - 1;
while (left <= right)
{
let mid = Math.floor((left + right) / 2);
if (arr2[mid] == arr1[mid])
left = mid + 1;
else
{
index = mid;
right = mid - 1;
}
}
return index;
}
let arr1 = [2, 4, 6, 8, 10, 12, 13];
let arr2 = [2, 4, 6, 8, 10, 12];
let n = arr2.length;
document.write(findExtra(arr1, arr2, n));
</script>
|
Complexity Analysis:
- Time complexity : O(log n).
The time complexity of binary search is O(log n)
- Space complexity : O(1).
As no extra space is required, so the time complexity is constant.
Method 3: This method solves the given problem using the predefined function.
Approach: To find the element which is different, find the sum of each array and subtract the sums and find the absolute value. Search the larger array and check if the absolute is equal to an index and return that index. If an element is missing and all the other elements are the same, then the difference of sums will be equal to missing element.
Algorithm:
- Create a function to calculate the sum of two arrays.
- Find the absolute difference between the sum of two arrays (value).
- Traverse the larger array from start too end
- If the element at any index is equal to value, then print the index and break the loop.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int sum( int arr[], int n)
{
int summ = 0;
for ( int i = 0; i < n; i++)
{
summ += arr[i];
}
return summ;
}
int indexOf( int arr[], int element, int n)
{
for ( int i = 0; i < n; i++)
{
if (arr[i] == element)
{
return i;
}
}
return -1;
}
int find_extra_element_index( int arrA[],
int arrB[],
int n, int m)
{
int extra_element = sum(arrA, n) -
sum(arrB, m);
return indexOf(arrA, extra_element, n);
}
int main()
{
int arrA[] = {2, 4, 6, 8, 10, 12, 13};
int arrB[] = {2, 4, 6, 8, 10, 12};
int n = sizeof (arrA) / sizeof (arrA[0]);
int m = sizeof (arrB) / sizeof (arrB[0]);
cout << find_extra_element_index(arrA, arrB, n, m);
}
|
Java
class GFG
{
static int find_extra_element_index( int [] arrA,
int [] arrB)
{
int extra_element = sum(arrA) - sum(arrB);
return indexOf(arrA, extra_element);
}
static int sum( int [] arr)
{
int sum = 0 ;
for ( int i = 0 ; i < arr.length; i++)
{
sum += arr[i];
}
return sum;
}
static int indexOf( int [] arr, int element)
{
for ( int i = 0 ; i < arr.length; i++)
{
if (arr[i] == element)
{
return i;
}
}
return - 1 ;
}
public static void main(String[] args)
{
int [] arrA = { 2 , 4 , 6 , 8 , 10 , 12 , 13 };
int [] arrB = { 2 , 4 , 6 , 8 , 10 , 12 };
System.out.println(find_extra_element_index(arrA, arrB));
}
}
|
Python3
def find_extra_element_index(arrA, arrB):
extra_element = sum (arrA) - sum (arrB)
return arrA.index(extra_element)
arrA = [ 2 , 4 , 6 , 8 , 10 , 12 , 13 ]
arrB = [ 2 , 4 , 6 , 8 , 10 , 12 ]
print (find_extra_element_index(arrA,arrB))
|
C#
using System;
class GFG
{
static int find_extra_element_index( int [] arrA,
int [] arrB)
{
int extra_element = sum(arrA) - sum(arrB);
return indexOf(arrA, extra_element);
}
static int sum( int [] arr)
{
int sum = 0;
for ( int i = 0; i < arr.Length; i++)
{
sum += arr[i];
}
return sum;
}
static int indexOf( int [] arr, int element)
{
for ( int i = 0; i < arr.Length; i++)
{
if (arr[i] == element)
{
return i;
}
}
return -1;
}
public static void Main(String[] args)
{
int [] arrA = {2, 4, 6, 8, 10, 12, 13};
int [] arrB = {2, 4, 6, 8, 10, 12};
Console.WriteLine(find_extra_element_index(arrA, arrB));
}
}
|
Javascript
<script>
function find_extra_element_index(arrA, arrB)
{
let extra_element = sum(arrA) - sum(arrB);
return indexOf(arrA, extra_element);
}
function sum(arr)
{
let sum = 0;
for (let i = 0; i < arr.length; i++)
{
sum += arr[i];
}
return sum;
}
function indexOf(arr, element)
{
for (let i = 0; i < arr.length; i++)
{
if (arr[i] == element)
{
return i;
}
}
return -1;
}
let arrA = [2, 4, 6, 8, 10, 12, 13];
let arrB = [2, 4, 6, 8, 10, 12];
document.write(find_extra_element_index(arrA, arrB));
</script>
|
Complexity Analysis:
- Time Complexity: O(n).
Since only three traversals through the array is needed, So the time complexity is linear.
- Space Complexity: O(1).
As no extra space is required, so the time complexity is constant.
Similar Reads
Find start and ending index of an element in an unsorted array
Given an array of integers, task is to find the starting and ending position of a given key. Examples: Input : arr[] = {1, 2, 3, 4, 5, 5} Key = 5Output : Start index: 4 Last index: 5Explanation: Starting index where 5is present is 4 and ending index is 5. Input :arr[] = {1, 3, 7, 8, 6}, Key = 2Outpu
11 min read
Find the only repeating element in a sorted array of size n
Given a sorted array of n elements containing elements in range from 1 to n-1 i.e. one element occurs twice, the task is to find the repeating element in an array. Examples : Input : arr[] = { 1, 2 , 3 , 4 , 4}Output : 4Input : arr[] = { 1 , 1 , 2 , 3 , 4}Output : 1Brute Force: Traverse the input ar
8 min read
Find position of an element in a sorted array of infinite numbers
Given a sorted array arr[] of infinite numbers. The task is to search for an element k in the array. Examples: Input: arr[] = [3, 5, 7, 9, 10, 90, 100, 130, 140, 160, 170], k = 10Output: 4Explanation: 10 is at index 4 in array. Input: arr[] = [2, 5, 7, 9], k = 3Output: -1Explanation: 3 is not presen
15+ min read
Find extra element in the second array
Given two arrays A[] and B[]. The second array B[] contains all the elements of A[] except for 1 extra element. The task is to find that extra element.Examples: Input: A[] = { 1, 2, 3 }, B[] = {1, 2, 3, 4} Output: 4 Element 4 is not present in arrayInput: A[] = {10, 15, 5}, B[] = {10, 100, 15, 5} Ou
10 min read
Find the only non-repeating element in a given array
Given an array A[] consisting of N (1 ? N ? 105) positive integers, the task is to find the only array element with a single occurrence. Note: It is guaranteed that only one such element exists in the array. Examples: Input: A[] = {1, 1, 2, 3, 3}Output: 2Explanation: Distinct array elements are {1,
11 min read
Find the Middle Element of an Array or List
Given an array or a list of elements. The task is to find the middle element of given array or list of elements. If the array size is odd, return the single middle element. If the array size is even, return the two middle elements. ExampleInput: arr = {1, 2, 3, 4, 5}Output: 3 Input: arr = {7, 8, 9,
9 min read
Find the nearest value present on the left of every array element
Given an array arr[] of size N, the task is for each array element is to find the nearest non-equal value present on its left in the array. If no such element is found, then print -1 Examples: Input: arr[] = { 2, 1, 5, 8, 3 }Output: -1 2 2 5 2Explanation:[2], it is the only number in this prefix. He
8 min read
Find index of the element differing in parity with all other array elements
Given an array arr[] of size N (N > 3), the task is to find the position of the element that differs in parity (odd/even) with respect to all other array elements. Note: It is guaranteed that there will always be a number that differs in parity from all other elements. Examples: Input: arr[] = {2
11 min read
Find elements which are present in first array and not in second
Given two arrays, the task is that we find numbers which are present in first array, but not present in the second array. Examples : Input : a[] = {1, 2, 3, 4, 5, 10}; b[] = {2, 3, 1, 0, 5};Output : 4 10 4 and 10 are present in first array, butnot in second array.Input : a[] = {4, 3, 5, 9, 11}; b[]
14 min read
Find first and last positions of an element in a sorted array
Given a sorted array arr[] with possibly some duplicates, the task is to find the first and last occurrences of an element x in the given array. Note: If the number x is not found in the array then return both the indices as -1. Examples: Input : arr[] = [1, 3, 5, 5, 5, 5, 67, 123, 125], x = 5Output
15+ min read