From 6905981e11d707b3e1a0bcc6e8211b6a8ec16302 Mon Sep 17 00:00:00 2001 From: anoushka <153769363+iamanolive@users.noreply.github.com> Date: Sun, 2 Jun 2024 14:27:47 +0300 Subject: [PATCH 1/3] notebook 4 --- .../python-programming/notebook4.ipynb | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 kaggle-courses/python-programming/notebook4.ipynb diff --git a/kaggle-courses/python-programming/notebook4.ipynb b/kaggle-courses/python-programming/notebook4.ipynb new file mode 100644 index 000000000..0b072c557 --- /dev/null +++ b/kaggle-courses/python-programming/notebook4.ipynb @@ -0,0 +1,79 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [], + "authorship_tag": "ABX9TyMKGW0L6C22sBHSSX7lR2a9", + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "id": "fRij3iqHqGHv" + }, + "outputs": [], + "source": [ + "def select_second(L):\n", + " if len(L) >= 2: return L[1]\n", + " else: return None" + ] + }, + { + "cell_type": "code", + "source": [ + "def losing_team_captain(teams):\n", + " return teams[-1][1]" + ], + "metadata": { + "id": "mUigMl6Fqd_X" + }, + "execution_count": 2, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "def purple_shell(racers):\n", + " racers[0], racers[-1] = racers[-1], racers[0]" + ], + "metadata": { + "id": "sRnDxgLqqvxy" + }, + "execution_count": 3, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "def fashionably_late(arrivals, name):\n", + " return (name != arrivals[-1]) and (arrivals.index(name) >= len(arrivals) / 2)" + ], + "metadata": { + "id": "QPwAmuirrY9I" + }, + "execution_count": 4, + "outputs": [] + } + ] +} \ No newline at end of file From 183a8fe009def60e0c222c60331c9e493c1923e1 Mon Sep 17 00:00:00 2001 From: anoushka <153769363+iamanolive@users.noreply.github.com> Date: Sun, 2 Jun 2024 14:28:08 +0300 Subject: [PATCH 2/3] delete files --- .../python-programming/notebook4.ipynb | 79 ------------------- 1 file changed, 79 deletions(-) delete mode 100644 kaggle-courses/python-programming/notebook4.ipynb diff --git a/kaggle-courses/python-programming/notebook4.ipynb b/kaggle-courses/python-programming/notebook4.ipynb deleted file mode 100644 index 0b072c557..000000000 --- a/kaggle-courses/python-programming/notebook4.ipynb +++ /dev/null @@ -1,79 +0,0 @@ -{ - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "provenance": [], - "authorship_tag": "ABX9TyMKGW0L6C22sBHSSX7lR2a9", - "include_colab_link": true - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "language_info": { - "name": "python" - } - }, - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "view-in-github", - "colab_type": "text" - }, - "source": [ - "\"Open" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "id": "fRij3iqHqGHv" - }, - "outputs": [], - "source": [ - "def select_second(L):\n", - " if len(L) >= 2: return L[1]\n", - " else: return None" - ] - }, - { - "cell_type": "code", - "source": [ - "def losing_team_captain(teams):\n", - " return teams[-1][1]" - ], - "metadata": { - "id": "mUigMl6Fqd_X" - }, - "execution_count": 2, - "outputs": [] - }, - { - "cell_type": "code", - "source": [ - "def purple_shell(racers):\n", - " racers[0], racers[-1] = racers[-1], racers[0]" - ], - "metadata": { - "id": "sRnDxgLqqvxy" - }, - "execution_count": 3, - "outputs": [] - }, - { - "cell_type": "code", - "source": [ - "def fashionably_late(arrivals, name):\n", - " return (name != arrivals[-1]) and (arrivals.index(name) >= len(arrivals) / 2)" - ], - "metadata": { - "id": "QPwAmuirrY9I" - }, - "execution_count": 4, - "outputs": [] - } - ] -} \ No newline at end of file From 9ccf52ffdbf978ca7de5679fc9312bfa388a1f4a Mon Sep 17 00:00:00 2001 From: anoushka Date: Tue, 4 Jun 2024 12:48:34 +0300 Subject: [PATCH 3/3] geeks for geeks solution --- .../gfg-solutions/0002-fascinating-number.md | 2 +- .../0003-at-least-two-greater-elements.md | 125 ++++++++++++++++++ 2 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 dsa-solutions/gfg-solutions/0003-at-least-two-greater-elements.md diff --git a/dsa-solutions/gfg-solutions/0002-fascinating-number.md b/dsa-solutions/gfg-solutions/0002-fascinating-number.md index 27d22ebbe..77c3646bd 100644 --- a/dsa-solutions/gfg-solutions/0002-fascinating-number.md +++ b/dsa-solutions/gfg-solutions/0002-fascinating-number.md @@ -139,6 +139,6 @@ The space complexity is $O(1)$ as well since the operations use a constant amoun ## References -- **LeetCode Problem:** [Geeks for Geeks Problem](https://www.geeksforgeeks.org/problems/fascinating-number3751/1?page=1&difficulty=School&sortBy=difficulty) +- **Geeks for Geeks Problem:** [Geeks for Geeks Problem](https://www.geeksforgeeks.org/problems/fascinating-number3751/1?page=1&difficulty=School&sortBy=difficulty) - **Solution Link:** [Fascinating Number on Geeks for Geeks](https://www.geeksforgeeks.org/problems/fascinating-number3751/1?page=1&difficulty=School&sortBy=difficulty) - **Authors LeetCode Profile:** [Anoushka](https://www.geeksforgeeks.org/user/iamanolive/) \ No newline at end of file diff --git a/dsa-solutions/gfg-solutions/0003-at-least-two-greater-elements.md b/dsa-solutions/gfg-solutions/0003-at-least-two-greater-elements.md new file mode 100644 index 000000000..17f07b608 --- /dev/null +++ b/dsa-solutions/gfg-solutions/0003-at-least-two-greater-elements.md @@ -0,0 +1,125 @@ +--- +id: at-least-two-greater-elements +title: At Least Two Greater Elements Problem (Geeks for Geeks) +sidebar_label: 0003 - At Least Two Greater Elements +tags: + - Beginner + - Array + - Slicing + - Sort + - Geeks for Geeks + - CPP + - Python + - DSA +description: "This is a solution to the At Least Two Greater Elements problem on Geeks for Geeks." +--- + +This tutorial contains a complete walk-through of the At Least Two Greater Elements problem from the Geeks for Geeks website. It features the implementation of the solution code in two programming languages: Python and C++. + +## Problem Description + +Given an array of N distinct elements, the task is to find all elements in array except two greatest elements in sorted order. + +## Examples + +**Example 1:** + +``` +Input : a[] = {2, 8, 7, 1, 5} +Output : 1 2 5 +Explanation : The output three elements have two or more greater elements. +``` + +**Example 2:** + +``` +Input : a[] = {7, -2, 3, 4, 9, -1} +Output : -2 -1 3 4 + +``` + +## Your Task + +You don't need to read input or print anything. Your task is to complete the function `findElements()` which takes the array `A[]` and its size N as inputs and return the vector sorted values denoting the elements in array which have at-least two greater elements than themselves. + + +Expected Time Complexity: $O(N*Log(N)$ +Expected Auxiliary Space: $O(N)$ + +## Constraints + +* `3 ≤ N ≤ 10^5` +* `-10^6 ≤ Ai ≤ 10^6` + +## Problem Explanation + +The problem is to find all elements in an array except the two greatest elements and return them in sorted order. In simpler terms, you want to remove the two largest elements from the array and sort the remaining elements. + +## Code Implementation + + + + + ```py + class Solution: + def findElements(self, a, n): + a.sort(); return a[0 : -2] + ``` + + + + + + ```cpp + class Solution { + public: + vector findElements(int a[], int n) { + sort(a, a + n); + vector result; + for (int i = 0; i < n - 2; i++) + result.push_back(a[i]); + return result; + } +}; +``` + + + + + +## Example Walkthrough + +For the array `[2, 8, 7, 1, 5]`: + +1. The two greatest elements are 8 and 7. +2. Removing these two, the remaining elements are `[2, 1, 5]`. +3. Sorting these remaining elements gives `[1, 2, 5]`. + +For the array `[7, −2, 3, 4, 9, −1]`: +1. The two greatest elements are 9 and 7 +2. Removing these two, the remaining elements are `[−2, 3, 4, −1]`. +3. Sorting these remaining elements gives `[−2, −1, 3, 4]`. + + +## Solution Logic: + +1. Sort the Array: First, sort the entire array. +2. Remove the Last Two Elements: After sorting, the last two elements will be the greatest. Removing these will leave us with the elements that have at least two greater elements. +3. Return the Result: The remaining sorted elements are the desired result. + +## Time Complexity + +* Sorting the Array: The primary operation is sorting the array, which has a time complexity of $O(N*Log(N)$, where N is the number of elements in the array. +* Slicing the Array: Extracting the elements excluding the last two elements has a time complexity of $O(1)$. + + +## Space Complexity + +Auxiliary Space: The auxiliary space complexity is $O(N)$ because the sorting algorithm typically requires additional space proportional to the size of the input array. + + +## References + +- **Geeks for Geeks Problem:** [Geeks for Geeks Problem](https://www.geeksforgeeks.org/problems/at-least-two-greater-elements4625/1?page=1&difficulty=School&sortBy=difficulty) +- **Solution Link:** [At Least Two Greater Elements on Geeks for Geeks](https://www.geeksforgeeks.org/problems/at-least-two-greater-elements4625/1?page=1&difficulty=School&sortBy=difficulty) +- **Authors LeetCode Profile:** [Anoushka](https://www.geeksforgeeks.org/user/iamanolive/) \ No newline at end of file