Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.