Find the Largest Positive Value with a Negative Counterpart

Quick Overview

Find the largest positive array value that also has its negative present, returning -1 when no such value exists.

Find the Largest Positive Value with a Negative Counterpart

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem In nonzero integer array `nums`, find the greatest positive integer `k` for which both `k` and `-k` appear. Return `k`, or `-1` if no such pair of values exists. ```cpp class Solution { public: int findMaxK(vector<int>& nums); }; ``` ### Examples **Example 1:** ```text Input: nums = [-5,5,2,-2,9] Output: 5 Explanation: Magnitudes2 and 5 have both signs, while9 lacks-9. ``` **Example 2:** ```text Input: nums = [-4,7,1] Output: -1 Explanation: No value has its opposite sign present. ``` ### Constraints - `1 <= nums.length <= 1000`. - `-1000 <= nums[i] <= 1000`, and no entry is 0. ### Clarifying Questions - Is the result positive when a pair exists? Yes. - Can two copies of the same sign form a pair? No; both signs must appear. - Can zero appear? No. - Does occurrence order matter? No. ```hint Check the boundary Check a duplicated positive value whose negative is absent. ```

Overview: Find the largest positive array value that also has its negative present, returning -1 when no such value exists.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

In nonzero integer array nums, find the greatest positive integer k for which both k and -k appear. Return k, or -1 if no such pair of values exists.

class Solution {
public:
    int findMaxK(vector<int>& nums);
};

Examples

Example 1:

Input: nums = [-5,5,2,-2,9]
Output: 5
Explanation: Magnitudes2 and 5 have both signs, while9 lacks-9.

Example 2:

Input: nums = [-4,7,1]
Output: -1
Explanation: No value has its opposite sign present.

Constraints

  • 1 <= nums.length <= 1000 .
  • -1000 <= nums[i] <= 1000 , and no entry is 0.

Clarifying Questions Guidance

  • Is the result positive when a pair exists? Yes.
  • Can two copies of the same sign form a pair? No; both signs must appear.
  • Can zero appear? No.
  • Does occurrence order matter? No.