1299. Replace Elements with Greatest Element on Right Side LeetCode Solution
In this guide, you will get 1299. Replace Elements with Greatest Element on Right Side LeetCode Solution with the best time and space complexity. The solution to Replace Elements with Greatest Element on Right Side problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Replace Elements with Greatest Element on Right Side solution in C++
- Replace Elements with Greatest Element on Right Side solution in Java
- Replace Elements with Greatest Element on Right Side solution in Python
- Additional Resources

Problem Statement of Replace Elements with Greatest Element on Right Side
Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1.
After doing so, return the array.
Example 1:
Input: arr = [17,18,5,4,6,1]
Output: [18,6,6,6,1,-1]
Explanation:
– index 0 –> the greatest element to the right of index 0 is index 1 (18).
– index 1 –> the greatest element to the right of index 1 is index 4 (6).
– index 2 –> the greatest element to the right of index 2 is index 4 (6).
– index 3 –> the greatest element to the right of index 3 is index 4 (6).
– index 4 –> the greatest element to the right of index 4 is index 5 (1).
– index 5 –> there are no elements to the right of index 5, so we put -1.
Example 2:
Input: arr = [400]
Output: [-1]
Explanation: There are no elements to the right of index 0.
Constraints:
1 <= arr.length <= 104
1 <= arr[i] <= 105
Complexity Analysis
- Time Complexity:
- Space Complexity:
1299. Replace Elements with Greatest Element on Right Side LeetCode Solution in C++
class Solution {
public:
vector<int> replaceElements(vector<int>& arr) {
int maxOfRight = -1;
for (int i = arr.size() - 1; i >= 0; --i)
maxOfRight = max(maxOfRight, exchange(arr[i], maxOfRight));
return arr;
}
};
/* code provided by PROGIEZ */
1299. Replace Elements with Greatest Element on Right Side LeetCode Solution in Java
class Solution {
public int[] replaceElements(int[] arr) {
int maxOfRight = -1;
for (int i = arr.length - 1; i >= 0; --i) {
int a = arr[i];
arr[i] = maxOfRight;
maxOfRight = Math.max(maxOfRight, a);
}
return arr;
}
}
// code provided by PROGIEZ
1299. Replace Elements with Greatest Element on Right Side LeetCode Solution in Python
class Solution:
def replaceElements(self, arr: list[int]) -> list[int]:
maxOfRight = -1
for i in reversed(range(len(arr))):
arr[i], maxOfRight = maxOfRight, max(maxOfRight, arr[i])
return arr
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.