fork download
  1. def balance(transactions)
  2. array = transactions.split(/\s+/).map(&:to_i)
  3. sum_left, sum_right, ans = 0, array.reduce(:+), []
  4. array.each_index do |i|
  5. sum_left += array[i - 1] if i > 0
  6. sum_right -= array[i]
  7. ans.push(i) if sum_left == sum_right
  8. end
  9. ans
  10. end
  11.  
  12. if __FILE__ == $0
  13. DATA.each_line { |transactions| puts balance(transactions).join(' ') }
  14. end
  15.  
  16. __END__
  17. 0 -3 5 -4 -2 3 1 0
  18. 3 -2 2 0 3 4 -6 3 5 -4 8
  19. 9 0 -5 -4 1 4 -4 -9 0 -7 -1
  20. 9 -7 6 -8 3 -9 -5 3 -6 -8 5
Success #stdin #stdout 0s 28688KB
stdin
Standard input is empty
stdout
0 3 7
5
8
6