{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Merge Sorted Arrays"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Into Third Array: O(n) - O(m + n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def mergeSortedArrays(arrA, arrB):\n",
    "    resArr = []\n",
    "    \n",
    "    l = len(arrA) if len(arrA) <= len(arrB) else len(arrB);\n",
    "    \n",
    "    for i in range(l):\n",
    "        if arrA[i] <= arrB[i]:\n",
    "            resArr.append(arrA[i])\n",
    "            resArr.append(arrB[i])\n",
    "        else:\n",
    "            resArr.append(arrB[i])\n",
    "            resArr.append(arrA[i])\n",
    "            \n",
    "    if len(arrA) < len(arrB):\n",
    "        resArr += arrB[len(arrA):]\n",
    "    elif len(arrB) < len(arrA):\n",
    "        resArr += arrA[len(arrB):]\n",
    "    \n",
    "    return resArr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([2, 7, 10], [5, 8, 12, 14, 22, 33])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arrA = [2, 7, 10]\n",
    "arrB = [5, 8, 12, 14, 22, 33]\n",
    "\n",
    "arrA, arrB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 5, 7, 8, 10, 12, 14, 22, 33]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mergeSortedArrays(arrB, arrA)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Into First Array: O(n) - O(m + n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(['NA', 'NA', 'NA', 'NA', 'NA', 'NA', 2, 7, 10], [5, 8, 12, 14, 22, 33])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arrA = [2, 7, 10]\n",
    "arrB = [5, 8, 12, 14, 22, 33]\n",
    "\n",
    "# Make space in first array\n",
    "arrA = ['NA'] * len(arrB) + arrA\n",
    "\n",
    "arrA, arrB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def mergeSortedArraysIntoFirst(arrA, arrB):\n",
    "    j = len(arrB)\n",
    "    k = 0\n",
    "    \n",
    "    for i in range(len(arrA)):\n",
    "        if j < len(arrA) and arrA[j] <= arrB[k]:\n",
    "            arrA[i] = arrA[j]\n",
    "            j += 1\n",
    "        else:\n",
    "            arrA[i] = arrB[k]\n",
    "            k += 1\n",
    "    \n",
    "    return arrA, arrB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([2, 5, 7, 8, 10, 12, 14, 22, 33], [5, 8, 12, 14, 22, 33])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mergeSortedArraysIntoFirst(arrA, arrB)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}