CareerCup Fill the array with product of all numbers except the number in that cell

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given N integer array, I want to fill the array with product of all numbers except the number in that cell. 
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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值