FastPrepFastPrep
Problem Brief

Get Pairs Count πŸ₯‘

INTERNOA
See Amazon online assessment and hiring insights

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.

1Example 1

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

2Example 2

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

3Example 3

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

Constraints

Limits and guarantees your solution can rely on.

  • 1 ≀ n ≀ 2 * 10^5
  • 1 ≀ tasks[i] ≀ 10^6
  • 0 ≀ threshold ≀ 10^6
public long getPairsCount(int[] tasks, int threshold) {
  // write your code here
}
Input

tasks

[100, 200, 300, 400]

threshold

250

Output

5

Sign in to submit your solution.