Skip to main content

Greater on Right Side

Problem​

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.

Examples:​

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)O(N)
  • Expected Auxiliary Space: O(1)O(1)

Constraints:​

  • 1<=N<=1051<=N<=10^5
  • 1<=Arri<=1061 <= Arr_i <= 10^6

Solution​

Python​

def nextGreatest(self,arr, n):
if n == 0:
return
max_right = -1
for i in range(n-1, -1, -1):
current = arr[i]
arr[i] = max_right
max_right = max(max_right, current)
arr[n-1] = -1

Java​

void nextGreatest(int arr[], int n) {
if (n == 0) {
return;
}
int maxRight = -1;
for (int i = n - 1; i >= 0; i--) {
int current = arr[i];
arr[i] = maxRight;
maxRight = Math.max(maxRight, current);
}
arr[n - 1] = -1;
}

C++​

void nextGreatest(int arr[], int n) {
if (n == 0) {
return;
}
int maxRight = -1;
for (int i = n - 1; i >= 0; i--) {
int current = arr[i];
arr[i] = maxRight;
maxRight = std::max(maxRight, current);
}
arr[n - 1] = -1;
}

C​

void nextGreatest(int arr[], int n) {
if (n == 0) {
return;
}

int maxRight = -1;
for (int i = n - 1; i >= 0; i--) {
int current = arr[i];
arr[i] = maxRight;
maxRight = (maxRight > current) ? maxRight : current;
}
arr[n - 1] = -1;
}
  • Time Complexity: O(N)O(N)
  • Auxiliary Space: O(1)O(1)