1
n = int(input("Enter the number of elements in the array (2-200,000):"))
a = [int(x) for x in input("Enter all numbers of the sequence with only non-negative intergers not exceeding 100,000:").split()]
c = list()3

for i in range(0,n):
    for j in range (1,n):
        if a[i] != a[j]:
            m = a[i]*a[j]
            c.append(m)
        else:
            continue
print(max(c))

This code works. However, I want to define a function to automatically calculating the max product from the 5th line in the code shown below.

def MaxPairwiseProduct(n,a,c):
for i in range(0,n):
    for j in range (1,n):
        if a[i] != a[j]:
            m = a[i]*a[j]
            c.append(m)
        else:
            continue

        Product = max(c)

        return Product

n = int(input("Enter the number of elements in the array (2-200,000):"))
a = [int(x) for x in input("Enter all numbers of the sequence with only non-negative intergers not exceeding 100,000:").split()]
c = list()
MaxPairwiseProduct(n,a,c)

I re-write the function but it does not work. It reveals "IndentationError: expected an indented block"

Julien
  • 13,986
  • 5
  • 29
  • 53

5 Answers5

2
# python3
result = 0
def max_pairwise_product_fast(n, numbers):
    max_index1 = -1
    for i in range(n):
        if max_index1 == -1 or numbers[i] > numbers[max_index1]:
            max_index1 = i
    max_index2 = -1

   for i in range(n):
        if i != max_index1 and (max_index2 == -1 or numbers[i] > numbers[max_index2]):
            max_index2 = i

    return numbers[max_index1] * numbers[max_index2]
if __name__ == '__main__':
    n = int(input())
    a = [int(x) for x in input().split()]
    assert (len(a) == n)

    print(max_pairwise_product_fast(n, a))
Anil Sah
  • 1,502
  • 1
  • 9
  • 11
0
def MaxPairwiseProduct(n,a,c):
    for i in range(0,n):
        for j in range (1,n):
            if a[i] != a[j]:
                m = a[i]*a[j]
                c.append(m)

            else:
                continue

    Product = max(c)
    return Product

n = int(input("Enter the number of elements in the array (2-200,000):"))
a = [int(x) for x in input("Enter all numbers of the sequence with only non-negative intergers not exceeding 100,000:").split()]
c = list()
print(MaxPairwiseProduct(n,a,c))
Jeremy_Tamu
  • 725
  • 1
  • 8
  • 21
0
# Uses python3
def MaxPairwiseProduct(n,a,c):
    for i in range(0,n):
        for j in range (1,n):
            if a[i] != a[j]:
                m = a[i]*a[j]
                c.append(m)

            else:
                continue

    Product1 = max(c)
    return Product1

def MaxPairwiseProductFast(n,a):
    max_index1 = -1
    for i in range(0,n):
        if a[i] > a[max_index1]:
            max_index1 = i
        else:
            continue
    #the value of the other index should be different compared to the        #first, else it will assume the same indices for both the max         
    max_index2 = -2
    for j in range(0,n):
        if a[j] > a[max_index2] and a[j] != a[max_index1]:
            max_index2 = j   
        else:
            continue

    Product2 = a[max_index1]*a[max_index2]
    return Product2

n = int(input("Enter the number of elements in the array (2-200,000):"))
a = [int(x) for x in input("Enter all numbers of the sequence with only non-negative intergers not exceeding 100,000:").split()]
c = list()

print('The max value by regular algorithm:', MaxPairwiseProduct(n,a,c))
print('The max value by faster algorithm:', MaxPairwiseProductFast(n,a))  

This code contains the second algorithm to calculate the max value.

Jeremy_Tamu
  • 725
  • 1
  • 8
  • 21
0

My stupid solution to the maximum pairwise product.

   n = int(input()) #
   nums = input().strip().split()
   nums = [ int(i) for i in nums ]
   firstmax, secondmax = sorted(set(nums))[-2:]
   print(firstmax*secondmax)
shining
  • 1,049
  • 16
  • 31
0

To do the stress testing: you can use this code here. It works when I did an assignment.

#Stress test

from random import randint

def max_prod_stress(N,M):

while True:
    
    n = randint(2,N)
    
    A = [None]*n
    
    for i in range(n):
        
        A[i] = randint(0,M)
    
     print(A)
    
     result1 = max_prod_naive(A)
    
     result2 = max_prod_fast(A)
    
      if result1==result2:
        
      print('OK')
    
      else:
        
          print('Wrong answer:',result1,result2)
        
      return

           max_prod_stress(5,100)
Ata Tekeli
  • 11
  • 1
  • 6