Problem Statement
You are given a list of integers in
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, 20 }
Returns: 1
This is the simplest case, where the only thing to do is take both elements, which thus add up to 21.
{ 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.
{ 10, 10, 11, 11 }
Returns: 4
There's a total of four ways to select a pair that adds to 21.
{ 18, 3, 6, 17, 9, 4, 2, 11, 8, 19 }
Returns: 3
{ 18, 3, 6, 17, 9, 4, 2, 11, 8, 19, 3, 6, 17, 9, 4, 2, 11, 8, 19 }
Returns: 10
{ 8 }
Returns: 0
There's no way to even select a pair of numbers when there's only one in the list.
{1, 1, 1, 1, 20, 20, 20 }
Returns: 12
{10, 10, 11, 11 }
Returns: 4
{1, 2 }
Returns: 0
{1, 20 }
Returns: 1
{1, 1, 1, 20, 20, 20 }
Returns: 9
{1, 1, 1, 20, 20 }
Returns: 6
{10, 11 }
Returns: 1