Largest Positive Integer That Exists With Its Negative - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [-1,2,-3,3]
Output: 3
💡 Note: 3 is the largest positive integer that has its negative -3 in the array. Both 2 and 3 have their negatives, but 3 is larger.
Example 2 — No Valid Pairs
$ Input: nums = [-1,10,6,7,-7,1]
Output: 7
💡 Note: Only 7 has its negative -7 in the array. Numbers like 10, 6 don't have their negatives present.
Example 3 — No Pairs Exist
$ Input: nums = [-10,8,6,7,4,2,3]
Output: -1
💡 Note: None of the positive integers have their negative counterparts in the array.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • -1000 ≤ nums[i] ≤ 1000
  • nums[i] ≠ 0

Visualization

Tap to expand
Find Largest Positive With Its Negative-12-33Input: [-1, 2, -3, 3]2 pairs with -2? NO3 pairs with -3? YES ✓Valid Pairs Found3 ↔ -3Largest: 3Output: 33 is the largest positive integer that has its negative -3 in the array
Understanding the Visualization
1
Input Array
Array with positive and negative integers
2
Find Pairs
Identify positive numbers that have their negatives in the array
3
Return Maximum
Return the largest positive from valid pairs
Key Takeaway
🎯 Key Insight: Use a hash set for O(1) lookups to efficiently find positive-negative pairs
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
23.5K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen