Skip to content

Instantly share code, notes, and snippets.

@Teino1978-Corp
Forked from bridgpal/pivot.rb
Created October 31, 2015 07:46
Show Gist options
  • Save Teino1978-Corp/b6e1a67311f4b22b9238 to your computer and use it in GitHub Desktop.
Save Teino1978-Corp/b6e1a67311f4b22b9238 to your computer and use it in GitHub Desktop.
Pivot Algorithm
input = [1, 4, 6, 3, 2]
def get_pivot(input=[])
1.upto(input.length-2) do |pivot|
left = input[0...pivot].inject(:+)
right = input[pivot+1..-1].inject(:+)
return pivot if left == right
end
return -1
end
puts get_pivot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment