Skip to content

Commit

Permalink
Add problem 2217: Find Palindrome With Fixed Length
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Oct 22, 2024
1 parent ceb0421 commit 3f5b765
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,7 @@ pub mod problem_2211_count_collisions_on_a_road;
pub mod problem_2212_maximum_points_in_an_archery_competition;
pub mod problem_2215_find_the_difference_of_two_arrays;
pub mod problem_2216_minimum_deletions_to_make_array_beautiful;
pub mod problem_2217_find_palindrome_with_fixed_length;
pub mod problem_2218_maximum_value_of_k_coins_from_piles;
pub mod problem_2220_minimum_bit_flips_to_convert_number;
pub mod problem_2221_find_triangular_sum_of_an_array;
Expand Down
73 changes: 73 additions & 0 deletions src/problem_2217_find_palindrome_with_fixed_length/mathematical.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
fn reverse(mut x: u32) -> u32 {
let mut result = 0;

loop {
result = result * 10 + x % 10;
x /= 10;

if x == 0 {
return result;
}
}
}

pub fn kth_palindrome(queries: Vec<i32>, int_length: i32) -> Vec<i64> {
let int_length = int_length as u32;
let half_int_length = int_length / 2;
let other_half_int_length = int_length - half_int_length;
let first = u32::pow(10, other_half_int_length - 1);
let count = first * 9;
let offset = first - 1;
let shift = u64::from(u32::pow(10, half_int_length));
let iter = queries.into_iter();

if int_length & 1 == 0 {
iter.map(|query| {
let query_u32 = query as u32;

if query_u32 <= count {
let left = offset + query_u32;

(u64::from(left) * shift + u64::from(Self::reverse(left))) as _
} else {
-1
}
})
.collect()
} else {
iter.map(|query| {
let query_u32 = query as u32;

if query_u32 <= count {
let left = offset + query_u32;

(u64::from(left) * shift + u64::from(Self::reverse(left / 10))) as _
} else {
-1
}
})
.collect()
}
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn kth_palindrome(queries: Vec<i32>, int_length: i32) -> Vec<i64> {
Self::kth_palindrome(queries, int_length)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
63 changes: 63 additions & 0 deletions src/problem_2217_find_palindrome_with_fixed_length/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
pub mod mathematical;

pub trait Solution {
fn kth_palindrome(queries: Vec<i32>, int_length: i32) -> Vec<i64>;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [
(
(&[1, 2, 3, 4, 5, 90] as &[_], 3),
&[101_i64, 111, 121, 131, 141, 999] as &[_],
),
((&[2, 4, 6], 4), &[1111, 1331, 1551]),
(
(
&[
392_015_495,
5,
4,
1,
425_320_571,
565_971_690,
3,
7,
6,
3,
506_222_280,
468_075_092,
5,
],
2,
),
&[-1, 55, 44, 11, -1, -1, 33, 77, 66, 33, -1, -1, 55],
),
(
(
&[
2,
201_429_812,
8,
520_498_110,
492_711_727,
339_882_032,
462_074_369,
9,
7,
6,
],
1,
),
&[2, -1, 8, -1, -1, -1, -1, 9, 7, 6],
),
];

for ((queries, int_length), expected) in test_cases {
assert_eq!(S::kth_palindrome(queries.to_vec(), int_length), expected);
}
}
}

0 comments on commit 3f5b765

Please sign in to comment.