Problem Β· Array

Get Pairs Count πŸ₯‘

Learn this problem
● EasyAmazonINTERNOA
See Amazon hiring insights

Problem statement

Amazon's development team is working on a parallel-processing analysis system designed to measure the computational demand of multiple tasks. There are n tasks, and the resource consumption for completing the i-th task is given by tasks[i].

Two tasks are considered computationally equivalent if the absolute difference in their resource requirements is less than or equal to k.

Given the list tasks and the integer k, compute the total number of unique pairs of tasks that can be classified as computationally equivalent.

Function

getPairsCount(tasks: int[], threshold: int) β†’ long

Examples

Example 1

tasks = [100, 200, 300, 400]threshold = 250return = 5
The pairs of processes considered computationally equivalent are (100, 200), (100, 300), (200, 300), (200, 400), and (300, 400).

Example 2

tasks = [10, 12, 11]threshold = 0return = 0
Every pair of processes has a difference in computational resource needs that is strictly greater than zero.

Example 3

tasks = [7, 10, 13, 11]threshold = 3return = 4
Example 3 illustration
The process pairs are shown in the above image. Hence the answer is 4.

Constraints

  • 1 ≀ n ≀ 2 * 10^5
  • 1 ≀ tasks[i] ≀ 10^6
  • 0 ≀ threshold ≀ 10^6

More Amazon problems

drafts saved locally
public long getPairsCount(int[] tasks, int threshold) {
  // write your code here
}
tasks[100, 200, 300, 400]
threshold250
expected5
checking account