-
Notifications
You must be signed in to change notification settings - Fork 1
/
fixedPoint.cpp
51 lines (44 loc) · 867 Bytes
/
fixedPoint.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
// LINEAR SEARCH
class Solution
{
public:
vector<int> valueEqualToIndex(int arr[], int n)
{
vector<int> ans;
for (int i = 0; i < n; i++)
{
if (arr[i] == (i + 1))
{
ans.push_back(i + 1);
}
}
return ans;
}
};
// BINARY SEARCH
class Solution
{
public:
int binarySearch(int arr[], int low, int high)
{
while (high >= low)
{
int mid = low + (high - low) / 2;
if (arr[mid] == mid)
return mid;
if (arr[mid] > mid)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
return -1;
}
int valueEqualToIndex(int arr[], int n)
{
return binarySearch(arr, 0, n - 1);
}
};