Skip to content

Commit 959b2fb

Browse files
authored
Merge pull request #509 from iamanolive/main
[Geeks for Geeks Solution]: At least two greater elements
2 parents 2207a72 + 794953a commit 959b2fb

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed

dsa-solutions/gfg-solutions/0002-fascinating-number.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,6 @@ The space complexity is $O(1)$ as well since the operations use a constant amoun
139139

140140
## References
141141

142-
- **LeetCode Problem:** [Geeks for Geeks Problem](https://www.geeksforgeeks.org/problems/fascinating-number3751/1?page=1&difficulty=School&sortBy=difficulty)
142+
- **Geeks for Geeks Problem:** [Geeks for Geeks Problem](https://www.geeksforgeeks.org/problems/fascinating-number3751/1?page=1&difficulty=School&sortBy=difficulty)
143143
- **Solution Link:** [Fascinating Number on Geeks for Geeks](https://www.geeksforgeeks.org/problems/fascinating-number3751/1?page=1&difficulty=School&sortBy=difficulty)
144144
- **Authors LeetCode Profile:** [Anoushka](https://www.geeksforgeeks.org/user/iamanolive/)
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
id: at-least-two-greater-elements
3+
title: At Least Two Greater Elements Problem (Geeks for Geeks)
4+
sidebar_label: 0003 - At Least Two Greater Elements
5+
tags:
6+
- Beginner
7+
- Array
8+
- Slicing
9+
- Sort
10+
- Geeks for Geeks
11+
- CPP
12+
- Python
13+
- DSA
14+
description: "This is a solution to the At Least Two Greater Elements problem on Geeks for Geeks."
15+
---
16+
17+
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++.
18+
19+
## Problem Description
20+
21+
Given an array of N distinct elements, the task is to find all elements in array except two greatest elements in sorted order.
22+
23+
## Examples
24+
25+
**Example 1:**
26+
27+
```
28+
Input : a[] = {2, 8, 7, 1, 5}
29+
Output : 1 2 5
30+
Explanation : The output three elements have two or more greater elements.
31+
```
32+
33+
**Example 2:**
34+
35+
```
36+
Input : a[] = {7, -2, 3, 4, 9, -1}
37+
Output : -2 -1 3 4
38+
39+
```
40+
41+
## Your Task
42+
43+
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.
44+
45+
46+
Expected Time Complexity: $O(N*Log(N)$
47+
Expected Auxiliary Space: $O(N)$
48+
49+
## Constraints
50+
51+
* `3 ≤ N ≤ 10^5`
52+
* `-10^6 ≤ Ai ≤ 10^6`
53+
54+
## Problem Explanation
55+
56+
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.
57+
58+
## Code Implementation
59+
60+
<Tabs>
61+
<TabItem value="Python" label="Python" default>
62+
<SolutionAuthor name="@iamanolive"/>
63+
```py
64+
class Solution:
65+
def findElements(self, a, n):
66+
a.sort(); return a[0 : -2]
67+
```
68+
69+
</TabItem>
70+
<TabItem value="C++" label="C++">
71+
<SolutionAuthor name="@iamanolive"/>
72+
73+
```cpp
74+
class Solution {
75+
public:
76+
vector<int> findElements(int a[], int n) {
77+
sort(a, a + n);
78+
vector <int> result;
79+
for (int i = 0; i < n - 2; i++)
80+
result.push_back(a[i]);
81+
return result;
82+
}
83+
};
84+
```
85+
86+
</TabItem>
87+
</Tabs>
88+
89+
90+
## Example Walkthrough
91+
92+
For the array `[2, 8, 7, 1, 5]`:
93+
94+
1. The two greatest elements are 8 and 7.
95+
2. Removing these two, the remaining elements are `[2, 1, 5]`.
96+
3. Sorting these remaining elements gives `[1, 2, 5]`.
97+
98+
For the array `[7, −2, 3, 4, 9, −1]`:
99+
1. The two greatest elements are 9 and 7
100+
2. Removing these two, the remaining elements are `[−2, 3, 4, −1]`.
101+
3. Sorting these remaining elements gives `[−2, −1, 3, 4]`.
102+
103+
104+
## Solution Logic:
105+
106+
1. Sort the Array: First, sort the entire array.
107+
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.
108+
3. Return the Result: The remaining sorted elements are the desired result.
109+
110+
## Time Complexity
111+
112+
* 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.
113+
* Slicing the Array: Extracting the elements excluding the last two elements has a time complexity of $O(1)$.
114+
115+
116+
## Space Complexity
117+
118+
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.
119+
120+
121+
## References
122+
123+
- **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)
124+
- **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)
125+
- **Authors LeetCode Profile:** [Anoushka](https://www.geeksforgeeks.org/user/iamanolive/)

0 commit comments

Comments
 (0)