I'm trying stress testing over the maximum pair-wise product algorithm, where the constraints are 2 <= n <= 2.10^5; 0 <= a1,...,an <= 2.10^5
where n
is the length of the array and a1,a2,...,an
is the element of list. When I'm running the code which is shown below, it's giving me an error saying overflow encountered in long scalers
. This can be avoided in c
using long long
but how to avoid in python
. please help.
MY CODE
# python3
import numpy as np
def max_pairwise_product(numbers):
n = len(numbers)
max_product = 0
for first in range(n):
for second in range(first + 1, n):
max_product = max(max_product, (numbers[first] * numbers[second]))
return max_product
def fast_max_pairwise_product(numbers):
max_num = max(numbers)
numbers.remove(max_num)
sec_max_num = max(numbers)
max_product = max_num * sec_max_num
return max_product
while(True):
n = np.random.randint(0,1000) + 2
print(n)
vector = list(np.random.randint(0,1000000, n))
print(vector)
res1 = max_pairwise_product(vector)
res2 = fast_max_pairwise_product(vector)
if(res1 != res2):
print('Wrong Answer', res1, ' ' , res2)
break
else:
print()
ERROR
C:/Users/INTEL/Desktop/Algorithm Specialization/Algorithm toolbox/Week 1/week1_programming_challenges/2_maximum_pairwise_product/stress_test.py:11: RuntimeWarning: overflow encountered in long_scalars
max_product = max(max_product, (numbers[first] * numbers[second]))
C:/Users/INTEL/Desktop/Algorithm Specialization/Algorithm toolbox/Week 1/week1_programming_challenges/2_maximum_pairwise_product/stress_test.py:23: RuntimeWarning: overflow encountered in long_scalars
max_product = max_num * sec_max_num