Skip to content

Commit

Permalink
Add a new soluton to problem 2183: Count Array Pairs Divisible by K
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Jul 14, 2024
1 parent d9b640a commit d33d5a4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<i32>, 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<i32>, k: i32) -> i64 {
Self::count_pairs(nums, k)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
1 change: 1 addition & 0 deletions src/problem_2183_count_array_pairs_divisible_by_k/mod.rs
Original file line number Diff line number Diff line change
@@ -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<i32>, k: i32) -> i64;
Expand Down

0 comments on commit d33d5a4

Please sign in to comment.