Solución

@isakidev·11/5/2026TypeScript
solution.tsTypeScript
function findMedian(numbers: number[]): number {

  if (numbers.length <= 1) return numbers[0]

  const isEven = numbers.length % 2 === 0
  const sortedNumbers = numbers.toSorted((a, b) => a - b)

  if (!isEven) {
    const mid = sortedNumbers.length / 2
    return sortedNumbers.at(mid)
  }

  const firstIndex = Math.floor(sortedNumbers.length / 3)
  const secondIndex = firstIndex + 1
  
  return (sortedNumbers.at(firstIndex) + sortedNumbers.at(secondIndex)) / 2
}

// No modificar: necesario para evaluar el resultado.
export { findMedian };
0respuestas
Respuestas

Aún no hay respuestas

¡Sé el primero en responder!

Escribir un comentario

Recuerda ser amable. Estás comentando la solución de otra persona. Comparte tu perspectiva de forma constructiva y respetuosa.

Debes iniciar sesión para publicar un comentario.