CareerCup Fill the array with product of all numbers except the number in that cell
2014-03-02 19:44
453 查看
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.
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.
相关文章推荐
- 黑马程序员---交通灯调度系统-项目分析-学习笔记
- JSP9个内置对象
- poj 1191 DP
- Spring Security3配置使用
- Collapsible Tree Layout
- 二级缓存在项目中的集成和使用
- Mac pro 休眠后没声音
- Android 4.0 Launcher2源码分析——Workspace切换AllApps流程
- 在iOS上加载显示pdf文件
- 以用户为中心的Web网站设计流程
- 面试中经常问到的七种排序方法
- 华为OJ 对象管理器
- 抽象类是否有构造方法
- spring-security3.1 + spring mvc + Hibernate 控制系统权限
- 封装代码和UI利器—Fragment(二)
- grep 或egrep的用法
- Tomcat源码分析之Server与Lifecycle的设计与实现
- hdu 1542 Atlantis (线段树+离散化+扫描线)
- Android 4.0 Launcher2源码分析——Launcher内容加载详细过程
- Binary Tree Zigzag Level Order Traversal 疑惑ing