Count Pairs Whose Sum is Less than Target Problem answer
Complexity

Time complexity: O(Nlog(n))


Space complexity: O(1)

Code


# Complexity
- Time complexity: O(Nlog(n))

- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    int countPairs(vector<int>& nums, int target) {
        int n = nums.size();
        sort(nums.begin(), nums.end()); 
        
        int count = 0;
        int left = 0, right = n - 1;
        
        while (left < right) {
            if (nums[left] + nums[right] < target) {
              
                count += right - left;
                left++;
            } else {
             
                right--;
            }
        }
        return count;
    }
};
```
['C++']