Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

304
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda