diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 7f3fd54..612e321 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -428,3 +428,5 @@ mod p3298_count_substrings_that_can_be_rearranged_to_contain_a_string_ii; mod p3270_find_the_key_of_the_numbers; mod p2275_largest_combination_with_bitwise_and_greater_than_zero; + +mod p2270_number_of_ways_to_split_array; diff --git a/src/problem/p2270_number_of_ways_to_split_array.rs b/src/problem/p2270_number_of_ways_to_split_array.rs new file mode 100644 index 0000000..3ab9b57 --- /dev/null +++ b/src/problem/p2270_number_of_ways_to_split_array.rs @@ -0,0 +1,39 @@ +/** + * [2270] Number of Ways to Split Array + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn ways_to_split_array(nums: Vec) -> i32 { + let nums: Vec = nums.into_iter().map(|x| x as i64).collect(); + let sum = nums.iter().sum::(); + + let mut current = 0; + let mut result = 0; + + for i in 0..nums.len() - 1 { + current += nums[i]; + + if current * 2 >= sum { + result += 1; + } + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2270() { + assert_eq!(2, Solution::ways_to_split_array(vec![10, 4, -8, 7])); + assert_eq!(2, Solution::ways_to_split_array(vec![2, 3, 1, 0])); + } +}