Greater on right side | Practice | GeeksforGeeks

You are given an array Arr of size N. Replace every element with the next greatest element (greatest element on its right side) in the array. Also, since there is no element next to the last element, replace it with -1.
Example 1:
Input:
N = 6
Arr[] = {16, 17, 4, 3, 5, 2}
Output:
17 5 5 5 2 -1
Explanation: For 16 the greatest element 
on its right is 17. For 17 it's 5. 
For 4 it's 5. For 3 it's 5. For 5 it's 2. 
For 2 it's -1(no element to its right). 
So the answer is 17 5 5 5 2 -1
Example 2:
Input:
N = 4
Arr[] = {2, 3, 1, 9}
Output:
9 9 9 -1
Explanation: For each element except 9 the
greatest element on its right is 9.
So the answer is 9 9 9 -1
Your Task:  
You don't need to read input or print anything. Your task is to complete the function nextGreatest() which takes the array of integers arr and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 105
1 <= Arri <= 106
 
0
abhisheksanwal4 days ago
class Solution:
def nextGreatest(self,arr, n):  curr_max=-1  for i in range(n-1,-1,-1):          x=curr_max          if arr[i]> curr_max:         curr_max=arr[i]              arr[i]=x         return arr
 
 
Just think to tackle this problem from right side it is very easy than.
0
sanjeevranjan12092 weeks ago
Python Solution 
Full testcase passed
class Solution:

	def nextGreatest(self,arr, n):
	    m = max(arr[1:])
	    for i in range(n-1):
	        if arr[i]!=m:
	            arr[i]=m
	        else:
	            m = max(arr[i+1:])
	            arr[i]=m
	    arr[n-1]=-1
	    return arr
+1
maneshram202 weeks ago
Best C++ solution
class Solution{
public:	
	/* Function to replace every element with the
	next greatest element */
	void nextGreatest(int arr[], int n) {
	    int hi = arr[n-1];
	    for(int i=n-2;i>=0;i--){
	        if(hi<arr[i]){
	            swap(hi,arr[i]);
	        }
	        else arr[i] = hi;
	    }
	    arr[n-1] = -1;
	}
};
0
amulmittal3 weeks ago
def nextGreatest(self,arr, n):    for i in range(0,n-1):        if i!=0:            if arr[i]<arr[i-1]:                arr[i] = arr[i-1]            else:                arr[i] = max(arr[i+1:n])        else:            arr[i] = max(arr[i+1:n])    arr[n-1] = -1    return arr
0
shubhamrikhari86511 month ago
void nextGreatest(int arr[], int n) {    int maxi = arr[n-1];    for(int i=n-1; i>=0; i--) {        int temp = maxi;        maxi = max(maxi, arr[i]);        arr[i] = temp;    }     arr[n-1] = -1;}
0
optimusprime031 month ago
easy using stack
stack<int >st;       vector<int >v;       for(int i=n-1;i>=0;i-- )       {                      if(st.empty()==true)          {              v.push_back(-1);               st.push(arr[i]);                       }          else if(arr[i]>st.top())          {               v.push_back(st.top());              st.push(arr[i]);              //v.push_back(st.top());          }          else          {              v.push_back(st.top());          }
                                     }       reverse(v.begin(),v.end());       for(int i=0;i<n;i++)       {           arr[i]=v[i];       }              
0
abhinavjain78981 month ago
Easy Java Solution 
Time Complexity = O(N)
Space Complexity = O(1)
class Solution {
    void nextGreatest(int arr[], int n) {
        // code here
        int max = arr[n-1];
        for(int i=n-1;i>=0;i--){
            if(arr[i]>max){
                int temp = arr[i];
                arr[i] = max;
                max = temp;
            }
            else if(arr[i]<max){
                arr[i] = max;
            }
        }
        arr[n-1] = -1;
        
    }
}
0
anuragprajapati1 month ago
//java solution
class Solution {   void nextGreatest(int arr[], int n) {       // code here       int max=arr[n-1];           for(int j=n-1; j>=0; j--){               if(max<arr[j]){                   max=max+arr[j];                   arr[j]=max-arr[j];                   max=max-arr[j];               }else if(arr[j]<max){                   arr[j]=max;               }           }       arr[n-1]=-1;   }}
0
alirezachaviwala1 month ago
JAVA Solution Time Complexity O(N) Space Complexity O(1)
int maxValue=arr[n-1];

for(int i=n-1;i>=0;i--){
   if(arr[i]>maxValue){
       maxValue+=arr[i];
       arr[i]=maxValue-arr[i];
       maxValue-=arr[i];
   }
   else if(arr[i]<maxValue){
       arr[i]=maxValue;
   }
}
arr[n-1]=-1;
0
rakeshklk1 month ago
java solution : 
void nextGreatest(int arr[], int n)
{
     int largest,x,y;
     for (x =0; x < n; x++)     {        largest=-1;        for (y = x+1; y < n; y++)        {            if (largest < arr[y])            largest=arr[y];        }        arr[x]=largest;     }
}
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.