This is the code that I have written in response to the counting pairs problem but it is giving tle. Could anyone please point out the reason? Thanks
#include <bits/stdc++.h>
#define ll long long
#define fo(i, a, n) for (int i = a; i < n; ++i)
#define whi(t) \
int t; \
cin >> t; \
while (t--)
#define vi(v) vector<ll int> v
using namespace std;
int b_search(int l, int k, vector<ll int> A)
{
ll int high = A.size() - 1, mid, low = l + 1;
//cout << A[low] << endl;
while (low <= high)
{
mid = (low + high) / 2;
if (A[mid] == k)
{
return 1;
}
else if (A[mid] < k)
{
low = mid + 1;
}
else
high = mid - 1;
//cout << A[mid] << "*" << A[low] << " " << A[high] << "\n";
}
return 0;
}
int main()
{
ios::sync_with_stdio(false);
ll int n, k, count;
cin >> n >> k;
vi(A)(n, 0);
for (int i = 0; i < n; ++i)
{
cin >> A[i];
}
count = 0;
sort(A.begin(), A.end());
for (int i = 0; i < n - 1; ++i)
{
count += b_search(i, k + A[i], A);
}
cout << count;
}