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.
* 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.