Statistics

Problem Statement for "NoSumOrProd"

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.

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. {1, 2, 3}

    Returns: 1

  2. {3, 2, 1}

    Returns: 2

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

  3. {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.

  4. {20, -30, 10, 10, -15, -15, 0, 0}

    Returns: -1

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

  5. {0, 0, 0, 0}

    Returns: -1

  6. {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.

  7. {3, 4, 83, 12, 120, 39, 12, 2, 431, 43, 2, 1, 29, 491, 2, 49}

    Returns: 83

  8. {3, 2, 1}

    Returns: 2

  9. {64, 29, 76, 34, 98, 12, 3, 43, 45, 23, 52, 52, 23, 54, 64, 23, 0, 0}

    Returns: 29

  10. {1, 1, 0}

    Returns: 0

  11. {-123, -123, -23, 123, 32, -12, 523, -23, -23, 22}

    Returns: -123

  12. {-45, -22, -23, 0, -22, -23, 0, 0}

    Returns: -1

  13. {3, 1, 2}

    Returns: 1

  14. {3, 2, 1}

    Returns: 2

  15. {3, 4, 5, 35, 3, 0}

    Returns: 4

  16. {0, 0, 0, 0}

    Returns: -1

  17. {20, -30, 10, 10, -15, -15, 0}

    Returns: 0

  18. {20, -30, 10, 10, -15, -15, 0, 0}

    Returns: -1

  19. {20, -30, 10, 10, -15, -15, 0}

    Returns: 0

  20. {1, 50, 1000}

    Returns: 1

  21. {3, 2, 1}

    Returns: 2

  22. {0, 0, 1, 1, 2, 3, 9}

    Returns: 9

  23. {6, 3, 2}

    Returns: 3

  24. {20, -30, 10, 10, -15, -15, 0}

    Returns: 0

  25. {6, 3, 1}

    Returns: 6

  26. {4, 2, 2}

    Returns: 2

  27. {2, 2, 3}

    Returns: 2

  28. {5, 2, 3}

    Returns: 2

  29. {3, 3, 3}

    Returns: 3

  30. {5, 5, 10, 10, 15, 15, 0, -5, 0, 10, 15, 20, 25, -15, -20, -19, 3, 14}

    Returns: -19

  31. {12, 3, 4}

    Returns: 3

  32. {16, 4, 2}

    Returns: 16

  33. {20, -30, 10, 10, -15, -15, 0, 0}

    Returns: -1

  34. {3, 2, 1}

    Returns: 2

  35. {-1, 3, -1}

    Returns: -1

  36. {4, 2, 1}

    Returns: 4

  37. {-30, -15, -15}

    Returns: -15

  38. {1, 2, 3, 4}

    Returns: 1

  39. {20, 10, 5}

    Returns: 20

  40. {2, 1, 1}

    Returns: 1

  41. {3, 4, 5, 35, 3, 0}

    Returns: 4

  42. {0, 0, 0, 1}

    Returns: 1

  43. {1, 2, 3}

    Returns: 1

  44. {1, 6, 9}

    Returns: 1


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: