-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiscellaneous.cpp
More file actions
38 lines (30 loc) · 988 Bytes
/
miscellaneous.cpp
File metadata and controls
38 lines (30 loc) · 988 Bytes
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
#include<bits/stdc++.h>
using namespace std;
int search(int element,vector<int> &a){
int low = 0,high= (a.size()-1)/2;
int ans = -1, ind = -1;
while(low<=high){
int mid = (low + high) / 2;
mid *= 2; // Adjust mid to account for the even index
if(a[mid]<=element){
ans=a[mid];
ind = mid;
low = mid/2+1;
}
else high = mid/2-1;
}
return ind; // Return the index of the found element or the just smaller element
}
int main(){
vector<int> a = {5,32,9,18,17,15,20,22,24,7};
int target = 18;
int n = a.size();
int justSmallerIndex1 = search(target, a);
int justSmallerIndex2 = search(a[justSmallerIndex1+1],a);
if(a[justSmallerIndex2+1] == target){
cout << "Element found at index: " << justSmallerIndex2 + 1 << endl;
} else {
cout << "Element not found, just smaller element is at index: " << justSmallerIndex2 + 1 << endl;
}
return 0;
}