Fundamentals/Largest K such that both K and -K exist in array
← PrevNext →
Given an array nums of integers, return the largest integer K > 0 such that both K and -K appear in nums. If no such K exists, return 0.
Input
nums: an array of integers
Output
The largest K > 0 with both K and -K present, or 0 if none exists.
Examples
Example 1
Inputnums = [3, 2, -2, 5, -3]
Output3
Example 2
Inputnums = [1, 2, 3, -4]
Output0
Constraints
1 <= nums.length <= 10^5.