double findMedianSortedArrays(vector<int>& a, vector<int>& b) {
vector<int> merged;
size_t i = 0, j = 0;
while (i < a.size() || j < b.size()) {
if (j == b.size() || (i < a.size() && a[i] <= b[j])) merged.push_back(a[i++]);
else merged.push_back(b[j++]);
}
int n = merged.size();
if (n % 2 == 1) return merged[n >> 1];
return ((double)merged[n / 2 - 1] + merged[n / 2]) / 2;
}
public double findMedianSortedArrays(int[] a, int[] b) {
int[] merged = new int[a.length + b.length]; int size = 0;
int i = 0, j = 0;
while (i < a.length || j < b.length) {
if (j == b.length || (i < a.length && a[i] <= b[j])) merged[size++] = a[i++];
else merged[size++] = b[j++];
}
int n = size;
if (n % 2 == 1) return merged[n >> 1];
return ((double) merged[n / 2 - 1] + merged[n / 2]) / 2;
}
def find_median_sorted_arrays(a, b):
merged = []
i = j = 0
while i < len(a) or j < len(b):
if j == len(b) or (i < len(a) and a[i] <= b[j]):
merged.append(a[i]); i += 1
else:
merged.append(b[j]); j += 1
n = len(merged)
if n % 2 == 1: return merged[n >> 1]
return (merged[n // 2 - 1] + merged[n // 2]) / 2
function findMedianSortedArrays(a: number[], b: number[]): number {
const merged: number[] = [];
let i = 0, j = 0;
while (i < a.length || j < b.length) {
if (j === b.length || (i < a.length && a[i] <= b[j])) merged.push(a[i++]);
else merged.push(b[j++]);
}
const n = merged.length;
if (n % 2 === 1) return merged[n >> 1];
return (merged[n / 2 - 1] + merged[n / 2]) / 2;
}
double findMedianSortedArrays(vector<int>& a, vector<int>& b) {
int total = a.size() + b.size();
size_t i = 0, j = 0; int prev = 0, curr = 0;
for (int step = 0; step <= total >> 1; step++) {
prev = curr;
if (i < a.size() && (j >= b.size() || a[i] <= b[j])) curr = a[i++];
else curr = b[j++];
}
return total % 2 == 1 ? curr : ((double)prev + curr) / 2;
}
public double findMedianSortedArrays(int[] a, int[] b) {
int total = a.length + b.length;
int i = 0, j = 0, prev = 0, curr = 0;
for (int step = 0; step <= total >> 1; step++) {
prev = curr;
if (i < a.length && (j >= b.length || a[i] <= b[j])) curr = a[i++];
else curr = b[j++];
}
return total % 2 == 1 ? curr : ((double) prev + curr) / 2;
}
def find_median_sorted_arrays(a, b):
total = len(a) + len(b)
i = j = prev = curr = 0
for step in range((total >> 1) + 1):
prev = curr
if i < len(a) and (j >= len(b) or a[i] <= b[j]):
curr = a[i]; i += 1
else:
curr = b[j]; j += 1
return curr if total % 2 == 1 else (prev + curr) / 2
function findMedianSortedArrays(a: number[], b: number[]): number {
const total = a.length + b.length;
let i = 0, j = 0, prev = 0, curr = 0;
for (let step = 0; step <= total >> 1; step++) {
prev = curr;
if (i < a.length && (j >= b.length || a[i] <= b[j])) curr = a[i++];
else curr = b[j++];
}
return total % 2 === 1 ? curr : (prev + curr) / 2;
}
double findMedianSortedArrays(vector<int>& a, vector<int>& b) {
int total = a.size() + b.size();
int na = a.size(), nb = b.size();
function<int(int)> kth = [&](int k) -> int {
int i = 0, j = 0;
while (true) {
if (i == na) return b[j + k - 1];
if (j == nb) return a[i + k - 1];
if (k == 1) return min(a[i], b[j]);
int half = k >> 1;
int ni = min(i + half, na) - 1;
int nj = min(j + half, nb) - 1;
if (a[ni] <= b[nj]) { k -= ni - i + 1; i = ni + 1; }
else { k -= nj - j + 1; j = nj + 1; }
}
};
if (total % 2 == 1) return kth((total + 1) >> 1);
return ((double)kth(total >> 1) + kth((total >> 1) + 1)) / 2;
}
public double findMedianSortedArrays(int[] a, int[] b) {
int total = a.length + b.length;
if (total % 2 == 1) return kth(a, b, (total + 1) >> 1);
return ((double) kth(a, b, total >> 1) + kth(a, b, (total >> 1) + 1)) / 2;
}
private int kth(int[] a, int[] b, int k) {
int i = 0, j = 0;
while (true) {
if (i == a.length) return b[j + k - 1];
if (j == b.length) return a[i + k - 1];
if (k == 1) return Math.min(a[i], b[j]);
int half = k >> 1;
int ni = Math.min(i + half, a.length) - 1;
int nj = Math.min(j + half, b.length) - 1;
if (a[ni] <= b[nj]) { k -= ni - i + 1; i = ni + 1; }
else { k -= nj - j + 1; j = nj + 1; }
}
}
def find_median_sorted_arrays(a, b):
total = len(a) + len(b)
def kth(k):
i = j = 0
while True:
if i == len(a): return b[j + k - 1]
if j == len(b): return a[i + k - 1]
if k == 1: return min(a[i], b[j])
half = k >> 1
ni = min(i + half, len(a)) - 1
nj = min(j + half, len(b)) - 1
if a[ni] <= b[nj]:
k -= ni - i + 1; i = ni + 1
else:
k -= nj - j + 1; j = nj + 1
if total % 2 == 1: return kth((total + 1) >> 1)
return (kth(total >> 1) + kth((total >> 1) + 1)) / 2
function findMedianSortedArrays(a: number[], b: number[]): number {
const total = a.length + b.length;
const kth = (k: number): number => {
let i = 0, j = 0;
while (true) {
if (i === a.length) return b[j + k - 1];
if (j === b.length) return a[i + k - 1];
if (k === 1) return Math.min(a[i], b[j]);
const half = k >> 1;
const ni = Math.min(i + half, a.length) - 1;
const nj = Math.min(j + half, b.length) - 1;
if (a[ni] <= b[nj]) { k -= ni - i + 1; i = ni + 1; }
else { k -= nj - j + 1; j = nj + 1; }
}
};
if (total % 2 === 1) return kth((total + 1) >> 1);
return (kth(total >> 1) + kth((total >> 1) + 1)) / 2;
}
double findMedianSortedArrays(vector<int>& a, vector<int>& b) {
if (a.size() > b.size()) return findMedianSortedArrays(b, a);
int m = a.size(), n = b.size();
int lo = 0, hi = m;
while (lo <= hi) {
int i = (lo + hi) >> 1;
int j = ((m + n + 1) >> 1) - i;
double aLeft = i == 0 ? -INFINITY : a[i - 1];
double aRight = i == m ? INFINITY : a[i];
double bLeft = j == 0 ? -INFINITY : b[j - 1];
double bRight = j == n ? INFINITY : b[j];
if (aLeft <= bRight && bLeft <= aRight) {
double leftMax = max(aLeft, bLeft);
if ((m + n) % 2 == 1) return leftMax;
return (leftMax + min(aRight, bRight)) / 2;
}
if (aLeft > bRight) hi = i - 1;
else lo = i + 1;
}
throw invalid_argument("inputs must be sorted");
}
public double findMedianSortedArrays(int[] a, int[] b) {
if (a.length > b.length) return findMedianSortedArrays(b, a);
int m = a.length, n = b.length;
int lo = 0, hi = m;
while (lo <= hi) {
int i = (lo + hi) >> 1;
int j = ((m + n + 1) >> 1) - i;
double aLeft = i == 0 ? Double.NEGATIVE_INFINITY : a[i - 1];
double aRight = i == m ? Double.POSITIVE_INFINITY : a[i];
double bLeft = j == 0 ? Double.NEGATIVE_INFINITY : b[j - 1];
double bRight = j == n ? Double.POSITIVE_INFINITY : b[j];
if (aLeft <= bRight && bLeft <= aRight) {
double leftMax = Math.max(aLeft, bLeft);
if ((m + n) % 2 == 1) return leftMax;
return (leftMax + Math.min(aRight, bRight)) / 2;
}
if (aLeft > bRight) hi = i - 1;
else lo = i + 1;
}
throw new IllegalArgumentException("inputs must be sorted");
}
def find_median_sorted_arrays(a, b):
if len(a) > len(b): return find_median_sorted_arrays(b, a)
m, n = len(a), len(b)
lo, hi = 0, m
while lo <= hi:
i = (lo + hi) >> 1
j = ((m + n + 1) >> 1) - i
a_left = float('-inf') if i == 0 else a[i - 1]
a_right = float('inf') if i == m else a[i]
b_left = float('-inf') if j == 0 else b[j - 1]
b_right = float('inf') if j == n else b[j]
if a_left <= b_right and b_left <= a_right:
left_max = max(a_left, b_left)
if (m + n) % 2 == 1: return left_max
return (left_max + min(a_right, b_right)) / 2
if a_left > b_right: hi = i - 1
else: lo = i + 1
raise ValueError('inputs must be sorted')
function findMedianSortedArrays(a: number[], b: number[]): number {
if (a.length > b.length) return findMedianSortedArrays(b, a);
const m = a.length, n = b.length;
let lo = 0, hi = m;
while (lo <= hi) {
const i = (lo + hi) >> 1;
const j = ((m + n + 1) >> 1) - i;
const aLeft = i === 0 ? -Infinity : a[i - 1];
const aRight = i === m ? Infinity : a[i];
const bLeft = j === 0 ? -Infinity : b[j - 1];
const bRight = j === n ? Infinity : b[j];
if (aLeft <= bRight && bLeft <= aRight) {
const leftMax = Math.max(aLeft, bLeft);
if ((m + n) % 2 === 1) return leftMax;
return (leftMax + Math.min(aRight, bRight)) / 2;
}
if (aLeft > bRight) hi = i - 1;
else lo = i + 1;
}
throw new Error('inputs must be sorted');
}