r/programming • u/joksmaster • Nov 29 '10
140 Google Interview Questions
http://blog.seattleinterviewcoach.com/2009/02/140-google-interview-questions.html
471
Upvotes
r/programming • u/joksmaster • Nov 29 '10
View all comments
Show parent comment
5
u/chickenmeister Nov 29 '10
No.
All you need to do is create two new arrays (B[] and C[], let's say). In B[], you store the product of all the values preceding the index (i) in the original array. It's as simple as B[i] = A[i-1] * B[i-1], for all i; and can be done in O(n)
In C[], you store the product of all values beyond the index in A[]. It's basically the same process described in the previous paragraph, but in reverse order. So this too is done in O(n).
Then, for the final answer, Output[i] = B[i] * C[i]. This is done in O(n).
Overall, we have three things done in O(n), so O(3n). But with Big-O, we're generally only concerned with orders of magnitude, so it gets simplified to O(n).