Created
August 5, 2014 19:24
-
-
Save bridgpal/f5403639f8c46a2320f1 to your computer and use it in GitHub Desktop.
Pivot Algorithm
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
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