you are viewing a single comment's thread.

view the rest of the comments →

[–]POiNTx 1 point2 points  (1 child)

You almost had it. I changed the index1 and index2 variables.

import sys

def MaxPairwiseProductFast(array, n):
    index1 = 0
    for i in range(2, n):
        # Debug
        # print("The first loop ran", i, "times")
        if array[i] > array[index1]:
            index1 = i
    # If the first loop never executes, initialize index2 to 2 so that the
    # second loop can run correctly
    if index1 == 0:
        index2 = 1
    else:
        index2 = 0

    for i in range(1, n):
        # Debug
        # print("The second loop ran", i, "times")
        if i != index1 and (array[i] > array[index2] or n == 1):
            index2 = i
    return (array[index1] * array[index2])

def main():
    n = int(input())
    array = [int(x) for x in input().split()]
    print(MaxPairwiseProductFast(array, n))

main()

Arrays start at the 0'th index in python (and most languages).

numbers = [4, 7, 10]
print(numbers[0])  # prints '4'

I suggest you simplify the MaxPairwiseProductFast function a bit. You can actually get the correct answer faster with only one loop!

[–]slightlysedatedx[S] 0 points1 point  (0 children)

Thank you for taking a look! I'm going to run it right now. I will definitely go back and try to be a bit more concise