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

253
Visualizações
How to check if the input matrix is symmetric?

The code below takes in a matrix (MAT) and transposes the matrix, calls it array. The definition of the symmetrical matrix is that it should be a square matrix and the elements in the given matrix compared to the transposed one should be the same.

The given matrix below and transposed matrix should output false if checked for symmetry.

I did create an if statement at first to check whether MAT[j][i] and array[j][i] are the same but keep getting the wrong answer. It's not properly checking all the elements together. Could someone help with that?

Thanks!

const symmetricMatrix = function (MAT) {
  let array = [];
  for (let i = 0; i < MAT.length; i++) {
    array.push([]);
    for (let j = 0; j < MAT.length; j++) {
      array[i].push(MAT[j][i]);

    }
  }

  
  return array;
};

console.log(
  symmetricMatrix(
    (MAT = [
      [1, 3, 1],
      [-1, 1, 4],
      [2, 1, 0],
    ])
  )
);

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First you can create a copy of matrix and then transpose it and then check if it has same element at that index.

const symmetricMatrix = function (mat) {
  const copy = Array.from(mat, (_) => []);

  for (let i = 0; i < mat.length; i++)
    for (let j = 0; j < mat.length; j++) 
      copy[i][j] = mat[j][i];

  for (let i = 0; i < mat.length; i++)
    for (let j = 0; j < mat.length; j++)
      if (copy[i][j] != mat[i][j]) return false;

  return true;
};

const matrix = [
  [1, 3, 1],
  [-1, 1, 4],
  [2, 1, 0],
];
const matrix2 = [
  [1, -1, 2],
  [-1, 1, 1],
  [2, 1, 0],
];
console.log(symmetricMatrix(matrix));
console.log(symmetricMatrix(matrix2));

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