# What's wrong with my binary search?

## Question

Include a specific question if you have one.
Is there something wrong with my binary search?

## My Work

``````#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define fastio ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);

int n, m, cnt = 1, sl;

int main() {
fastio; cin >> n >> m;
int price[n], maximum[m], dem[n];
for(int i = 0; i < n; i ++) cin >> price[i];
for(int i = 0; i < m; i ++) cin >> maximum[i];
sort(price, price + n); vector <int> arr;
for(int i = 1; i < n; i ++) {
if(price[i] != price[i - 1])
dem[sl ++] = cnt, cnt = 1, arr.push_back(price[i - 1]);
else
++ cnt;
}
dem[sl] = cnt; arr.push_back(price[n - 1]);
for(int i = 0; i < m; i ++) {
int l = 0, r = arr.size() - 1, ans = -1, pos = -1;
while(l <= r) {
int mid = (l + r) / 2;
if(arr[mid] <= maximum[i]) {
if(ans < arr[mid] && dem[mid] > 0)
ans = arr[mid], pos = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
if(ans > -1)
-- dem[pos];
cout << ans << '\n';
}
}

Add explanation of code here :
I count the appearance of every member than store the sorted price members into the arr only once for each member. Then i use binary search to find the ticket which largest value does not exceed the maximum money that a person is willingly to spend to the ticket.``````