Pergunta de entrevista da empresa AKUNA CAPITAL

Find largest products of two numbers from an array

Respostas da entrevista

Sigiloso

4 de set. de 2016

I think you need some dynamic programming especially that there might be large negative values that give a large product. @previous post

Sigiloso

4 de nov. de 2016

This accounts for negative numbers in the array. The sort will run in (n log n) time, everything else is constant time. a2 = [1,2,3,4,5,6,7,8,9,-11,-32,32] def max_product(a): # returns the largest product of 2 numbers from the given array # sort array - return max(a[0] * a[1], a[-1], a[-2]) if len(a) < 2: return None a_sorted = sorted(a) print a_sorted, a_sorted[0] * a_sorted[1], a_sorted[-2] * a_sorted[-1] return max(a_sorted[0] * a_sorted[1], a_sorted[-2] * a_sorted[-1]) if __name__ == '__main__': print max_product(a2)