i have used vector , it is giving me error , dont know why ? please help
Median of sorted array error
ll n;
cin >> n;
vector<ll>v1, v2;
vector<ll> v(2 * n);
f(i, 0, n) {
ll x;
cin >> x;
v1.pb(x);
}
f(i, 0, n) {
ll x;
cin >> x;
v2.pb(x);
}
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v.begin());
cout << (v[n] + v[n - 1]) / 2;
thanks for debugging , but please why declaring vector without size solves this problem ???
because your vector size is initially is n and you push another n elements in it… so size becomes 2n
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.