File: largest-positive-integer-that-exists-with-its-negative/solution.py

Date: 2026-06-06

Time: 17:16

Purpose

This file solves LeetCode 2441: Largest Positive Integer That Exists With Its Negative. It exports a single function find_K that finds the largest positive integer k in an array where -k is also present. It's one of hundreds of problem solutions in the leetcode-implementations repo, each living in its own directory alongside tests and review artifacts.

Key Components

find_K(nums: list[int]) -> int — The sole public function. Contract: given a list of non-zero integers, return the largest positive k where both k and -k exist, or -1 if no such pair exists.

Patterns

The solution follows a set-lookup idiom: convert the input to a set for O(1) membership testing, then iterate over positive values checking for their negation. This is the standard approach for "does a complement exist?" problems (same family as Two Sum with a set).

The iteration is over num_set rather than nums — this avoids redundant checks when duplicates are present, though it doesn't change the asymptotic complexity.

Dependencies

Imports: None — pure stdlib Python using only built-in set and max.

Imported by: The corresponding testsolution.py in the same directory, plus (based on the metadata) hundreds of other test files across the repo. That's almost certainly an artifact of the "Imported By" analysis picking up a shared test harness or runner, not actual imports of findK from unrelated problem directories.

Flow

1. Build num_set from nums — O(n) time, O(n) space.

2. Initialize result = -1 (the sentinel for "no pair found").

3. Iterate over each unique value in the set. For each positive value, check if its negation exists in the set. If so, update result via max.

4. Return result.

Total: O(n) time, O(n) space.

Invariants

Error Handling

None. The function trusts that inputs conform to the problem constraints (non-zero integers, list length 1–1000). No validation, no exceptions. This is typical for LeetCode solutions where the caller guarantees valid input.

Topics to Explore

Beliefs