#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll lower_bound(vector<ll> arr, ll tar, ll n)
{
ll l = 0, h = n - 1;
ll ans = n;
while (l <= h)
{
ll mid = l + (h - l) / 2;
if (arr[mid] >= tar)
{
ans = mid;
h = mid - 1;
}
else
{
l = mid + 1;
}
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ll n;
cin >> n;
vector<ll> arr(n, 0);
for (ll i = 0; i < n; i++)
{
cin >> arr[i];
}
ll q;
cin >> q;
while (q--)
{
ll val;
cin >> val;
ll ans = lower_bound(arr, val, n);
// cout << ans << endl;
if (ans == n)
{
cout << "No " << n << endl;
}
else if (arr[ans] == val)
{
cout << "Yes " << ans + 1 << endl;
}
else if (arr[ans] != val)
{
cout << "No " << ans + 1 << endl;
}
}
return 0;
}
I have implemented the Lower Bound algorithm without using the standard library. But Why I am getting time limit exceeded for the above code?
Is the STL's std::lower_bound()
faster than the above code?
The above code is \$O(n \log n)\$.
Problem Link: https://www.hackerrank.com/challenges/cpp-lower-bound/leaderboard
arr
every time you calllower_bound
, because the parameter is passed by value. Make it aconst
reference and the TLE probably goes away. \$\endgroup\$arr
when you pass it by value to the functionlower_bound
. :-) \$\endgroup\$