Ctrl K

A Number and Its Opposite

Easy
You get an integer array values that contains no zeros. Find the largest positive number k such that -k also appears in the array. Return that k. If there is no such number, return -1.
Example 1
values=[5, -2, 8, 2, -5, 3] → 5

Both 2 and 5 have their opposites in the array; 8 and 3 do not. The larger one is 5.

Example 2
values=[4, 6, -1, -3] → -1

No positive number has its negative counterpart, so the answer is -1.

Example 3
values=[-9, 9] → 9
Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • -1000 ≤ values[i] ≤ 1000
  • values[i] ≠ 0
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValues in Both Lists EasyShared Values With Repeats EasyTwice as Much EasyAverages of Extreme Pairs EasyA Subsequence of One Word Only Medium
loading Python…
Input
values=[5, -2, 8, 2, -5, 3]
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 115 tests on the server, hidden ones included.