{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Homework 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Your Name: " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Big-$O$ Notation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Question 1 (3 points)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Is $3^{n+1}=O(3^n)$? Justify your answer using [Definition 3.1](https://6001-majhi.datasci.land/notes/big-o.html#def-o)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Question 2 (3 points)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Is $5^{2n}=O(5^n)$? Justify your answer using [Definition 3.1](https://6001-majhi.datasci.land/notes/big-o.html#def-o)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Question 3 (3 points)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using [Definition 3.1](https://6001-majhi.datasci.land/notes/big-o.html#def-o), show that the function $700n\\lg{n}$ is $O(n^2-1)$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Question 4 (1+1 points)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Describe the difference between an upper bound and a tight upper bound? Explain using an example.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Question 5 (1+1 points)\n", "Provide an example of an $O(1)$ and $O(n)$ algorithm.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Question 6 (🏅 Bonus)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If $f(n)=O(g(n))$, then $2^{f(n)} = O(2^{g(n)})$ True or false? Justify your answer.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Maximum difference" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Question 7 (5 points)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given an integer array nums, return the maximum difference between two successive elements in its sorted form. If the array contains less than two elements, return 0. \n", "Note : Do not use built-in sorting functions in Python.\n", "\n", "Eg: \n", "Input: x = [3,6,9,1] \n", "Output: 3 \n", "Explanation : The sorted form of the array is [1,3,6,9], either (3,6) or (6,9) has the maximum difference 3." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "b. Implement your algorithm in Python." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def max_diff(A):\n", " # implement me\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Question 8 (2 points)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If the input list has $n$ elements, describe the time complexity of your code. A thorough explanation is required.\n" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 2 }