Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

309
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda