Largest Positive Integer That Exists With Its Negative

Given an integer array nums that does not contain any zeros, find the largest positive integer k such that -k also exists in the array.

Return the positive integer k. If there is no such integer, return -1.

Example 1
Inputnums = [-1,2,-3,3]
Output3
3 is the only valid k we can find in the array.
Example 2
Inputnums = [-1,10,6,7,-7,1]
Output7
Both 1 and 7 have their corresponding negative values in the array, and 7 has a larger value.

Constraints

  • 1 <= nums.length <= 1000
  • -1000 <= nums[i] <= 1000
  • nums[i] != 0

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate