# https://www.codechef.com/COLE2020/problems/CLPNT/

https://www.codechef.com/COLE2020/problems/CLPNT/

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;
}

Please try to use three backticks(key left to the 1) before and after the code you post. People tend to ignore such posts.

I donβt know how you code using the vector is working , the vector type is missing.
Also i think the index you are calculating would exceed n-1 when the lower_bound returns n.

This is how i did it

``````            auto it = lower_bound(points.begin(), points.end(), c);
if(it != points.end() && *it == c) {
cout << -1 << endl;
} else {
cout << it - points.begin() << endl;
}
``````
1 Like