Count Pairs Whose Sum Is Greater Than Each Query

Quick Overview

Given a list of integer pairs and a list of query values, return for each query how many pairs have a sum strictly greater than it. The problem tests answering many threshold queries faster than scanning every pair, along with careful handling of the strict comparison and repeated sums.

Count Pairs Whose Sum Is Greater Than Each Query

Company: PayPal

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a 2D integer array `pairs`, where `pairs[i] = [a_i, b_i]` holds a pair of numbers, and an integer array `queries`. For each `queries[j]`, count how many pairs have a sum **strictly greater** than `queries[j]`. More formally, for each query `queries[j]`, find the number of indices `i` such that `pairs[i][0] + pairs[i][1] > queries[j]`. Return an array `answer` where `answer[j]` is the answer to the `j`-th query. ### Function Signature ```python def count_pairs(pairs: list[list[int]], queries: list[int]) -> list[int]: ``` ### Rules - `answer[j]` belongs to `queries[j]`, so the output has the same length as `queries` and keeps its order. - Pairs are counted by index. Two pairs with the same values both count. - A pair whose sum equals the query does not count. ### Constraints - `1 <= len(pairs) <= 10^5` - `-10^9 <= pairs[i][0], pairs[i][1] <= 10^9` - `1 <= len(queries) <= 10^5` - `-10^9 <= queries[j] <= 10^9` ### Examples ```text Input: pairs = [[1,2],[3,4],[5,6]], queries = [5,7,10] Output: [2,1,1] ``` The pair sums are 3, 7 and 11. Two of them exceed 5, and only 11 exceeds 7 or 10. The sum 7 does not count for the query 7 because the comparison is strict. ```text Input: pairs = [[1,1],[2,2],[3,3]], queries = [1,3,6] Output: [3,2,0] ``` The pair sums are 2, 4 and 6. All three exceed 1, two exceed 3, and none exceeds 6. ```text Input: pairs = [[-5,2],[0,0],[4,-4],[10,-1]], queries = [0,-3,-4,9] Output: [1,3,4,0] ``` The pair sums are -3, 0, 0 and 9. Only 9 exceeds 0, the two zeros and 9 exceed -3, all four exceed -4, and nothing exceeds 9. The two zero sums come from different pairs, so both count.

Overview: Given a list of integer pairs and a list of query values, return for each query how many pairs have a sum strictly greater than it. The problem tests answering many threshold queries faster than scanning every pair, along with careful handling of the strict comparison and repeated sums.

PayPal logo
PayPal
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a 2D integer array pairs, where pairs[i] = [a_i, b_i] holds a pair of numbers, and an integer array queries. For each queries[j], count how many pairs have a sum strictly greater than queries[j].

More formally, for each query queries[j], find the number of indices i such that pairs[i][0] + pairs[i][1] > queries[j].

Return an array answer where answer[j] is the answer to the j-th query.

Function Signature

def count_pairs(pairs: list[list[int]], queries: list[int]) -> list[int]:

Rules

  • answer[j] belongs to queries[j] , so the output has the same length as queries and keeps its order.
  • Pairs are counted by index. Two pairs with the same values both count.
  • A pair whose sum equals the query does not count.

Constraints

  • 1 <= len(pairs) <= 10^5
  • -10^9 <= pairs[i][0], pairs[i][1] <= 10^9
  • 1 <= len(queries) <= 10^5
  • -10^9 <= queries[j] <= 10^9

Examples

Input: pairs = [[1,2],[3,4],[5,6]], queries = [5,7,10]
Output: [2,1,1]

The pair sums are 3, 7 and 11. Two of them exceed 5, and only 11 exceeds 7 or 10. The sum 7 does not count for the query 7 because the comparison is strict.

Input: pairs = [[1,1],[2,2],[3,3]], queries = [1,3,6]
Output: [3,2,0]

The pair sums are 2, 4 and 6. All three exceed 1, two exceed 3, and none exceeds 6.

Input: pairs = [[-5,2],[0,0],[4,-4],[10,-1]], queries = [0,-3,-4,9]
Output: [1,3,4,0]

The pair sums are -3, 0, 0 and 9. Only 9 exceeds 0, the two zeros and 9 exceed -3, all four exceed -4, and nothing exceeds 9. The two zero sums come from different pairs, so both count.