Statistics

Problem Statement for "Sum21"

Problem Statement

You are given a list of integers in int[] x. We wish to select two integers from the list that add up to 21. Return the number of ways we can do this.

Definition

Class:
Sum21
Method:
countWays
Parameters:
int[]
Returns:
int
Method signature:
int countWays(int[] x)
(be sure your method is public)

Notes

  • In the case of duplicate integers appearing in the list, each one may be used separately (see example #2).

Constraints

  • x will contain between 1 and 50 elements, inclusive.
  • Each element of x will be between 1 and 20, inclusive.

Examples

  1. { 1, 20 }

    Returns: 1

    This is the simplest case, where the only thing to do is take both elements, which thus add up to 21.

  2. { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

    Returns: 0

    There's a lot of choices, but no pair will add up to 21.

  3. { 10, 10, 11, 11 }

    Returns: 4

    There's a total of four ways to select a pair that adds to 21.

  4. { 18, 3, 6, 17, 9, 4, 2, 11, 8, 19 }

    Returns: 3

  5. { 18, 3, 6, 17, 9, 4, 2, 11, 8, 19, 3, 6, 17, 9, 4, 2, 11, 8, 19 }

    Returns: 10

  6. { 8 }

    Returns: 0

    There's no way to even select a pair of numbers when there's only one in the list.

  7. {1, 1, 1, 1, 20, 20, 20 }

    Returns: 12

  8. {10, 10, 11, 11 }

    Returns: 4

  9. {1, 2 }

    Returns: 0

  10. {1, 20 }

    Returns: 1

  11. {1, 1, 1, 20, 20, 20 }

    Returns: 9

  12. {1, 1, 1, 20, 20 }

    Returns: 6

  13. {10, 11 }

    Returns: 1


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: