Skip to content

Instantly share code, notes, and snippets.

@jihchi
Last active November 11, 2025 08:31
Show Gist options
  • Select an option

  • Save jihchi/49f89035b39445e99e2a7473e754665a to your computer and use it in GitHub Desktop.

Select an option

Save jihchi/49f89035b39445e99e2a7473e754665a to your computer and use it in GitHub Desktop.

Prerequisite: Deno v2

deno run https://gist.github.com/jihchi/49f89035b39445e99e2a7473e754665a/raw/solve.ts
// three-pointer and merging from the end
function merge(a: number[], b: number[]): number[] {
let i = a.length - b.length - 1; // point to last "real" element of `a`
let j = b.length - 1; // point to last element of `b`
let k = a.length - 1; // point to last element of `a`
while (j >= 0) {
if (a[i] > b[j]) {
a[k] = a[i];
i--;
} else {
a[k] = b[j];
j--;
}
k--;
}
return a;
}
console.log(merge([1, 3, 5, 0, 0, 0], [2, 4, 6]));
// output: [1, 2, 3, 4, 5, 6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment