FastPrepGet Computational Equivalents

Get Computational Equivalents

Amazon logoAmazon● EasyOA
Learn

Problem statement

Developers at Amazon are building a multi-process analysis tool to analyze the computational intensity of the processes. There are n processes and the ithprocess needs process[i] computation resources for completion.

Two processes are considered to be computationally the same if their resource requirements differ by at most m.

Given the array process and an integer m, find the number of pairs of processes that are computationally the same.

Function

getComputationalEquivalents(process: int[], m: int) → int

Complete the function getComputationalEquivalents in the editor.

getComputationalEquivalents takes the following arguments:

  1. int process[n]: the computational resource requirement of the processes
  2. int m: the threshold for being computationally the same

Examples

Example 1

process = [7, 10, 13, 11]m = 3return = 4
Example 1 illustration

Hence, the answer is 4 :)

Constraints

  • 1 <= n <= 2 * 10^5
  • 1 <= process[i] <= 10^6
  • 1 <= m <= 10^6

More Amazon problems

See Amazon hiring insights
public int getComputationalEquivalents(int[] process, int m) {
  // write your code here
}
process[7, 10, 13, 11]
m3
expected4
Checking account…