FastPrepFastPrep
Problem Brief

Get Distinct Pairs (Also apply to AS intern)

INTERNNEW GRADOA
See Amazon online assessment and hiring insights

A financial strategist at Amazon Web Services (AWS) is analyzing a collection of profitable investments, each represented by an integer array. Every value in the array indicates the annual gain of a particular investment. The strategist's goal is to identify all unique investment pairs whose combined annual returns exactly match a given target value.

Unique pairs are defined as combinations that vary by at least one element (i.e., their values are not at the exact same positions or do not have identical values in identical positions).

Given the array of gains, compute the number of unique investment pairs whose sum equals the specified target return.

Function Description

Complete the function getDistinctPairs in the editor.

getDistinctPairs has the following parameter(s):

  1. 1. int investmentReturns[n]: an array of integers representing each investment’s yearly gain
  2. 2. goal: an integer denoting the targeted combined return

Returns

int: the total number of unique pairs that meet the target return

1Example 1

Input
stocksProfit = [5, 7, 9, 13, 11, 6, 6, 3, 3], target = 12
Output
3
Explanation
There are four pairs whose combined gains equal the target return of 12. However, since the array includes duplicate values of 3, there are two versions of the pair (9, 3): one between positions 2 and 7, and another between positions 2 and 8. But only one of these can be counted to maintain uniqueness. Therefore, the valid and unique pairs are:
  • (5, 7)
  • (3, 9)
  • (6, 6)
  • We return 3.

    Constraints

    Limits and guarantees your solution can rely on.

  • 1 ≤ n ≤ 5 x 10^5
  • 0 ≤ investmentReturns[i] ≤ 10^9
  • 0 ≤ goal ≤ 5 x 10^9
  • public int getDistinctPairs(int[] investmentReturns, int goal) {
      // write your code here
    }
    
    Input

    stocksProfit

    [5, 7, 9, 13, 11, 6, 6, 3, 3]

    target

    12

    Output

    3

    Sign in to submit your solution.