Google | Snapshot (OA) | Maximum unique pairs that form a sum
Anonymous User
4399

Could anyone help with the following question:

You are given an array of integers. Your task is to create pairs of them, such that every created pair has the same sum. This sum is NOT specified, but the number of created pairs should be the maximum possible. Each array element may belong to one pair only. Write a function: public int solution(int[] A) that given an array A of N integers, returns the maximum possible number of pairs with the same sum. Examples:

  1. A = [1, 9, 8, 100, 2], should return 2: the pairs are [1, 9] and [8, 2] for a sum of 10
  2. A = [2, 2, 2, 3], should return 1: [2, 2] (sum 4) OR [2, 3] (sum 5). Notice we can only form sum 4 once since there is overlap between the elements
  3. A = [2, 2, 2, 2, 2], should return 2: [2, 2] and [2, 2] for a sum for 4. The fifth 2 is not used, while the first four 2's are used.

How is the difficulty of this? I thought this was hard.

Comments (8)