What is the complexity ? Do not worry about 0's or negative numbers in the array.
[Interviewer was more interested in how the multiplication/division gets effected as number of bits required to represent the intermediate products increases.]
---------------------------------------------------------------------------------------------
1. First pass calculate the product
P of all the numbers in array A
2. Second pass recreate the array A[i] = P / A[i]
As the interviewer has indicated the product can be very big, if the numbers in the array are big and/or the array length is big. Some languages support BigInteger operations,
like Python and I think Java also has BigInteger class. If using the programming language provided implementation is not an option, then you'll need to implement your own "BigInteger" class. You need to only implement the constructor, which will take the number
and convert it to string and then two methods for multiplication and division. C++ version will probably will overload the multiplication(*) and division(/) operators.

本文探讨了如何在不考虑数组中的零或负数的情况下,计算一个整数数组中每个元素位置上的乘积,该乘积等于数组中除当前位置元素外的所有其他元素的乘积。文章还讨论了在计算过程中可能出现的大数问题,并提出了解决方案。
1831

被折叠的 条评论
为什么被折叠?



