Skip to content

Commit f617acf

Browse files
authored
Merge pull request #4210 from Ishitamukherjee2004/new-branch
Star Elements problem from gfg is added
2 parents 3a02a4d + 617fc3e commit f617acf

File tree

1 file changed

+192
-0
lines changed

1 file changed

+192
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
---
2+
id: star-elements
3+
title: Star Elements
4+
sidebar_label: Star-Elements
5+
tags:
6+
- Modular Arithmetic
7+
- Algorithms
8+
description: "This tutorial covers the solution to the Star elements problem from the GeeksforGeeks."
9+
---
10+
## Problem Description
11+
Given an unsorted array `arr` of size `n`. The task is to find all the star and super star elements in the array. Star are those elements which are strictly greater than all the elements on its right side. Super star are those elements which are strictly greater than all the elements on its left and right side.
12+
13+
Note: Assume first element (`arr[0]`) is greater than all the elements on its left side, And last element `(arr[n-1])` is greater than all the elements on its right side.
14+
15+
## Examples
16+
17+
**Example 1:**
18+
19+
```
20+
Input:
21+
n = 10
22+
arr[] = {1, 6, 5, 4, 7, 8, 4, 3, 2, 1}
23+
Output:
24+
8 4 3 2 1
25+
8
26+
Explanation: Star elements are 8, 4, 3, 2 and 1.
27+
Super star element is 8.
28+
```
29+
30+
**Example 2:**
31+
32+
```
33+
Input:
34+
a = 9
35+
arr[] = {1, 2, 10, 3, 4, 5, 8, 10, 4}
36+
Output:
37+
10 4
38+
-1
39+
Explanation: Star elements are 10 and 4.
40+
No super star element present here.
41+
```
42+
43+
44+
Expected Time Complexity: O(n)
45+
46+
Expected Auxiliary Space: O(1)
47+
48+
## Constraints
49+
50+
* `1 ≤ N ≤ 10^6`
51+
52+
## Problem Explanation
53+
54+
The task is to traverse the array and search the number.
55+
56+
## Code Implementation
57+
58+
### C++ Solution
59+
60+
```cpp
61+
#include <iostream>
62+
using namespace std;
63+
64+
void findStars(int arr[], int n) {
65+
int maxRight = arr[n-1];
66+
cout << "Super Stars: ";
67+
for (int i = n-1; i >= 0; i--) {
68+
if (arr[i] > maxRight) {
69+
cout << arr[i] << " ";
70+
maxRight = arr[i];
71+
}
72+
}
73+
cout << endl;
74+
cout << "Stars: ";
75+
maxRight = arr[n-1];
76+
for (int i = n-2; i >= 0; i--) {
77+
if (arr[i] > maxRight) {
78+
cout << arr[i] << " ";
79+
maxRight = arr[i];
80+
}
81+
}
82+
cout << endl;
83+
}
84+
85+
int main() {
86+
int arr[] = {4, 6, 3, 7, 2, 8};
87+
int n = sizeof(arr)/sizeof(arr[0]);
88+
findStars(arr, n);
89+
return 0;
90+
}
91+
92+
93+
94+
```
95+
96+
```java
97+
98+
public class Main {
99+
public static void findStars(int[] arr) {
100+
int maxRight = arr[arr.length-1];
101+
System.out.print("Super Stars: ");
102+
for (int i = arr.length-1; i >= 0; i--) {
103+
if (arr[i] > maxRight) {
104+
System.out.print(arr[i] + " ");
105+
maxRight = arr[i];
106+
}
107+
}
108+
System.out.println();
109+
System.out.print("Stars: ");
110+
maxRight = arr[arr.length-1];
111+
for (int i = arr.length-2; i >= 0; i--) {
112+
if (arr[i] > maxRight) {
113+
System.out.print(arr[i] + " ");
114+
maxRight = arr[i];
115+
}
116+
}
117+
System.out.println();
118+
}
119+
120+
public static void main(String[] args) {
121+
int[] arr = {4, 6, 3, 7, 2, 8};
122+
findStars(arr);
123+
}
124+
}
125+
126+
127+
128+
```
129+
130+
```python
131+
def find_stars(arr):
132+
max_right = arr[-1]
133+
super_stars = []
134+
for i in range(len(arr)-1, -1, -1):
135+
if arr[i] > max_right:
136+
super_stars.append(arr[i])
137+
max_right = arr[i]
138+
print("Super Stars:", super_stars)
139+
max_right = arr[-1]
140+
stars = []
141+
for i in range(len(arr)-2, -1, -1):
142+
if arr[i] > max_right:
143+
stars.append(arr[i])
144+
max_right = arr[i]
145+
print("Stars:", stars)
146+
147+
arr = [4, 6, 3, 7, 2, 8]
148+
find_stars(arr)
149+
150+
```
151+
152+
```javascript
153+
function findStars(arr) {
154+
let maxRight = arr[arr.length-1];
155+
let superStars = [];
156+
for (let i = arr.length-1; i >= 0; i--) {
157+
if (arr[i] > maxRight) {
158+
superStars.push(arr[i]);
159+
maxRight = arr[i];
160+
}
161+
}
162+
console.log("Super Stars:", superStars);
163+
maxRight = arr[arr.length-1];
164+
let stars = [];
165+
for (let i = arr.length-2; i >= 0; i--) {
166+
if (arr[i] > maxRight) {
167+
stars.push(arr[i]);
168+
maxRight = arr[i];
169+
}
170+
}
171+
console.log("Stars:", stars);
172+
}
173+
174+
175+
```
176+
177+
## Solution Logic:
178+
1. Initialize maxRight to the last element of the array.
179+
2. Iterate through the array from right to left (starting from the second last element).
180+
3. For each element, check if it is greater than maxRight. If it is, add it to the stars array and update maxRight.
181+
4. Repeat steps 2-3 until the beginning of the array is reached.
182+
5. To find super stars, repeat steps 2-4, but add elements to the superStars array only if they are greater than maxRight and also greater than all elements on their left side.
183+
184+
185+
## Time Complexity
186+
187+
* The time complexity is $O(log(n))$ where n is the input number.
188+
189+
190+
## Space Complexity
191+
192+
* The auxiliary space complexity is $O(1)$ due to the only extra memory used is for temporary variables while swapping two values in Array.

0 commit comments

Comments
 (0)