Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

305
Views
Is there a way to multiply two nested arrays by each other while maintaining O(n)?

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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).

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!