Generation

generate functionSat, 12 Nov 2022

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

function findMedianSortedArrays(nums1: number[], nums2: number[]): number { const merged = [...nums1, ...nums2].sort((a, b) => a - b); const mid = Math.floor(merged.length / 2); if (merged.length % 2 === 0) { return (merged[mid] + merged[mid - 1]) / 2; } return merged[mid]; };

TypeScript
Generate More

Questions about programming?Chat with your personal AI assistant