-
Notifications
You must be signed in to change notification settings - Fork 7
/
magic_index.cpp
74 lines (63 loc) · 1.6 KB
/
magic_index.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Q - A magic index in an array[0....n-1] is defined to be an index such that A[i] = i.Given a
sorted array of distinct integers, write a method to find a magic index if one exits in array A
additonal: what if values are not distinct?
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <vector>
#include <set>
#include <climits>
#include <map>
#include <string>
using namespace std;
typedef long long int lint;
/*int magic_helper(int *A, int start, int end, int A_size) { // if values are distinct & sorted.
if (end < start || start < 0 || end >= A_size) {
return -1;
}
int mid = (start + end)/2;
if (A[mid] == mid) {
return mid;
} else if (A[mid] > mid) {
magic_helper(A, start, mid - 1, A_size);
} else {
magic_helper(A, mid + 1, end, A_size);
}
}*/
int magic_helper(int *A, int start, int end, int A_size) { // if values are not distinct.
if (end < start || start < 0 || end >= A_size) {
return -1;
}
int midIndex = (start + end)/2;
int midValue = A[midIndex];
if (midValue == midIndex) {
return midIndex;
}
// search left
int left = magic_helper(A, start, min(midIndex - 1, midValue), A_size);
if (left >= 0) {
return left;
}
//search right
int right = magic_helper(A, max(midIndex + 1, midValue), end, A_size);
if (right >= 0) {
return right;
}
}
int magic_index(int *A, int start, int end) {
int A_size = end + 1;
return magic_helper(A, start, end, A_size);
}
int main() {
int N;
cin >> N;
int A[N + 1];
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
cout << magic_index(A, 0, N - 1) << endl;
return 0;
}