Created
February 6, 2015 02:59
-
-
Save carlqt/1ed0a145eb11bc4fc3ba to your computer and use it in GitHub Desktop.
Golden Age problem
This file contains hidden or 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
def golden_age array | |
start = 0 | |
end_index = 0 | |
max_sum = 0 | |
largest = 0 | |
sub_arr = array.dup | |
return_array = [] | |
array.each_with_index do |num, ind| | |
start = ind | |
sub_arr.shift | |
sum = num | |
sub_arr.each_with_index do |num1, ind1| | |
sum = sum + num1 | |
if sum > max_sum | |
max_sum = sum | |
end_index = ind1 + ind + 1 | |
end | |
end | |
if max_sum > largest | |
largest = max_sum | |
return_array = [start, end_index] | |
end | |
end | |
return_array | |
end | |
arr = [5, 3, -5, 1, 19, 2, -4] | |
arr2 = [100, -101, 200, -3, 1000] | |
puts golden_age(arr) == [0,5] | |
puts golden_age(arr2) == [2, 4] | |
p golden_age arr | |
p golden_age arr2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment