Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define flush cin.ignore(numeric_limits<streamsize>::max(),'\n')
- #define FASTERIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
- #define NL cout<<'\n';
- #define pi acos(-1.0) //3.1415926535897932384626
- #define pb push_back
- #define mk make_pair
- #define mx 1000005
- #define EPS 1e-10
- #define dpoint(x) fixed<<setprecision(x)
- # define my_sizeof(type) ((char *)(&type+1)-(char*)(&type))
- typedef long long int ll;
- typedef double dl;
- typedef unsigned long long int ull;
- void sort_ascending(int arr[], int n) {
- for (int i = 0; i < n - 1; i++) {
- for (int j = i + 1; j < n; j++) {
- if (arr[i] > arr[j])swap(arr[i], arr[j]);
- }
- }
- }
- int binary_search(int arr[], int key) {
- int low = 0, high = my_sizeof(arr)/my_sizeof(arr[0]);
- high -=1;
- while (low <= high) {
- int mid = low + (high - low) / 2;
- if (arr[mid] > key)high = mid - 1;
- else if (arr[mid] < key) low = mid + 1;
- else return mid;
- }
- return -1;
- }
- int main() {
- #ifdef anikakash
- clock_t tStart = clock();
- freopen("input.txt", "r", stdin);
- freopen("tmp.txt", "w", stdout);
- #endif
- FASTERIO; //cmt when use scanf & printf ;
- int n; cin >> n;
- int arr[n + 1];
- for(int i=0; i<n; i++)cin>>arr[i];
- sort_ascending(arr, n);
- int test; cin >> test;
- while (test--) {
- int key; cin >> key;
- int ans = binary_search(arr, key);
- if(ans!=-1)
- cout<<key<<" is found at index "<<ans<<endl;
- else cout<<key<<" is not found in the array"<<endl;
- }
- #ifdef anikakash
- fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
- #endif
- return 0;
- }
Add Comment
Please, Sign In to add comment