08(June) Index of an Extra Element
08. Index of an Extra Element
The problem can be found at the following link: Question Link
Problem Description
You are given two sorted arrays arr1[] and arr2[] of distinct elements. The first array has one extra element added in between. Return the index of the extra element.
Note: 0-based indexing is followed.
Example:
Input:
n = 7
arr1 = [2, 4, 6, 8, 9, 10, 12]
arr2 = [2, 4, 6, 8, 10, 12]Output:
4Explanation: In the first array, 9 is extra added and its index is 4.
My Approach
Initialization:
Set two pointers,
lowto 0 andhighton - 1.
Binary Search:
Use a binary search approach to find the index of the extra element.
Calculate the middle index
mid.Compare elements at index
midin both arrays:If
arr1[mid]equalsarr2[mid], move thelowpointer tomid + 1because the extra element must be in the right half.Otherwise, move the
highpointer tomid - 1because the extra element is in the left half.
Return:
The
lowpointer will eventually point to the index of the extra element.
Time and Auxiliary Space Complexity
Expected Time Complexity: O(log n), as we are using a binary search which divides the search space by half each time.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for the pointers and index variables.
Code
C++
class Solution {
public:
int findExtra(int n, int arr1[], int arr2[]) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr1[mid] == arr2[mid]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return low;
}
};Java
class Solution {
public int findExtra(int n, int arr1[], int arr2[]) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (mid < arr2.length && arr1[mid] == arr2[mid]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return low;
}
}Python
class Solution:
def findExtra(self, n, a, b):
low, high = 0, n - 1
while low <= high:
mid = (low + high) // 2
if mid < len(b) and a[mid] == b[mid]:
low = mid + 1
else:
high = mid - 1
return lowContribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated