require 'bigdecimal' | |
# Input the number of numbers | |
n = readline | |
# Input the list of numbers | |
array_list = readline.split(' ') | |
# Initializing the variables | |
max1 = BigDecimal("0") | |
max2 = BigDecimal("0") | |
min1 = BigDecimal("0") | |
min2 = BigDecimal("0") | |
min1_index = 0 | |
max1_index = 0 | |
# Finding the Biggest and Smallest Number and storing their index | |
array_list.each_with_index do |value, index| | |
if BigDecimal(value) > 0 | |
if max1 < BigDecimal(value) | |
max1 = BigDecimal(value) | |
max1_index = index | |
end | |
end | |
if BigDecimal(value) < 0 | |
if min1 < BigDecimal(value) * -1 | |
min1 = BigDecimal(value) * -1 | |
min1_index = index | |
end | |
end | |
end | |
#Finding the second biggest and smallest numbers. | |
array_list.each_with_index do |value, index| | |
if BigDecimal(value) > 0 | |
max2 = BigDecimal(value) if BigDecimal(value) <= max1 and BigDecimal(value) > max2 and max1_index != index | |
end | |
if BigDecimal(value) < 0 | |
min2 = BigDecimal(value) * -1 if BigDecimal(value) >= min1 * -1 and BigDecimal(value) < min2 * -1 and min1_index != index | |
end | |
end | |
#Checking if any of them is 0, else multiplying them accordingly | |
if( min1 != 0 or min2!= 0 or max1 != 0 or max2 !=0) | |
if(max1 * max2 < min1 * min2) | |
result1 = (min1 * min2) | |
else | |
result1 = (max1 * max2) | |
end | |
else | |
result1 = min1 * max1 | |
end | |
#outputting to a format to pass the tests in Coursera | |
puts result1.to_s("F").split(".")[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment