I dont understand why my solution using vector isnt corrent btt using array it is.
#include <bits/stdc++.h>
#include
using namespace std;
typedef long long int lld;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
lld t = 1;
cin >> t;
while (tβ)
{
lld n;
cin >> n;
vector arr;
for (lld i = 0; i < n; i++)
{
lld a;
cin >> a;
arr.push_back(a);
}
//sort(arr.begin(), arr.end());
lld q;
cin >> q;
while (qβ)
{
lld x, y;
cin >> x >> y;
lld sum = x + y;
lld index = lower_bound(arr.begin(), arr.end(), sum) - arr.begin();
if (arr[index] == sum)
{
cout << -1 << endl;
}
else
{
cout << index << endl;
}
}
}
return 0;
}
solution using arrays
#include <bits/stdc++.h>
#include
using namespace std;
typedef long long int lld;
int main()
{
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
lld t = 1;
cin >> t;
while (tβ)
{
lld n;
cin >> n;
lld arr[n];
for (lld i = 0; i < n; i++)
{
cin >> arr[i];
}
//sort(arr.begin(), arr.end());
lld q;
cin >> q;
while (qβ)
{
lld x, y;
cin >> x >> y;
lld sum = x + y;
lld index = lower_bound(arr, arr + n, sum) - arr;
if (arr[index] == sum)
{
cout << β-1β << endl;
}
else
{
cout << index << endl;
}
}
}
return 0;
}