|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "attachments": {}, |
| 5 | + "cell_type": "markdown", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "### Find Second largest element in an array" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "attachments": {}, |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "- array = [3, 9, 1, 6, 2, 7, 8, 5]" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "attachments": {}, |
| 21 | + "cell_type": "markdown", |
| 22 | + "metadata": {}, |
| 23 | + "source": [ |
| 24 | + "### Algorithm for the problem\n", |
| 25 | + "\n", |
| 26 | + "1. Initialize largest and second_largest to negative infinity (float('-inf')).\n", |
| 27 | + "2. Iterate through each element of the array.\n", |
| 28 | + "3. For each element, compare it with the value of largest.\n", |
| 29 | + " * If the current element is greater than largest, update second_largest with the value of largest, and update largest with the current element.\n", |
| 30 | + " * If the current element is not greater than largest but is greater than second_largest, update second_largest with the current element.\n", |
| 31 | + "4. Repeat this process for each element in the array.\n", |
| 32 | + "5. After iterating through the entire array, the variable second_largest will hold the second largest element.\n", |
| 33 | + "6. Return the value of second_largest as the result." |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "code", |
| 38 | + "execution_count": 1, |
| 39 | + "metadata": {}, |
| 40 | + "outputs": [ |
| 41 | + { |
| 42 | + "name": "stdout", |
| 43 | + "output_type": "stream", |
| 44 | + "text": [ |
| 45 | + "Second largest element: 8\n" |
| 46 | + ] |
| 47 | + } |
| 48 | + ], |
| 49 | + "source": [ |
| 50 | + "# Find the second largest element in an array\n", |
| 51 | + "\n", |
| 52 | + "def find_second_largest(arr):\n", |
| 53 | + " largest = float('-inf')\n", |
| 54 | + " second_largest = float('-inf')\n", |
| 55 | + "\n", |
| 56 | + " for num in arr:\n", |
| 57 | + " if num > largest:\n", |
| 58 | + " second_largest = largest\n", |
| 59 | + " largest = num\n", |
| 60 | + " elif num > second_largest:\n", |
| 61 | + " second_largest = num\n", |
| 62 | + "\n", |
| 63 | + " return second_largest\n", |
| 64 | + "\n", |
| 65 | + "array = [3, 9, 1, 6, 2, 7, 8, 5]\n", |
| 66 | + "second_largest = find_second_largest(array)\n", |
| 67 | + "print(\"Second largest element:\", second_largest)\n" |
| 68 | + ] |
| 69 | + } |
| 70 | + ], |
| 71 | + "metadata": { |
| 72 | + "kernelspec": { |
| 73 | + "display_name": "Python 3", |
| 74 | + "language": "python", |
| 75 | + "name": "python3" |
| 76 | + }, |
| 77 | + "language_info": { |
| 78 | + "codemirror_mode": { |
| 79 | + "name": "ipython", |
| 80 | + "version": 3 |
| 81 | + }, |
| 82 | + "file_extension": ".py", |
| 83 | + "mimetype": "text/x-python", |
| 84 | + "name": "python", |
| 85 | + "nbconvert_exporter": "python", |
| 86 | + "pygments_lexer": "ipython3", |
| 87 | + "version": "3.11.2" |
| 88 | + }, |
| 89 | + "orig_nbformat": 4 |
| 90 | + }, |
| 91 | + "nbformat": 4, |
| 92 | + "nbformat_minor": 2 |
| 93 | +} |
0 commit comments