From 3a01c0e2a2af9a609b2975849a2c71192bdb8529 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Sat, 13 May 2023 13:43:40 +0800 Subject: [PATCH] add: 230513-CN --- cpp/2305/LC230513CN.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 cpp/2305/LC230513CN.cpp 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; +}