Created
October 4, 2020 16:41
-
-
Save sudiptamondal/e0771d4a3b5f86b497fcf1da4b4d031b to your computer and use it in GitHub Desktop.
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
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