Advertisement

Count the Number of Fair Pairs - LeetCode 2563 Solution

Count the Number of Fair Pairs - Complete Solution Guide

Count the Number of Fair Pairs is LeetCode problem 2563, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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: 0 <= i < j < n , and lower <= nums[i] + nums[j] <= upper 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: 1 <= nums.leng

Detailed Explanation

The problem asks us to count the number of 'fair pairs' in a given array `nums`. A fair pair `(i, j)` is defined as a pair of indices where `0 <= i < j < n` (meaning `i` is strictly less than `j`, and both are valid indices in the array) and `lower <= nums[i] + nums[j] <= upper`. We are given the array `nums` and the integer bounds `lower` and `upper` as input, and we need to return the total count of such fair pairs.

Solution Approach

The solution involves sorting the input array and then using a two-pointer technique to count the number of pairs whose sum falls within the given range. The core idea is to count the number of pairs with a sum less than or equal to `upper` and then subtract the number of pairs with a sum less than `lower` (which is equivalent to less than or equal to `lower - 1`). This difference gives us the count of fair pairs.

Step-by-Step Algorithm

  1. Step 1: Sort the input array `nums` in ascending order using a standard sorting algorithm (e.g., merge sort, quicksort).
  2. Step 2: Define a helper function `count_less_equal(k)` that counts the number of pairs `(i, j)` such that `i < j` and `nums[i] + nums[j] <= k`. This function uses a two-pointer approach.
  3. Step 3: In the `count_less_equal(k)` function, initialize two pointers, `left` to 0 and `right` to `len(nums) - 1`.
  4. Step 4: While `left < right`, check if `nums[left] + nums[right] <= k`. If it is, then all pairs `(left, left+1)`, `(left, left+2)`, ..., `(left, right)` satisfy the condition, so increment the count by `right - left`. Then increment `left` to explore larger sums.
  5. Step 5: If `nums[left] + nums[right] > k`, it means the current sum is too large. Decrement `right` to try smaller sums.
  6. Step 6: Return the count from the `count_less_equal(k)` function.
  7. Step 7: Calculate the result as `count_less_equal(upper) - count_less_equal(lower - 1)` and return it.

Key Insights

  • Insight 1: Sorting the array allows us to efficiently find pairs that sum up to a value within the specified range (lower, upper) using a two-pointer approach.
  • Insight 2: Instead of directly counting fair pairs in the range [lower, upper], we can count pairs whose sum is less than or equal to `upper` and subtract the count of pairs whose sum is less than `lower`. This simplifies the counting process.
  • Insight 3: It's crucial to consider that `i` must be strictly less than `j`. The two-pointer approach must maintain this invariant.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(1)

Topics

This problem involves: Array, Two Pointers, Binary Search, Sorting.

Companies

Asked at: Qualcomm, Salesforce.