Given two sorted arrays a[] and b[] of distinct elements. The first array has one element extra added in between. Return the index of the extra element.
Note: 0-based indexing is followed.
Examples:
Input: a[] = [2, 4, 6, 8, 9, 10, 12], b[] = [2, 4, 6, 8, 10, 12]
Output: 4
Explanation: In the first array, 9 is extra added and it's index is 4.Input: a[] = [3,5,7,8,11,13], b[] = [3,5,7,11,13]
Output: 3
Explanation: In the first array, 8 is extra and it's index is 3.Input: a[] = [3,5], b[] = [3]
Output: 1
Explanation: In the first array, 5 is extra and it's index is 1.
Table of Content
[Naive Approach] Linear Scan - O(n) Time and O(1) Space
Since both arrays are sorted and identical except for one extra element in a[], compare elements at the same indices. All elements match until the extra element causes a shift, so the first mismatch index gives the answer. If no mismatch is found, the extra element is at the end of a[].
#include <iostream>
#include <vector>
using namespace std;
// Returns index of extra element in a[]
int findExtra(vector<int>& a, vector<int>& b)
{
for (int i = 0; i < b.size(); i++)
{
if (a[i] != b[i])
return i;
}
return b.size();
}
int main()
{
vector<int> a = {2, 4, 6, 8, 9, 10, 12};
vector<int> b = {2, 4, 6, 8, 10, 12};
cout << findExtra(a, b);
return 0;
}
public class GFG {
// Returns index of extra element in a
static int findExtra(int[] a, int[] b) {
// Compare elements until mismatch is found
for (int i = 0; i < b.length; i++) {
if (a[i] != b[i]) {
return i;
}
}
// If all match, extra element is at the end
return b.length;
}
public static void main(String[] args) {
int[] a = {2, 4, 6, 8, 9, 10, 12};
int[] b = {2, 4, 6, 8, 10, 12};
System.out.println(findExtra(a, b));
}
}
# Returns index of extra element in a
def findExtra(a, b):
# Compare elements until mismatch is found
for i in range(len(b)):
if a[i] != b[i]:
return i
# If all match, extra element is at the end
return len(b)
if __name__ == "__main__":
a = [2, 4, 6, 8, 9, 10, 12]
b = [2, 4, 6, 8, 10, 12]
print(findExtra(a, b))
using System;
class GFG
{
// Returns index of extra element in a
static int FindExtra(int[] a, int[] b)
{
// Compare elements until mismatch is found
for (int i = 0; i < b.Length; i++)
{
if (a[i] != b[i])
{
return i;
}
}
// If all match, extra element is at the end
return b.Length;
}
static void Main()
{
int[] a = { 2, 4, 6, 8, 9, 10, 12 };
int[] b = { 2, 4, 6, 8, 10, 12 };
Console.WriteLine(FindExtra(a, b));
}
}
// Returns index of extra element in a
function findExtra(a, b) {
// Compare elements until mismatch is found
for (let i = 0; i < b.length; i++) {
if (a[i] !== b[i]) {
return i;
}
}
// If all match, extra element is at the end
return b.length;
}
// Driver code
let a = [2, 4, 6, 8, 9, 10, 12];
let b = [2, 4, 6, 8, 10, 12];
console.log(findExtra(a, b));
Output
4
[Expected Approach] Using Binary Search - O(log(n)) Time and O(1) Space
Since both arrays are sorted and identical except for one extra element, use binary search to find the first index where elements differ. If elements at mid are equal, the extra element lies on the right, otherwise, it lies on the left (including mid). keep narrowing the range to find the earliest mismatch.
a[] = [2, 4, 6, 8, 9, 10, 12], b[] = [2, 4, 6, 8, 10, 12]
left = 0, right = b.size() - 1 = 5
mid = 2
Compare a[2] and b[2], since they are same, we move to the right half
left = 3, right = 5
mid = 4Compare a[4] and b[4], since they are not same, we move to the left half and update index = 3
left = 3, right = 4
mid = 3Compare a[3] and b[3], since they are same, we move to the right half
left = 4 right = 4
mid = 4Compare a[4] and b[4], since they are not same, we move to the right half and update index = 4
left = 4 right = 3
Since left is now more than right, stop.
#include <iostream>
#include <vector>
using namespace std;
// Returns index of extra element in a
int findExtra(vector<int>& a, vector<int>& b) {
int left = 0;
int right = b.size() - 1;
int index = b.size();
// Binary search to find first mismatch
while (left <= right) {
int mid = (left + right) / 2;
// If elements match, extra element is on right side
if (a[mid] == b[mid])
left = mid + 1;
else {
// Possible answer, move left to find earlier mismatch
index = mid;
right = mid - 1;
}
}
return index;
}
int main() {
vector<int> a = {2, 4, 6, 8, 9, 10, 12};
vector<int> b = {2, 4, 6, 8, 10, 12};
cout << findExtra(a, b);
return 0;
}
public class GFG {
// Returns index of extra element in a
static int findExtra(int[] a, int[] b) {
int left = 0;
int right = b.length - 1;
int index = b.length;
// Binary search to find first mismatch
while (left <= right) {
int mid = (left + right) / 2;
// If elements match, extra element is on right side
if (a[mid] == b[mid]) {
left = mid + 1;
} else {
// Possible answer, move left to find earlier mismatch
index = mid;
right = mid - 1;
}
}
return index;
}
public static void main(String[] args) {
int[] a = {2, 4, 6, 8, 9, 10, 12};
int[] b = {2, 4, 6, 8, 10, 12};
System.out.println(findExtra(a, b));
}
}
# Returns index of extra element in a
def findExtra(a, b):
left = 0
right = len(b) - 1
index = len(b)
# Binary search to find first mismatch
while left <= right:
mid = (left + right) // 2
# If elements match, extra element is on right side
if a[mid] == b[mid]:
left = mid + 1
else:
# Possible answer, move left to find earlier mismatch
index = mid
right = mid - 1
return index
if __name__ == "__main__":
a = [2, 4, 6, 8, 9, 10, 12]
b = [2, 4, 6, 8, 10, 12]
print(findExtra(a, b))
using System;
class GFG {
// Returns index of extra element in a
static int FindExtra(int[] a, int[] b) {
int left = 0;
int right = b.Length - 1;
int index = b.Length;
// Binary search to find first mismatch
while (left <= right) {
int mid = (left + right) / 2;
// If elements match, extra element is on right side
if (a[mid] == b[mid]) {
left = mid + 1;
}
else {
// Possible answer, move left to find earlier mismatch
index = mid;
right = mid - 1;
}
}
return index;
}
static void Main() {
int[] a = { 2, 4, 6, 8, 9, 10, 12 };
int[] b = { 2, 4, 6, 8, 10, 12 };
Console.WriteLine(FindExtra(a, b));
}
}
// Returns index of extra element in a
function findExtra(a, b) {
let left = 0;
let right = b.length - 1;
let index = b.length;
// Binary search to find first mismatch
while (left <= right) {
let mid = Math.floor((left + right) / 2);
// If elements match, extra element is on right side
if (a[mid] === b[mid]) {
left = mid + 1;
} else {
// Possible answer, move left to find earlier mismatch
index = mid;
right = mid - 1;
}
}
return index;
}
// Driver code
let a = [2, 4, 6, 8, 9, 10, 12];
let b = [2, 4, 6, 8, 10, 12];
console.log(findExtra(a, b));
Output
4