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

100
Visualizações
How to get 2 numbers that swapping them make them correctly positioned

I have a problem with sorting. How can I get 2 numbers that when they swap they are both in the correct position? So I think for example about 3 and 4 because swapping them makes them at the correct position. I want to have a boolean variable that says if 2 swapped integers go to the right places it is true, else it is false. Basically for example if in loop we are at integer 4, we are checking his proposed index so it is (3). Then we use this index to see what is currently at this index in elephantsOrderGiven so it is 3. Then I want to check if 4 goes to the place of 3 will be correctly placed (so at index 3 in proposedOrder) and if 3 goes to the place of 4 will be correctly placed index(1 in proposedOrder). So it means 4 go to index (3), 3 goes to the index(1) and after swap they are both correct, so I want to have a boolean variable if swapping two integers will position them both correctly, because for example 5 and 2 will not be corrected positoned, only 2 will be.

let elephantsOrderGiven = [1, 4, 5, 3, 6, 2];
let proposedOrder = [5, 3, 2, 4, 6, 1]; 

for (let i = 0; i < elephantsOrderGiven.length; i++) {

}

Swapping I mean this operation:

temp = elephantsOrder[currentIndex];
      elephantsOrder[currentIndex] = elephantsOrder[correctIndex];
      elephantsOrder[correctIndex] = temp;

After this operation for example 4 and 3 should be corrected positoned like in proposed order. If not boolean variable should give False and the swapping should not be executed, I mean 4 and 3 should stay in their current positions.

about 4 years ago · Juan Pablo Isaza
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