Skip to content

Commit 9320778

Browse files
committed
Add problem 3115: Maximum Prime Difference
1 parent 1ca342a commit 9320778

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,6 +2094,7 @@ pub mod problem_3107_minimum_operations_to_make_median_of_array_equal_to_k;
20942094
pub mod problem_3110_score_of_a_string;
20952095
pub mod problem_3111_minimum_rectangles_to_cover_points;
20962096
pub mod problem_3114_latest_time_you_can_obtain_after_replacing_characters;
2097+
pub mod problem_3115_maximum_prime_difference;
20972098
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
20982099

20992100
#[cfg(test)]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn maximum_prime_difference(nums: Vec<i32>) -> i32 {
7+
const PRIMES: [u64; 2] = [0x_2820_8A20_A08A_28AC, 0x_8002_28A2_0208_8288];
8+
9+
let mut iter = nums.iter().enumerate().filter_map(|(i, &num)| {
10+
let num = num as u8;
11+
12+
PRIMES
13+
.get(usize::from(num / 64))
14+
.is_some_and(|&bits| bits & (1 << (num % 64)) != 0)
15+
.then_some(i)
16+
});
17+
18+
let first = iter.next().unwrap();
19+
let last = iter.next_back().unwrap_or(first);
20+
21+
(last - first) as _
22+
}
23+
}
24+
25+
// ------------------------------------------------------ snip ------------------------------------------------------ //
26+
27+
impl super::Solution for Solution {
28+
fn maximum_prime_difference(nums: Vec<i32>) -> i32 {
29+
Self::maximum_prime_difference(nums)
30+
}
31+
}
32+
33+
#[cfg(test)]
34+
mod tests {
35+
#[test]
36+
fn test_solution() {
37+
super::super::tests::run::<super::Solution>();
38+
}
39+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod greedy;
2+
3+
pub trait Solution {
4+
fn maximum_prime_difference(nums: Vec<i32>) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [(&[4, 2, 9, 5, 3] as &[_], 3), (&[4, 8, 2, 8], 0)];
13+
14+
for (nums, expected) in test_cases {
15+
assert_eq!(S::maximum_prime_difference(nums.to_vec()), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)