The Indian Engineer

Problem 2563 Count the Number of Fair Pairs

Posted on 5 mins

Array Two-Pointers Binary-Search Sorting

Problem Statement

Link - Problem 2563

Question

Given a 0-indexed integer array nums of size n and two integers lower and upper, return the number of fair pairs.

A pair (i, j) is fair if:

Example 1:

Input: nums = [0,1,7,4,4,5], lower = 3, upper = 6
Output: 6
Explanation: There are 6 fair pairs: (0,3), (0,4), (0,5), (1,3), (1,4), and (1,5).

Example 2:

Input: nums = [1,7,9,2,5], lower = 11, upper = 11
Output: 1
Explanation: There is a single fair pair: (2,3).

Constraints:

Solution

class Solution {
public:
    long long countFairPairs(vector<int>& nums, int lower, int upper) {
        sort(nums.begin(),nums.end());
        long long ans = 0;
        for(int i=0;i<nums.size(); i++){
            auto right = upper_bound(nums.begin()+i+1,nums.end(),upper-nums[i]);
            auto left = lower_bound(nums.begin()+i+1,nums.end(),lower-nums[i]);
            ans += (right-left);
        }
        return ans;

    }
};

Complexity Analysis

| Algorithm                              | Time Complexity | Space Complexity |
| -------------------------------------- | --------------- | ---------------- |
| Modified Merge Sort with Binary Search | O(n log n)      | O(1)             |

Explanation

Intial Thoughts

When approaching a problem with pairs and constraints, consider first organizing the data if possible, as seen in the use of sorting. Then, identify potential pairs by narrowing the differences in values. This includes determining a range for the acceptable values, like using lower and upper bounds. Think about using ‘sliding window’ style solutions to reduce the total number of calculations needed, as there can be pairs of pairs. One can also notice the repeated subtraction in the potential calculation of the pairs; instead, compute each difference and then set conditions.

Intuitive Analysis

For fair pairs, see the bigger picture: instead of individually inspecting pairs one by one, slice the whole list of numbers and check individual ‘head-starts’ as we proceed in our sequence – which gives a relative space to efficiently spot pairs that we care about. Observe and then minimize the steps in computing potential valid pairs – it’s better to minimize redundant calculations instead of iterating and checking for every potential situation.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Sort the array in ascending order.

For each number in the array, keep track of the smallest and largest numbers in the array that can form a fair pair with this number.

As you move to larger number, both boundaries move down.


Similar Questions:

Title URL Difficulty
Count of Range Sum https://leetcode.com/problems/count-of-range-sum Hard
Finding Pairs With a Certain Sum https://leetcode.com/problems/finding-pairs-with-a-certain-sum Medium
Count Number of Pairs With Absolute Difference K https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k Easy
Count Pairs Whose Sum is Less than Target https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target Easy