From 2808f7d5d84a9f75425060e15f5365358401d418 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Thu, 2 May 2024 23:37:39 +0200 Subject: [PATCH] =?UTF-8?q?rs:=20add=20=C2=AB2441.=20Largest=20Positive=20?= =?UTF-8?q?Integer=20That=20Exists=20With=20Its=20Negative=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- ...e-integer-that-exists-with-its-negative.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 rs/largest-positive-integer-that-exists-with-its-negative.rs diff --git a/rs/largest-positive-integer-that-exists-with-its-negative.rs b/rs/largest-positive-integer-that-exists-with-its-negative.rs new file mode 100644 index 0000000..e8b8b89 --- /dev/null +++ b/rs/largest-positive-integer-that-exists-with-its-negative.rs @@ -0,0 +1,19 @@ +use std::cmp; +use std::collections::HashSet; + +impl Solution { + pub fn find_max_k(nums: Vec) -> i32 { + let mut seen = HashSet::::new(); + + let mut max_k = -1; + for x in &nums { + if seen.contains(&-x) { + max_k = cmp::max(max_k, x.abs()); + } + + seen.insert(*x); + } + + max_k + } +}