I want to multiply these two arrays. After I reduce any nested array with the product of its values.
[ [ 1 ], [ 1 ], [ 1, 2 ], [ 1, 2, 3 ] ]
[ [ 2, 3, 4 ], [ 3, 4 ], [ 4 ], [ 1 ] ]
The answer should be:
[24, 12, 8, 6]
Clarification:
24 = 1 * 2 * 3 * 4
If there is any other approach please let me know. The code can't be of anything higher than O(n) and no usage of division operator.
When talking about big-o complexity like O(n), it's important to know what the n refers to. In case of a simple array it's typically the array size. In your case however, you have arrays of arrays so n can refer to the size of the inner or the outer array.
To simplify it, consider the outer array as containing x arrays and the inner arrays as having y elements. Then it require 2y-1 multiplications for each inner array. Since there are x inner arrays, it will in total require x(2y-1) multiplications. In big-o that would be O(xy).
So to answer your question.
If your n refers to either the number of inner arrays or the number of elements in the inner arrays then yes, it will be O(n).
However, if your n refers to both the inner and the outer dimension (i.e. both are growing at the same time) then no, it will be O(n^2).
In table form:
--------------------------------------------------
| Outer dimension | Inner dimension | Complexity |
--------------------------------------------------
| growing | constant | O(n) |
--------------------------------------------------
| constant | growing | O(n) |
--------------------------------------------------
| growing | growing | O(n^2) |
--------------------------------------------------
And just to make clear: There is no magic that can turn the last case into O(n).