Created
July 20, 2017 09:28
-
-
Save aminbenarieb/b7d522fcd206e9980c59b87f21d7150b 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
import random | |
MIN_ARRAY_LENGTH = 1 | |
MAX_ARRAY_LENGTH = 50 | |
MIN_NUM = 0 | |
MAX_NUM = 100 | |
# Uses python3 | |
def max_mult(a,n): | |
if n < 1: | |
return 0 | |
if n < 2: | |
return a[0] | |
if n < 3: | |
return a[0] * a[1] | |
max1_idx = 0 | |
for i in range(0, n): | |
if a[max1_idx] < a[i]: | |
max1_idx = i | |
max2_idx = 1 if max1_idx == 0 else 0 | |
for i in range(0, n): | |
if a[max2_idx] < a[i] and i != max1_idx: | |
max2_idx = i | |
return a[max1_idx] * a[max2_idx] | |
def max_mult_test(a,n): | |
if n < 1: | |
return 0 | |
if n < 2: | |
return a[0] | |
if n < 3: | |
return a[0] * a[1] | |
result = 0 | |
max1_idx = 0 | |
max2_idx = 0 | |
for i in range (0, n): | |
for j in range (i+1, n): | |
_result = a[i] * a[j] | |
if _result > result: | |
result = _result | |
max1_idx = i | |
max2_idx = j | |
return a[max1_idx] * a[max2_idx] | |
def rand_array(n): | |
return [random.randint(MIN_NUM, MAX_NUM) for i in range(n)] | |
n = int(input()) | |
a = [int(x) for x in input().split()] | |
assert(len(a) == n) | |
# i = 0 | |
# while True: | |
# i += 1 | |
# print("\n-- #" + str(i) + " --\n") | |
# n = random.randint(MIN_ARRAY_LENGTH, MAX_ARRAY_LENGTH) | |
# a = rand_array(n) | |
# result1 = max_mult(a,n) | |
# result2 = max_mult_test(a,n) | |
# if result1 != result2: | |
# print("Array: " + ' '.join(str(e) for e in a) ) | |
# print("Got: " + str(result1) ) | |
# print("Stress test: " + str(result2) ) | |
# print("\n") | |
# break | |
result = max_mult(a,n) | |
print(result) | |
# result = max_mult_test(a,n) | |
# print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment