{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Base case: Empty array then return -1\n",
    "\n",
    "Induction Hypothesis: If smaller list talks the correct 1st index of x\n",
    "\n",
    "Induction Step: Check if 1st place is x or not if found then return 0 else find x in rest of the list\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def firstIndex(arr, si, x):\n",
    "    l = len(arr)\n",
    "    if l == 0:\n",
    "        return -1 \n",
    "    if arr[si] == x:\n",
    "        return si\n",
    "    return firstIndex(arr, si+1, x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "arr = [1,0,1,0,1,0,6,4,9,4]\n",
    "x = 4\n",
    "print(firstIndex(arr, 0, x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "1 3 5 9 11\n",
      "7\n",
      "-1\n"
     ]
    }
   ],
   "source": [
    "# Make a copy of an array\n",
    "\n",
    "def firstIndex(arr, x):\n",
    "    l = len(arr)\n",
    "    if l == 0:\n",
    "        return -1\n",
    "    \n",
    "    if arr[0] == x:\n",
    "        return 0\n",
    "    \n",
    "    smallerList = arr[1:]\n",
    "    smallerListOutput = firstIndex(smallerList, x)\n",
    "    if smallerListOutput == -1:\n",
    "        return -1\n",
    "    else:\n",
    "        return smallerListOutput + 1\n",
    "\n",
    "    \n",
    "# Main\n",
    "from sys import setrecursionlimit\n",
    "setrecursionlimit(11000)\n",
    "n=int(input())\n",
    "arr=list(int(i) for i in input().strip().split(' '))\n",
    "\n",
    "x=int(input())\n",
    "print(firstIndex(arr, x))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "def firstIndexBetter(a, x, si):\n",
    "    l = len(a)\n",
    "    if si == l:\n",
    "        return -1\n",
    "    \n",
    "    if a[si] == x:\n",
    "        return si\n",
    "    \n",
    "    return firstIndexBetter(a, x, si+1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1,2,3,4,5,6,7]\n",
    "x = 5\n",
    "firstIndexBetter(a, x, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
