Largest Positive Integer That Exists With Its Negative

Updated on 16 June, 2025
Largest Positive Integer That Exists With Its Negative header image

Problem Statement

The task is to analyze an array of integers which notably does not contain the value zero, identifying the largest positive integer k such that its negative counterpart -k is also present in the same array. If the array comprises both an integer and its negative counterpart, that integer is eligible. Among all possible integers in the array that fulfill this condition, we need to find the largest one. If no such integer exists within the array that has its negative present, the result should be -1. This problem emphasizes not only identifying matching positive-negative pairs but also determining the maximum of such integers.

Examples

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] <= 1000
  • nums[i] != 0

Approach and Intuition

To solve the problem described, the following approach can be used:

  1. Create a set to store unique elements from the array. This helps in quick look-up operations, distinguishing the existence of the negative counterpart of any positive integer.

  2. Initialize an integer max_k with a default value, for instance, -1. This will hold our result.

  3. Iterate through each integer in the array:

    • Check if the current integer is positive.
    • If positive, check whether its negative counterpart exists in the set.
    • If both the integer and its negative are found, compare this integer with max_k and update max_k to this integer if it's larger.
  4. After completing the traversal of the array, max_k will hold the largest positive integer k for which -k is also present in the array if such a k exists. If no such pair is found, max_k will remain as the initial value -1.

Example Analysis from Given Examples:

  • In Example 1 (nums = [-1,2,-3,3]), we find that 3 has its negative -3 in the array, and since it is the only positive integer with this property, it is the answer.
  • In Example 2 (nums = [-1,10,6,7,-7,1]), both 1 and 7 have their corresponding negatives but 7 is larger, thus it is our answer.
  • In Example 3 (nums = [-10,8,6,7,-2,-3]), there are no positive integers that fulfill the condition, thus output is -1.

This approach ensures the identification of the correct and largest k where possible within the constraints provided.

Solutions

  • C++
  • Java
  • Python
cpp
class Solution {
public:
    int maximumK(vector<int>& values) {
        int maxK = -1;
        bitset<2048> observed;
    
        for (int val : values) {
            int valAbs = abs(val);
            if (valAbs > maxK && observed[-val + 1024])
                maxK = valAbs;
            observed[val + 1024] = true;
        }
    
        return maxK;
    }
};

This solution defines a function maximumK that determines the largest integer k for which both k and -k exist in a given vector of integers named values. The approach utilizes a bitset of size 2048 to efficiently track which values (and their negative counterparts) exist in the vector.

  • The bitset, named observed, is initialized with a size of 2048, allowing the tracking of integers and their negatives within an assumed reasonable range by shifting the index by 1024. This ensures indices remain positive within the bitset.
  • For each integer val in the vector values, calculate valAbs, the absolute value of val.
  • If valAbs is greater than the current maxK and the bit corresponding to -val + 1024 is set in observed (indicating both val and -val have been observed so far), then update maxK to valAbs.
  • The bitset index for the current val is then set to true (observed[val + 1024] = true).
  • After processing all elements in the vector, maxK is returned. If no such integer k exists, the function returns -1.

This approach is effective in solving the problem as it operates in linear time, thanks to the O(1) complexity of bitset operations and the single pass through the vector. Thus, maximumK competently identifies the maximum k that satisfies the conditions, in an optimal runtime.

java
class Solution {
    public int findMaximumK(int[] numbers) {
        int maxK = -1;
        BitSet observed = new BitSet(2048);
    
        for (int number : numbers) {
            int absoluteValue = Math.abs(number);
            if (absoluteValue > maxK && observed.get(-number + 1024))
                maxK = absoluteValue;
            observed.set(number + 1024);
        }
    
        return maxK;
    }
}

To solve the problem of finding the largest positive integer that exists with its negative in an array, you utilize the BitSet data structure in Java. This approach effectively tracks the existence of integers within the provided array, optimizing the search for the maximum integer k such that both k and -k are present in the array.

  • Initialize maxK to -1, which will store the maximum integer found that meets the condition.
  • Create BitSet observed with a size of 2048 to accommodate the range of possible values encountered in the array, considering positive and negative shifts.
  • Iterate through each integer in the given array:
    • Compute the absolute value of the current number.
    • Check if the set contains the index corresponding to the negative of the current number (considering an offset of 1024 for actual 0 index). If both the number and its negative are observed, update maxK if the absolute value is larger than the current maxK.
    • Add the current number to the BitSet with an appropriate offset to handle negative values.

This method efficiently finds the largest integer k such that both k and -k are present, and returns it. If no such integer exists, the result is -1.

python
class Solution:
    def maximumK(self, numbers: List[int]) -> int:
        maximum_value = -1
        tracked_numbers = set()
    
        for value in numbers:
            abs_value = abs(value)
    
            # Check if current absolute value is greater than the stored value
            # and its opposite has been encountered
            if abs_value > maximum_value and -value + 1024 in tracked_numbers:
                maximum_value = abs_value
            # Add the current value adjusted by 1024 to the set
            tracked_numbers.add(value + 1024)
    
        return maximum_value

The provided Python solution determines the largest positive integer from a list that also has its negative present in the same list.

  • The function maximumK expects an integer list numbers.
  • It initializes maximum_value to -1 and creates an empty set tracked_numbers to track the adjusted values.
  • The solution iterates through the list and for each value, calculates its absolute value abs_value.
  • It checks if abs_value is greater than maximum_value and if the negative of the current value adjusted by 1024 exists in tracked_numbers. If both conditions are met, it updates maximum_value to the abs_value.
  • After checking conditions, it adds the current value adjusted by 1024 into the tracked_numbers set.
  • Finally, the function returns maximum_value, which represents the largest integer that and its negative are present in the list, or -1 if no such number exists.

The offset value of 1024 is used to handle negative index issues by ensuring all values are adjusted to a positive index in the set. This method efficiently finds the desired integer with a time complexity of O(n), where n is the number of elements in the provided list.

Comments

No comments yet.