Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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:
Input: nums = [-1,2,-3,3] Output: 3 Explanation: 3 is the only valid k we can find in the array.
Example 2:
Input: nums = [-1,10,6,7,-7,1] Output: 7 Explanation: Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
Example 3:
Input: nums = [-10,8,6,7,-2,-3] Output: -1 Explanation: There is no a single valid k, we return -1.
Constraints:
1 <= nums.length <= 1000-1000 <= nums[i] <= 1000nums[i] != 0Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Two Pointers
[-1,2,-3,3]
[-1,10,6,7,-7,1]
[-10,8,6,7,-2,-3]
two-sum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2441: Largest Positive Integer That Exists With Its Negative
class Solution {
public int findMaxK(int[] nums) {
int ans = -1;
Set<Integer> s = new HashSet<>();
for (int x : nums) {
s.add(x);
}
for (int x : s) {
if (s.contains(-x)) {
ans = Math.max(ans, x);
}
}
return ans;
}
}
// Accepted solution for LeetCode #2441: Largest Positive Integer That Exists With Its Negative
func findMaxK(nums []int) int {
ans := -1
s := map[int]bool{}
for _, x := range nums {
s[x] = true
}
for x := range s {
if s[-x] && ans < x {
ans = x
}
}
return ans
}
# Accepted solution for LeetCode #2441: Largest Positive Integer That Exists With Its Negative
class Solution:
def findMaxK(self, nums: List[int]) -> int:
s = set(nums)
return max((x for x in s if -x in s), default=-1)
// Accepted solution for LeetCode #2441: Largest Positive Integer That Exists With Its Negative
use std::collections::HashSet;
impl Solution {
pub fn find_max_k(nums: Vec<i32>) -> i32 {
let s = nums.into_iter().collect::<HashSet<i32>>();
let mut ans = -1;
for &x in s.iter() {
if s.contains(&-x) {
ans = ans.max(x);
}
}
ans
}
}
// Accepted solution for LeetCode #2441: Largest Positive Integer That Exists With Its Negative
function findMaxK(nums: number[]): number {
let ans = -1;
const s = new Set(nums);
for (const x of s) {
if (s.has(-x)) {
ans = Math.max(ans, x);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.