Merge Two Sorted Arrays

Medium Arrays
Merge two already-sorted arrays into one sorted array using the two-pointer technique.

Sample input

[1, 3, 5] and [2, 4, 6]

Sample output

1 2 3 4 5 6

Solution

a = [1, 3, 5]
b = [2, 4, 6]
result = []
i = j = 0
while i < len(a) and j < len(b):
    if a[i] <= b[j]:
        result.append(a[i])
        i += 1
    else:
        result.append(b[j])
        j += 1
result.extend(a[i:])
result.extend(b[j:])
print(*result)
const a = [1, 3, 5];
const b = [2, 4, 6];
const result = [];
let i = 0, j = 0;
while (i < a.length && j < b.length) {
  if (a[i] <= b[j]) result.push(a[i++]);
  else result.push(b[j++]);
}
while (i < a.length) result.push(a[i++]);
while (j < b.length) result.push(b[j++]);
console.log(result.join(" "));
public class Main {
    public static void main(String[] args) {
        int[] a = {1, 3, 5};
        int[] b = {2, 4, 6};
        int[] result = new int[a.length + b.length];
        int i = 0, j = 0, k = 0;
        while (i < a.length && j < b.length) {
            if (a[i] <= b[j]) result[k++] = a[i++];
            else result[k++] = b[j++];
        }
        while (i < a.length) result[k++] = a[i++];
        while (j < b.length) result[k++] = b[j++];
        StringBuilder sb = new StringBuilder();
        for (int x = 0; x < result.length; x++) {
            sb.append(result[x]);
            if (x < result.length - 1) sb.append(" ");
        }
        System.out.println(sb.toString());
    }
}
fun main() {
    val a = intArrayOf(1, 3, 5)
    val b = intArrayOf(2, 4, 6)
    val result = IntArray(a.size + b.size)
    var i = 0
    var j = 0
    var k = 0
    while (i < a.size && j < b.size) {
        if (a[i] <= b[j]) result[k++] = a[i++]
        else result[k++] = b[j++]
    }
    while (i < a.size) result[k++] = a[i++]
    while (j < b.size) result[k++] = b[j++]
    println(result.joinToString(" "))
}
let a = [1, 3, 5]
let b = [2, 4, 6]
var result = [Int]()
var i = 0, j = 0
while i < a.count && j < b.count {
    if a[i] <= b[j] {
        result.append(a[i])
        i += 1
    } else {
        result.append(b[j])
        j += 1
    }
}
while i < a.count {
    result.append(a[i])
    i += 1
}
while j < b.count {
    result.append(b[j])
    j += 1
}
print(result.map { String($0) }.joined(separator: " "))
void main() {
  List<int> a = [1, 3, 5];
  List<int> b = [2, 4, 6];
  List<int> result = [];
  int i = 0, j = 0;
  while (i < a.length && j < b.length) {
    if (a[i] <= b[j]) {
      result.add(a[i++]);
    } else {
      result.add(b[j++]);
    }
  }
  while (i < a.length) result.add(a[i++]);
  while (j < b.length) result.add(b[j++]);
  print(result.join(' '));
}
#include <iostream>
using namespace std;

int main() {
    int a[] = {1, 3, 5};
    int b[] = {2, 4, 6};
    int na = 3, nb = 3;
    int result[6];
    int i = 0, j = 0, k = 0;
    while (i < na && j < nb) {
        if (a[i] <= b[j]) result[k++] = a[i++];
        else result[k++] = b[j++];
    }
    while (i < na) result[k++] = a[i++];
    while (j < nb) result[k++] = b[j++];
    for (int x = 0; x < na + nb; x++) {
        cout << result[x];
        if (x < na + nb - 1) cout << " ";
    }
    cout << endl;
    return 0;
}
#include <stdio.h>

int main() {
    int a[] = {1, 3, 5};
    int b[] = {2, 4, 6};
    int na = 3, nb = 3;
    int result[6];
    int i = 0, j = 0, k = 0;
    while (i < na && j < nb) {
        if (a[i] <= b[j]) result[k++] = a[i++];
        else result[k++] = b[j++];
    }
    while (i < na) result[k++] = a[i++];
    while (j < nb) result[k++] = b[j++];
    for (int x = 0; x < na + nb; x++) {
        printf("%d", result[x]);
        if (x < na + nb - 1) printf(" ");
    }
    printf("\n");
    return 0;
}