Skip to content

Commit ac7fbb2

Browse files
authored
Merge pull request #3702 from Ishitamukherjee2004/main
Array leaders from gfg is added
2 parents f464ce3 + e96cafd commit ac7fbb2

File tree

1 file changed

+228
-0
lines changed

1 file changed

+228
-0
lines changed
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
---
2+
id: array-leaders
3+
title: Array Leaders
4+
sidebar_label: Array-Leaders
5+
tags:
6+
- Array
7+
- Data Structure
8+
description: "This tutorial covers the solution to the Array Leaders problem from the GeeksforGeeks website."
9+
---
10+
## Problem Description
11+
12+
Given an array `arr` of `n` positive integers, your task is to find all the leaders in the array. An element of the array is considered a leader if it is greater than all the elements on its right side or if it is equal to the maximum element on its right side. The rightmost element is always a leader.
13+
14+
## Examples
15+
16+
**Example 1:**
17+
18+
```
19+
Input:
20+
arr = [16, 17, 4, 3, 5, 2]
21+
Output: 17, 5, 2
22+
Explanation: The leaders are 17, 5, and 2.
23+
```
24+
25+
**Example 2:**
26+
27+
```
28+
Input:
29+
arr = [1, 2, 3, 4, 0]
30+
Output: 4, 0
31+
Explanation: The leaders are 4 and 0.
32+
```
33+
34+
## Your Task
35+
36+
You don't need to read input or print anything. Your task is to complete the function `leaders()` which takes the array `arr[]` and its size `n` as input parameters and returns a list of integers containing the leaders in the array.
37+
38+
Expected Time Complexity: $O(n)$
39+
40+
Expected Auxiliary Space: $O(n)$
41+
42+
## Constraints
43+
44+
* `1 ≤ n ≤ 10^7`
45+
* `0 ≤ arr[i] ≤ 10^7`
46+
47+
## Problem Explanation
48+
49+
The task is to find all the leaders in the array, where a leader is an element that is greater than or equal to all the elements to its right. The rightmost element is always a leader.
50+
51+
## Code Implementation
52+
53+
<Tabs>
54+
<TabItem value="Python" label="Python" default>
55+
<SolutionAuthor name="@Ishitamukherjee2004"/>
56+
57+
```python
58+
class Solution:
59+
def leaders(self, arr, n):
60+
leaders = []
61+
max_from_right = arr[-1]
62+
leaders.append(max_from_right)
63+
64+
for i in range(n-2, -1, -1):
65+
if arr[i] >= max_from_right:
66+
leaders.append(arr[i])
67+
max_from_right = arr[i]
68+
69+
return leaders[::-1]
70+
71+
# Example usage
72+
if __name__ == "__main__":
73+
solution = Solution()
74+
arr = [16, 17, 4, 3, 5, 2]
75+
print(solution.leaders(arr, len(arr))) # Expected output: [17, 5, 2]
76+
```
77+
78+
</TabItem>
79+
<TabItem value="C++" label="C++">
80+
<SolutionAuthor name="@Ishitamukherjee2004"/>
81+
82+
```cpp
83+
//{ Driver Code Starts
84+
#include <bits/stdc++.h>
85+
using namespace std;
86+
87+
// } Driver Code Ends
88+
class Solution {
89+
public:
90+
vector<int> leaders(int n, int arr[]) {
91+
vector<int> leaders;
92+
int max_from_right = arr[n-1];
93+
leaders.push_back(max_from_right);
94+
95+
for (int i = n-2; i >= 0; i--) {
96+
if (arr[i] >= max_from_right) {
97+
leaders.push_back(arr[i]);
98+
max_from_right = arr[i];
99+
}
100+
}
101+
102+
reverse(leaders.begin(), leaders.end());
103+
return leaders;
104+
}
105+
};
106+
107+
//{ Driver Code Starts.
108+
int main() {
109+
long long t;
110+
cin >> t; // testcases
111+
while (t--) {
112+
long long n;
113+
cin >> n; // total size of array
114+
115+
int arr[n];
116+
117+
// inserting elements in the array
118+
for (long long i = 0; i < n; i++) {
119+
cin >> arr[i];
120+
}
121+
Solution obj;
122+
// calling leaders() function
123+
vector<int> v = obj.leaders(n, arr);
124+
125+
// printing elements of the vector
126+
for (auto it = v.begin(); it != v.end(); it++) {
127+
cout << *it << " ";
128+
}
129+
130+
cout << endl;
131+
}
132+
}
133+
// } Driver Code Ends
134+
```
135+
136+
</TabItem>
137+
138+
<TabItem value="Javascript" label="Javascript" default>
139+
<SolutionAuthor name="@Ishitamukherjee2004"/>
140+
141+
```javascript
142+
class Solution {
143+
leaders(arr, n) {
144+
let leaders = [];
145+
let maxFromRight = arr[n - 1];
146+
leaders.push(maxFromRight);
147+
for (let i = n - 2; i >= 0; i--) {
148+
if (arr[i] >= maxFromRight) {
149+
leaders.push(arr[i]);
150+
maxFromRight = arr[i];
151+
}
152+
}
153+
return leaders.reverse();
154+
}
155+
}
156+
157+
```
158+
159+
</TabItem>
160+
161+
<TabItem value="Typescript" label="Typescript" default>
162+
<SolutionAuthor name="@Ishitamukherjee2004"/>
163+
164+
```typescript
165+
class Solution {
166+
leaders(arr: number[], n: number): number[] {
167+
let leaders: number[] = [];
168+
let maxFromRight: number = arr[n - 1];
169+
leaders.push(maxFromRight);
170+
for (let i: number = n - 2; i >= 0; i--) {
171+
if (arr[i] >= maxFromRight) {
172+
leaders.push(arr[i]);
173+
maxFromRight = arr[i];
174+
}
175+
}
176+
return leaders.reverse();
177+
}
178+
}
179+
180+
181+
```
182+
183+
</TabItem>
184+
185+
<TabItem value="Java" label="Java" default>
186+
<SolutionAuthor name="@Ishitamukherjee2004"/>
187+
188+
```java
189+
import java.util.ArrayList;
190+
import java.util.Collections;
191+
192+
public class Solution {
193+
public ArrayList<Integer> leaders(ArrayList<Integer> arr, int n) {
194+
ArrayList<Integer> leaders = new ArrayList<Integer>();
195+
int maxFromRight = arr.get(n - 1);
196+
leaders.add(maxFromRight);
197+
for (int i = n - 2; i >= 0; i--) {
198+
if (arr.get(i) >= maxFromRight) {
199+
leaders.add(arr.get(i));
200+
maxFromRight = arr.get(i);
201+
}
202+
}
203+
Collections.reverse(leaders);
204+
return leaders;
205+
}
206+
}
207+
208+
```
209+
210+
</TabItem>
211+
</Tabs>
212+
213+
214+
## Solution Logic:
215+
216+
1. Traverse the array from right to left.
217+
2. Keep track of the maximum element encountered so far.
218+
3. If the current element is greater than or equal to the maximum element, it is a leader.
219+
4. Add the leader to the result list and update the maximum element.
220+
5. Return the result list in reverse order.
221+
222+
## Time Complexity
223+
224+
* The function iterates through the array once, so the time complexity is $O(n)$.
225+
226+
## Space Complexity
227+
228+
* The function uses additional space for the result list, so the auxiliary space complexity is $O(n)$.

0 commit comments

Comments
 (0)