diff --git a/cpp/2305/LC230513CN.cpp b/cpp/2305/LC230513CN.cpp new file mode 100644 index 0000000..83f71d9 --- /dev/null +++ b/cpp/2305/LC230513CN.cpp @@ -0,0 +1,24 @@ +#include +#include +#include + +/** + * 2441. Largest Positive Integer That Exists With Its Negative + * + * 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. + */ + +class LC230513 { +public: + static int findMaxK(const std::vector&) noexcept; +}; + +int LC230513::findMaxK(const std::vector& n) noexcept { + std::unordered_set s(n.begin(), n.end()); + int ret = -1; + for (int i: s) + if (s.count(-i)) + ret = std::max(ret, std::abs(i)); + return ret; +}