Skip to content

Commit

Permalink
Add problem 2177: Find Three Consecutive Integers That Sum to a Given…
Browse files Browse the repository at this point in the history
… Number
  • Loading branch information
EFanZh committed Jun 6, 2024
1 parent 8efe99f commit b2a432b
Show file tree
Hide file tree
Showing 3 changed files with 52 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 @@ -1532,6 +1532,7 @@ pub mod problem_2165_smallest_value_of_the_rearranged_number;
pub mod problem_2166_design_bitset;
pub mod problem_2169_count_operations_to_obtain_zero;
pub mod problem_2170_minimum_operations_to_make_the_array_alternating;
pub mod problem_2177_find_three_consecutive_integers_that_sum_to_a_given_number;
pub mod problem_2180_count_integers_with_even_digit_sum;
pub mod problem_2185_counting_words_with_a_given_prefix;
pub mod problem_2186_minimum_number_of_steps_to_make_two_strings_anagram_ii;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
pub struct Solution;

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

impl Solution {
pub fn sum_of_three(num: i64) -> Vec<i64> {
let num = num as u64;

if num % 3 == 0 {
let middle = (num / 3) as i64;

vec![middle - 1, middle, middle + 1]
} else {
Vec::new()
}
}
}

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

impl super::Solution for Solution {
fn sum_of_three(num: i64) -> Vec<i64> {
Self::sum_of_three(num)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub mod divide_by_3;

pub trait Solution {
fn sum_of_three(num: i64) -> Vec<i64>;
}

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

pub fn run<S: Solution>() {
let test_cases = [(33, &[10_i64, 11, 12] as &[_]), (4, &[])];

for (num, expected) in test_cases {
assert_eq!(S::sum_of_three(num), expected);
}
}
}

0 comments on commit b2a432b

Please sign in to comment.