FastPrepProject Estimates

Project Estimates

Goldman Sachs logoGoldman Sachs● EasyFULLTIMEINTERNOA
Learn

Problem statement

A number of bids are being taken for a project. Determine the number of distinct pairs of project costs where their absolute difference is some target value. Two pairs are distinct if they differ in at least one value.

Complete the function countPairs in the editor.

countPairs has the following parameter(s):

  • int projectCosts[n]: array of integers
  • int target: the target difference
  • int: the number of distinct pairs in projectCosts with an absolute difference of target

Function

countPairs(n: int, projectCosts: int[], target: int) → int

Examples

Example 1

n = 5projectCosts = [1, 5, 3, 4, 2]target = 2return = 3

Count the number of pairs in projectCosts whose difference is target = 2. The following three pairs meet the criterion: (1, 3), (5, 3), and (4, 2).

Example 2

n = 3projectCosts = [1, 3, 5]target = 2return = 2

There are 2 pairs [1, 3], [3, 5] that have the target difference target = 2, therefore a value of 2 is returned.

Constraints

  • 5 < n <= 2 * 105
  • 0 < projectCosts[i] <= 2 * 109
  • Each projectCosts[i] is distinct, i.e. unique within projectCosts
  • 1 <= target<= 109

More Goldman Sachs problems

See Goldman Sachs hiring insights
public int countPairs(int n, int[] projectCosts, int target) {
    // write your code here
}
n5
projectCosts[1, 5, 3, 4, 2]
target2
expected3
Checking account…