Core Idea
After sorting the array, pairs can be counted by fixing the left endpoint and finding how far the right endpoint can move while the sum stays below x.
Algorithm
Use two pointers. If a[l] + a[r] < x, then every index from l + 1 through r pairs with l; add that count and move l. Otherwise move r left.
Common Mistakes
The answer can be quadratic in size, so store it in 64-bit integer type. Do not double-count pairs; the two-pointer method counts each unordered pair once.