# 2824. Count Pairs Whose Sum is Less than Target

## Problem Statement

<br>

Given a **0-indexed** integer array `nums` of length `n` and an integer `target`, return *the number of pairs* `(i, j)` *where* `0 <= i < j < n` *and* `nums[i] + nums[j] < target`.

&#x20;

**Example 1:**

<pre><code><strong>Input: nums = [-1,1,2,3,1], target = 2
</strong><strong>Output: 3
</strong><strong>Explanation: There are 3 pairs of indices that satisfy the conditions in the statement:
</strong>- (0, 1) since 0 &#x3C; 1 and nums[0] + nums[1] = 0 &#x3C; target
- (0, 2) since 0 &#x3C; 2 and nums[0] + nums[2] = 1 &#x3C; target 
- (0, 4) since 0 &#x3C; 4 and nums[0] + nums[4] = 0 &#x3C; target
Note that (0, 3) is not counted since nums[0] + nums[3] is not strictly less than the target.
</code></pre>

**Example 2:**

<pre><code><strong>Input: nums = [-6,2,5,-2,-7,-1,3], target = -2
</strong><strong>Output: 10
</strong><strong>Explanation: There are 10 pairs of indices that satisfy the conditions in the statement:
</strong>- (0, 1) since 0 &#x3C; 1 and nums[0] + nums[1] = -4 &#x3C; target
- (0, 3) since 0 &#x3C; 3 and nums[0] + nums[3] = -8 &#x3C; target
- (0, 4) since 0 &#x3C; 4 and nums[0] + nums[4] = -13 &#x3C; target
- (0, 5) since 0 &#x3C; 5 and nums[0] + nums[5] = -7 &#x3C; target
- (0, 6) since 0 &#x3C; 6 and nums[0] + nums[6] = -3 &#x3C; target
- (1, 4) since 1 &#x3C; 4 and nums[1] + nums[4] = -5 &#x3C; target
- (3, 4) since 3 &#x3C; 4 and nums[3] + nums[4] = -9 &#x3C; target
- (3, 5) since 3 &#x3C; 5 and nums[3] + nums[5] = -3 &#x3C; target
- (4, 5) since 4 &#x3C; 5 and nums[4] + nums[5] = -8 &#x3C; target
- (4, 6) since 4 &#x3C; 6 and nums[4] + nums[6] = -4 &#x3C; target
</code></pre>

&#x20;

**Constraints:**

* `1 <= nums.length == n <= 50`
* `-50 <= nums[i], target <= 50`<br>

## Intuition

```
Brute Force:

Optimal: Two Pointer
Sort the array and 

Start pointer from left and right
Basically, target = 2
-1 1 1 2 3
l      r

Here l and r addition is less than 2
Observe that r-l = 3 , And -1 can make pair with all in the interval
1 1 2

Similarly for all

nlogn
```

### Links

<https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target/description/>

### Video Links

### Approach 1:

```
Brute
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    int countPairs(vector<int>& nums, int target) {
        int n = nums.size();
        int ans = 0;

        for(int i=0; i<n; i++){
            for(int j=i+1; j<n; j++){
                if(nums[i] + nums[j] < target)
                    ans ++;
            }
        }

        return ans;
        
    }
};
```

{% endcode %}

### Approach 2:

```
Optimal
```

{% code title="C++" lineNumbers="true" %}

```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;
        int right = n - 1;

        while (left < right) {
            if (nums[left] + nums[right] < target) 
            {
                count += right - left;
                left++;
            } else {
                right--;
            }
        }

        return count;
    }
};
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###
