**Given an integer X, find the number of pairs (i, j) such that A[i] + B[j] ≤ X**

Note: It is given that the above question can be solved in O(n), also, we may assume that the array given are sorted. Here’s my approach:

```
int count_pairs(vector<ll> a, vector<ll> b, ll x)
{
int ap = 0,bp = 0;
int c = 0;
while(ap<a.size() && bp<b.size() && a[ap]+b[bp]<=x)
{
ap++;
bp++;
c=ap*ap;
}
while(ap<a.size() && a[ap]+b[0]<=x)
{
for(int i=0; i<bp; ++i)
{
if(a[ap]+b[i]>x)
break;
c++;
}
ap++;
}
return c;
}
```

However, we can see that this is not an optimal solution. So what should be the approach?