Skip to content

Instantly share code, notes, and snippets.

@dharshan
Created December 25, 2022 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dharshan/7004f90783f1f2dd1562199e61dc559b to your computer and use it in GitHub Desktop.
Save dharshan/7004f90783f1f2dd1562199e61dc559b to your computer and use it in GitHub Desktop.
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