
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] <= 1000nums[i] != 0
Approach and Intuition
To solve the problem described, the following approach can be used:
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.
Initialize an integer
max_kwith a default value, for instance,-1. This will hold our result.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_kand updatemax_kto this integer if it's larger.
After completing the traversal of the array,
max_kwill hold the largest positive integerkfor which-kis also present in the array if such akexists. If no such pair is found,max_kwill remain as the initial value-1.
Example Analysis from Given Examples:
- In Example 1 (
nums = [-1,2,-3,3]), we find that3has its negative-3in 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]), both1and7have their corresponding negatives but7is 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
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, namedobserved, 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
valin the vectorvalues, calculatevalAbs, the absolute value ofval. - If
valAbsis greater than the currentmaxKand the bit corresponding to-val + 1024is set inobserved(indicating bothvaland-valhave been observed so far), then updatemaxKtovalAbs. - The bitset index for the current
valis then set to true (observed[val + 1024] = true). - After processing all elements in the vector,
maxKis returned. If no such integerkexists, 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.
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
maxKto -1, which will store the maximum integer found that meets the condition. - Create
BitSetobserved 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
maxKif the absolute value is larger than the currentmaxK. - Add the current number to the
BitSetwith 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.
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
maximumKexpects an integer listnumbers. - It initializes
maximum_valueto-1and creates an empty settracked_numbersto track the adjusted values. - The solution iterates through the list and for each
value, calculates its absolute valueabs_value. - It checks if
abs_valueis greater thanmaximum_valueand if the negative of the currentvalueadjusted by 1024 exists intracked_numbers. If both conditions are met, it updatesmaximum_valueto theabs_value. - After checking conditions, it adds the current
valueadjusted by 1024 into thetracked_numbersset. - Finally, the function returns
maximum_value, which represents the largest integer that and its negative are present in the list, or-1if 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.