Connection Status:
Competition Arena > ListOps
SRM 109 · 2002-08-14 · by brett1479
Class Name: ListOps
Return Type: int
Method Name: howMany
Arg Types: (vector<int>)
Problem Statement

Problem Statement

Given a list of numbers you will get all of the unique unordered pairs that are not of the form (x,x) (i.e. (1,1) isn't allowed). Since these pairs are unordered (a,b) and (b,a) are the same pair and do not count twice (e.g. (1,2) and (2,1) are the same pair and only count as one pair). For example: numbers = {1,2,3}
Then the pairs are (1,2),(1,3),(2,3). Also:

numbers = {1,3,2,1,3,2} Then the pairs are still (1,2),(1,3),(2,3).

Then your method will return how many unique unordered pairs will either multiply, add, divide, or subtract to produce a value in the list. You must abide by these rules:
- When considering a pair like (2,3) the operands of the operation may be in any order (i.e. you can try 2-3 and 3-2)
- Division is real division, so 3/2 = 1.5
- Division by 0 will never produce a number in the list

numbers = {1,2,3}
(1,2) : 1+2 = 3 (note that 1*2 is also an element, but we only count the pair once)
(1,3) : 3/1 = 3
(2,3) : 3-2 = 1
Three of the pairs can produce values in the list, so your method would return 3.

Create a class ListOps that contains the method howMany, which takes an int[] numbers and returns an int representing how many pairs of numbers can be used to produce other numbers in the list.

Constraints

  • numbers will contain between 2 and 50 elements, inclusive.
  • Each element of numbers will be between -10000 and 10000, inclusive.
Examples
0)
{1, 2, 3}
Returns: 3

This is the example from above.

1)
{3, 2}
Returns: 0

There is no possible way to produce any element of the list.

2)
{-1000, 0, -500, 23, 4000, 9000, 1231, 23}
Returns: 7
3)
{14, 0, 1239, -10, -10000, 10000, 9999}
Returns: 7
4)
{0, 1, 0}
Returns: 1

Submissions are judged against all 43 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class ListOps with a public method int howMany(vector<int> numbers) · 43 test cases · 2 s / 256 MB per case

Submitting as anonymous