summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2026-03-29 08:56:40 +0200
committerIgor Pashev <pashev.igor@gmail.com>2026-03-30 10:20:50 +0200
commit9cb69e7fe09caa71cdea5b3f1911076c24ad6ba0 (patch)
treead6886920ff2071160f3c2634f1e022172f9c192 /src
downloadbalance-9cb69e7fe09caa71cdea5b3f1911076c24ad6ba0.tar.gz
Initial commit
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..eead58f
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,33 @@
+pub fn brutal(arr: &[usize]) -> Option<usize> {
+ let sum: usize = arr.iter().sum();
+
+ let x = arr
+ .iter()
+ .enumerate()
+ .try_fold((0, sum), |(left, right), (idx, x)| {
+ if left == right - x {
+ Err(idx)
+ } else {
+ Ok((left + x, right - x))
+ }
+ });
+
+ if let Err(idx) = x { Some(idx) } else { None }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use test_case::test_case;
+
+ #[test_case(&[1, 1, 1, 2], 2)]
+ #[test_case(&[1, 2, 3, 6, 3, 3], 3)]
+ #[test_case(&[0, 1, 0], 1)]
+ #[test_case(&[0, 0, 0], 0)]
+ #[test_case(&[4, 1000, 1, 1, 1, 1], 1)]
+ fn test(arr: &[usize], expected_idx: usize) {
+ let idx = brutal(arr);
+
+ assert_eq!(idx, Some(expected_idx));
+ }
+}