From d33d5a4fea680fb974472a27fa8117470cfaccfa Mon Sep 17 00:00:00 2001 From: EFanZh Date: Sun, 14 Jul 2024 15:24:56 +0800 Subject: [PATCH] Add a new soluton to problem 2183: Count Array Pairs Divisible by K --- .../gcd_and_hash_map_2.rs | 67 +++++++++++++++++++ .../mod.rs | 1 + 2 files changed, 68 insertions(+) create mode 100644 src/problem_2183_count_array_pairs_divisible_by_k/gcd_and_hash_map_2.rs diff --git a/src/problem_2183_count_array_pairs_divisible_by_k/gcd_and_hash_map_2.rs b/src/problem_2183_count_array_pairs_divisible_by_k/gcd_and_hash_map_2.rs new file mode 100644 index 00000000..9677b1f4 --- /dev/null +++ b/src/problem_2183_count_array_pairs_divisible_by_k/gcd_and_hash_map_2.rs @@ -0,0 +1,67 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +use std::cmp::Ordering; +use std::collections::hash_map::Entry; +use std::collections::HashMap; +use std::num::NonZeroU32; + +impl Solution { + fn gcd(mut x: u32, mut y: NonZeroU32) -> NonZeroU32 { + while let Some(z) = NonZeroU32::new(x % y) { + x = y.get(); + y = z; + } + + y + } + + pub fn count_pairs(nums: Vec, k: i32) -> i64 { + let k = NonZeroU32::new(k as _).unwrap(); + let mut counts = HashMap::<_, u64>::new(); + + for num in nums { + match counts.entry(Self::gcd(num as _, k)) { + Entry::Occupied(entry) => *entry.into_mut() += 1, + Entry::Vacant(entry) => { + entry.insert(1); + } + } + } + + let mut result = 0; + + for (&gcd_1, &count_1) in &counts { + let required = NonZeroU32::new(k.get() / gcd_1).unwrap(); + + for (&gcd_2, &count_2) in &counts { + if gcd_2.get() % required == 0 { + result += match gcd_1.cmp(&gcd_2) { + Ordering::Less => count_1 * count_2, + Ordering::Equal => count_1 * (count_1 - 1) / 2, + Ordering::Greater => continue, + }; + } + } + } + + result as _ + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn count_pairs(nums: Vec, k: i32) -> i64 { + Self::count_pairs(nums, k) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2183_count_array_pairs_divisible_by_k/mod.rs b/src/problem_2183_count_array_pairs_divisible_by_k/mod.rs index 7f621662..2cf0ff12 100644 --- a/src/problem_2183_count_array_pairs_divisible_by_k/mod.rs +++ b/src/problem_2183_count_array_pairs_divisible_by_k/mod.rs @@ -1,4 +1,5 @@ pub mod gcd_and_hash_map; +pub mod gcd_and_hash_map_2; pub trait Solution { fn count_pairs(nums: Vec, k: i32) -> i64;