-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day_93.cpp
58 lines (46 loc) · 1.41 KB
/
Day_93.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
DAY 93: Index of an extra element present in one sorted array.
https://www.geeksforgeeks.org/find-index-of-an-extra-element-present-in-one-sorted-array/
QUESTION : 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.
Expected Time Complexity: O(log n)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^7
0 ≤ arr[i] <= 10^7
*/
#include <bits/stdc++.h>
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 (arr1[mid]==arr2[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]);
// Solve is passed both arrays
cout << findExtra(arr1, arr2, n);
return 0;
}