Leetcode Problem 2441. Largest Positive Integer That Exists With Its Negative

2441. Largest Positive Integer That Exists With Its Negative

Leetcode Solutions

Using HashSet to Find Maximum k with Negative Counterpart

  1. Initialize a HashSet to store the numbers from the array.
  2. Initialize a variable maxK to -1 to keep track of the maximum k found.
  3. Iterate through each number in the array. a. If the number is positive, check if its negative counterpart is in the HashSet. b. If the negative counterpart exists, update maxK with the maximum of maxK and the current number.
  4. After the iteration, return maxK if it's not -1, otherwise return -1.
UML Thumbnail

Sorting and Two Pointers to Find Maximum k with Negative Counterpart

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...