Count the Number of Fair Pairs - Binary Search & Sorting [JS]

Description 

Solution: Binary Search & Sorting

First, sort nums in asc order (order doesn't matter)
Then for each index i, binary search for the lower and upper bound indices:

  1. Binary search for the smallest index j where nums[i] + nums[j] >= lower (and j > i)
  2. Binary search for the largest index j where nums[i] + nums[j] <= upper (and j > i)
    The number of pairs with the index i = upperBound - lowerBound + 1

Time Complexity: O(n log(n))
Space Complexity: O(log(n)) (space for sorting)

var countFairPairs = function(nums, lower, upper) {
  let n = nums.length, pairs = 0;
  nums.sort((a, b) => a - b);
  for (let i = 0; i < n - 1; i++) {
    let lowerBound = lower_bound(i); 
    let upperBound = upper_bound(i);
    pairs += Math.max(0, upperBound - lowerBound + 1);
  }
  return pairs;
  
  function lower_bound(i) {
    // Find smallest index j where nums[i] + nums[j] >= lower
    let low = i + 1, high = n - 1;
    while (low < high) {
      let mid = Math.floor((low + high) / 2);
      if (nums[mid] + nums[i] >= lower) high = mid;
      else low = mid + 1;
    }
    return nums[low] + nums[i] >= lower ? low : n;
  }
  
  function upper_bound(i) {
    // Find largest index j where nums[i] + nums[j] <= upper
    let low = i + 1, high = n - 1;
    while (low < high) {
      let mid = Math.ceil((low + high) / 2);
      if (nums[mid] + nums[i] <= upper) low = mid;
      else high = mid - 1;
    }
    return nums[low] + nums[i] <= upper ? low : i;
  }
};

Comments

Popular posts from this blog

Maximum Value of an Ordered Triplet II - Two Solutions [JS]

Maximum Sum of Distinct Subarrays With Length K - Sliding Window w/ Two Pointers & Set [JS]

Sum of Prefix Scores of Strings - Trie [JS]

Maximum Count of Positive Integer and Negative Integer - Binary Search [JS]

Count Subarrays With Median K - Count Left & Right Balance [JS]