Problem Statement
* 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.
Definition
- Class:
- NoSumOrProd
- Method:
- getFirst
- Parameters:
- int[]
- Returns:
- int
- Method signature:
- int getFirst(int[] numbers)
- (be sure your method is public)
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
{1, 2, 3}
Returns: 1
{3, 2, 1}
Returns: 2
By the first note, 1 + 1 cannot be used.
{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.
{20, -30, 10, 10, -15, -15, 0, 0}
Returns: -1
0 cannot be returned because 0 times any other number is 0
{0, 0, 0, 0}
Returns: -1
{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.
{3, 4, 83, 12, 120, 39, 12, 2, 431, 43, 2, 1, 29, 491, 2, 49}
Returns: 83
{3, 2, 1}
Returns: 2
{64, 29, 76, 34, 98, 12, 3, 43, 45, 23, 52, 52, 23, 54, 64, 23, 0, 0}
Returns: 29
{1, 1, 0}
Returns: 0
{-123, -123, -23, 123, 32, -12, 523, -23, -23, 22}
Returns: -123
{-45, -22, -23, 0, -22, -23, 0, 0}
Returns: -1
{3, 1, 2}
Returns: 1
{3, 2, 1}
Returns: 2
{3, 4, 5, 35, 3, 0}
Returns: 4
{0, 0, 0, 0}
Returns: -1
{20, -30, 10, 10, -15, -15, 0}
Returns: 0
{20, -30, 10, 10, -15, -15, 0, 0}
Returns: -1
{20, -30, 10, 10, -15, -15, 0}
Returns: 0
{1, 50, 1000}
Returns: 1
{3, 2, 1}
Returns: 2
{0, 0, 1, 1, 2, 3, 9}
Returns: 9
{6, 3, 2}
Returns: 3
{20, -30, 10, 10, -15, -15, 0}
Returns: 0
{6, 3, 1}
Returns: 6
{4, 2, 2}
Returns: 2
{2, 2, 3}
Returns: 2
{5, 2, 3}
Returns: 2
{3, 3, 3}
Returns: 3
{5, 5, 10, 10, 15, 15, 0, -5, 0, 10, 15, 20, 25, -15, -20, -19, 3, 14}
Returns: -19
{12, 3, 4}
Returns: 3
{16, 4, 2}
Returns: 16
{20, -30, 10, 10, -15, -15, 0, 0}
Returns: -1
{3, 2, 1}
Returns: 2
{-1, 3, -1}
Returns: -1
{4, 2, 1}
Returns: 4
{-30, -15, -15}
Returns: -15
{1, 2, 3, 4}
Returns: 1
{20, 10, 5}
Returns: 20
{2, 1, 1}
Returns: 1
{3, 4, 5, 35, 3, 0}
Returns: 4
{0, 0, 0, 1}
Returns: 1
{1, 2, 3}
Returns: 1
{1, 6, 9}
Returns: 1