Skip to content

Commit b410cdf

Browse files
authored
Create tape_equilibrium.rb
1 parent 77640ac commit b410cdf

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

tape_equilibrium.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def solution(a)
2+
sum_left = 0
3+
sum_right = a.inject(:+)
4+
minimal_diff = nil
5+
(a.length-1).times do |index|
6+
sum_left += a[index]
7+
sum_right -= a[index]
8+
diff = (sum_left - sum_right).abs
9+
minimal_diff = diff if minimal_diff.nil? || diff < minimal_diff
10+
end
11+
minimal_diff
12+
end

0 commit comments

Comments
 (0)