Leet Code Daily 2024-05-02 - Largest Positive Integer That Exists With Its Negative
Code
constexpr int kMaxNum = 1000;
class Solution {
public:
int findMaxK(vector<int>& nums) {
bool has_pos[kMaxNum + 1] = { false };
bool has_neg[kMaxNum + 1] = { false };
for (const auto& x : nums) {
if (x >= 0) {
has_pos[+x] = true;
}
if (x <= 0) {
has_neg[-x] = true;
}
}
for (int i = kMaxNum; i >= 0; i--) {
if (has_pos[i] && has_neg[i]) {
return i;
}
}
return -1;
}
};