Connection Status:
Competition Arena > NoSumOrProd
SRM 90 · 2002-05-21 · by PabloGilberto
Class Name: NoSumOrProd
Return Type: int
Method Name: getFirst
Arg Types: (vector<int>)
Problem Statement

Problem Statement

Return an integer, I, that appears in a given int[] and satifies the following requirements. If more than one element satisfies the requirements, your method should return the element satisfying the requirements which appears first in the int[].
* The sum of no two other integers in the list equals I.
* The product of no two other integers in the list equals I.
If no such integer exists, return -1.

Notes

  • When determining if numbers[a] and numbers[b] add or multiply to numbers[c], a, b, and c must be different.

Constraints

  • numbers contains between 3 and 50 ints, inclusive.
  • Elements of numbers are between -1000 and 1000, inclusive.
Examples
0)
{1, 2, 3}
Returns: 1
1)
{3, 2, 1}
Returns: 2

By the first note, 1 + 1 cannot be used.

2)
{20, -30, 10, 10, -15, -15, 0}
Returns: 0

Note 10 + 10 = 20 and -15 + -15 = -30 invalidate 20 and -30 because there are two 10's and two -15's.

3)
{20, -30, 10, 10, -15, -15, 0, 0}
Returns: -1

0 cannot be returned because 0 times any other number is 0

4)
{0, 0, 0, 0}
Returns: -1
5)
{3, 4, 5, 35, 3, 0}
Returns: 4

Note 4, 5, 35 and 0 all satisfy the properties, but 4 appears first in the int[] so it is the returned value.

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

Coding Area

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

Submitting as anonymous