TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

SumPairs

Problem statement, definition, constraints, and public examples.

Problem Statement

Given a list of distinct integers, return how many different pairs of them sum up to result. A pair is any 2 numbers from the given values. 2 pairs are different if any of their elements are different.

Definition

Class:
SumPairs
Method:
howMany
Parameters:
int[], int
Returns:
int
Method signature:
int howMany(int[] values, int result)
(be sure your method is public)

Constraints

  • values will contain between 2 and 50 elements inclusive.
  • Each element of values will be between -1000 and 1000 inclusive
  • Each element of values will be distinct.
  • result will be between -50000 and 50000 inclusive.

Examples

  1. {2,3}
    5
    Returns: 1
    The only pair consists of 2 and 3. It does indeed sum to 5.
  2. {2,3}
    10
    Returns: 0
    Same as before, but the pair doesn't sum to 10.
  3. {2,4,5,1}
    6
    Returns: 2
    The only 2 pairs of numbers that sum to 6 are (2,4) and (5,1). Note, there are 4 other pairs that do not work.
  4. {1,2,3,4,5,6,7,8,9,10}
    11
    Returns: 5
Back to all problems