Created
December 25, 2022 17:52
-
-
Save dharshan/7004f90783f1f2dd1562199e61dc559b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution | |
def solution(array) | |
left_part = array[0] | |
right_part = array.sum - left_part | |
diff = (left_part - right_part).abs | |
total = array.length | |
array.each_with_index do |element, index| | |
next if index == 0 | |
left_part += element | |
right_part -= element | |
new_diff = (left_part - right_part).abs | |
diff = [diff, new_diff].min | |
end | |
diff | |
end | |
end | |
puts Solution.new.solution([3,1,2,4,3]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment