diff --git a/rs/divide-array-into-arrays-with-max-difference.rs b/rs/divide-array-into-arrays-with-max-difference.rs new file mode 100644 index 0000000..607ff75 --- /dev/null +++ b/rs/divide-array-into-arrays-with-max-difference.rs @@ -0,0 +1,18 @@ +impl Solution { + pub fn divide_array(mut nums: Vec, k: i32) -> Vec> { + nums.sort(); + + let divisions: Vec> = nums.as_slice().chunks_exact(3).map(Vec::from).collect(); + + for d in &divisions { + let first = d.first().expect("must have first element"); + let last = d.last().expect("we are guaranteed triples"); + + if last - first > k { + return vec![]; + } + } + + divisions + } +}