Star elements | Practice | GeeksforGeeks

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.
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.
Example 1:
Input:
n = 10
arr[] = {1, 6, 5, 4, 7, 8, 4, 3, 2, 1}
Output: 
8 4 3 2 1 
8
Explanation: Star elements are 8, 4, 3, 2 and 1.
Super star element is 8.

Example 2:
Input:
a = 9
arr[] = {1, 2, 10, 3, 4, 5, 8, 10, 4}
Output: 
10 4 
-1
Explanation: Star elements are 10 and 4.
No super star element present here.

Your Task:
You don't need to read input or print anything. Your task is to complete the function getStarAndSuperStar() which takes the array of integers arr and n as parameters and returns an array of integers denoting the answer where first element is the super star element and rest are star elements. Return -1 in first element of the array if no super star element is present. The driver code itself prints the star elements in first line and super star element in second line.
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(1)
Constraints:
1 <= n <= 106
0 <= |arr[i]| <= 106
 
0
thakuraditya6216 days ago
 
public static int[] getStarAndSuperStar(int[] arr, int n) {
        // code here
        Stack<Integer> st = new Stack<>();
        int max = Integer.MIN_VALUE;
        
        // loop from right to valid the case 1 
        // case1 : Star are those elements which are strictly 
        //         greater than all the elements on its right side
        for(int i = n-1; i >= 0; i--){
            if(arr[i] > max){
                st.push(arr[i]);
                max = arr[i];
            }
        }
        
        // find super star max
        int count = 0;
        for(int i = 0; i < n ; i++){
            if(arr[i] == max){
                count++;
            }
        }
        
        // push max element at top
        if(count > 1){
            st.push(-1);
        } else {
            st.push(max);
        }
        
        // reverse the st for matching the answer output
        Collections.reverse(st);
        
        int [] ans = new int[st.size()];
        int i = 0;
        
        for(Integer val : st){
            ans[i++] = val;
        }
        
        return ans;
    }
0
0902ee1810132 weeks ago
java:=
public static int[] getStarAndSuperStar(int[] arr, int n) {       // code here             Stack<Integer> s = new Stack<>();       int max = Integer.MIN_VALUE;       for(int i =n-1;i>=0;i--){           if(arr[i]>max){               s.push(arr[i]);               max=arr[i];           }       }       int c=0;       for(int i=0;i<n;i++){           if(arr[i]==max){               c++;           }       }       if(c>1){           s.push(-1);       }else{           s.push(max);       }       Collections.reverse(s);        int[] d = new int[s.size()];       int count=0;              for(Integer item: s){           d[count++]=item;       }       return d;          }
0
chessnoobdj4 months ago
C++
vector<int> getStarAndSuperStar(int arr[], int n) {
    vector <int> res;
    int max_ele = INT_MIN;
    for(int i=n-1; i>=0; i--){
        if(arr[i] > max_ele){
            max_ele = arr[i];
            res.push_back(arr[i]);
        }
    }
    int cnt = count(arr, arr+n, max_ele);
    int super_star = (cnt > 1) ? -1 : max_ele;
    res.push_back(super_star);
    reverse(res.begin(), res.end());
    return res;
}
+1
badgujarsachin835 months ago
vector<int> getStarAndSuperStar(int arr[], int n) {
    // code here   
    vector<int> v;
    int mx=INT_MIN;
    for(int i=n-1;i>=0;i--){
        if(arr[i]>mx){
            v.push_back(arr[i]);
            mx=arr[i];
        }
    }
    int c=0;
    for(int i=0;i<n;i++){
        if(arr[i]==mx){
            c++;
        }
        
    }
    if(c>1){
        v.push_back(-1);
    }else{
        v.push_back(mx);
    }
    reverse(v.begin(),v.end());
    return v;
}
-1
saranyamaity20007 months ago
in O(N) time and O(1) space 
vector<int> getStarAndSuperStar(int arr[], int n) {
    
    vector<int> ans ; 
    
    int maxi = n-1 ; 
    
    ans.push_back(maxi) ;
    
    for(int i  = n-2 ; i>=0 ; i--)
    {
        if(arr[i] >= arr[maxi])
        {
            if(arr[i] > arr[maxi])
               ans.push_back(i) ;
            maxi = i ; 
        }
    }
    
    n = ans.size() ; 
    
    if(maxi != ans[n-1]) ans.push_back(-1) ; 
    else ans.push_back(ans[n-1]) ; 
    
    reverse(ans.begin(), ans.end()) ; 
   
    for(int i = 0 ; i<n+1 ; i++)
    {
        if(ans[i] != -1) ans[i] = arr[ans[i]] ; 
    }
     
    return ans ; 
}
0
Subh Arora9 months ago
Subh Arora
def getStarAndSuperStar(arr, n):    # code here    star=[arr[-1]]    super_star=[]    for i in arr[-2:-len(arr)-1:-1]:        #print(i)        if i>star[-1]:            star.append(i)    max_ele=max(star)    count=0    for i in arr:        if i==max_ele:            count+=1    if count>1:        max_ele=-1    return [max_ele," ".join(str(x) for x in star[::-1])]
0
shahzdor9 months ago
shahzdor
Python solution:Execution time: 0.2 sec
def getStarAndSuperStar(arr, n):  maximum=max(arr)  indexx=[i for i in range(n) if arr[i]==maximum]  if len(indexx)>1:    superstar=[-1]  else:    superstar=[maximum]  start_index=indexx[-1]  star=[]  for i in range(-1,start_index-n-1,-1):    if i==-1:      star.append(arr[i])    elif arr[i]>star[-1]:      star.append(arr[i])  return superstar+star[::-1]
-1
SACHIN KUMAR11 months ago
SACHIN KUMAR
vector<int> getStarAndSuperStar(int arr[], int n) {    vector<int>v;    int mx = INT_MIN;    for(int i=n-1;i>=0;i--)    {        if(arr[i]>mx)        {            v.push_back(arr[i]);            mx = arr[i];        }    }    int c=0;    for(int i=0;i<n;i++) {="" if(arr[i]="=mx)" c++;="" }="" if(c="">1)    v.push_back(-1);    else    v.push_back(mx);    reverse(v.begin(),v.end());    return v;}
0
jasmeen kaur1 year ago
jasmeen kaur
Correct Answer.Correct AnswerExecution Time:0.03void star(int arr[],int n){    vector<int>s;    s.push_back(arr[n-1]);
    int x = arr[n-1];     for(int i=n-2;i>=0;i--)    {        if(arr[i]>x)        {            s.push_back(arr[i]);            x=arr[i];        }    }
    reverse(s.begin(),s.end());    for(int i=0;i<s.size();i++){ cout<<s.at(i)<<"="" ";="" }="" cout<<endl;="" int="" m="*max_element(s.begin()," s.end())="" ;="" int="" c="count(arr,arr+n,m);" if(c="">1)    cout<<"-1"<<endl; else="" cout<<m<<endl;="" }="" int="" main()="" {="" int="" t;="" cin="">>t;while(t--){    int size; cin>>size;    int arr[size];    for(int i=0;i<size;i++){ cin="">>arr[i];    }    star(arr,size);}return 0;}
0
Chandra Shekhar Joshi1 year ago
Chandra Shekhar Joshi
c++ solution #include<bits stdc++.h="">using namespace std;int main() {int t;cin>>t;while(t--){    int n;    cin>>n;    int *a = new int [n];    for(int i=0;i<n;i++) {="" cin="">>a[i];    }    stack<int> st;    st.push(a[n-1]);    int c_max=0;    for(int i=n-2;i>=0;i--)    {        if(st.top()==a[i])        {            c_max++;        }
        if(st.top()<a[i]) {="" st.push(a[i]);="" c_max="0;" }="" }="" int="" ss="st.top();" while(!st.empty())="" {="" cout<<st.top()<<"="" ";="" st.pop();="" }="" cout<<endl;="" if(c_max="">0)     cout<<"-1"<<endl; else="" cout<<ss<<endl;="" }="" return="" 0;="" }="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.