# Leetcode – Replace Elements with Greatest Element on Right Side Solution

The problem “Replace Elements with Greatest Element on Right Side” on Leetcode serves as an insightful example of how array manipulations can be optimized with a clever approach. The problem allows us to dive deep into Python’s array handling capabilities, time complexity issues, and algorithmic improvements. This comprehensive guide aims to cover multiple methods for solving this problem and analyze their efficiency.

1. Problem Description
2. Brute-force Approach
3. Improved Iterative Approach
4. Right-to-Left Iteration Approach
5. Testing and Edge Cases
6. Time and Space Complexity
7. Conclusion

## 1. Problem Description

Given an array arr, replace every element in the array with the greatest element among the elements to its right, and replace the last element with -1.

### Example:

Input: [17, 18, 5, 4, 6, 1]

Output: [18, 6, 6, 6, 1, -1]

### Constraints:

• 1 <= arr.length <= 10^4
• 1 <= arr[i] <= 10^5

## 2. Brute-force Approach

The most straightforward method is to traverse each element and find the maximum element on its right side by initiating another nested loop.

### Algorithm:

1. Loop over each element i in the array.
2. Initialize a maximum element variable for each i.
3. Loop over the elements to the right of i, update the maximum element if a greater element is found.
4. Replace the element at index i with the maximum element.
5. Replace the last element with -1.

### Python Code:

def replaceElements(arr):
n = len(arr)

for i in range(n-1):
max_elem = -1
for j in range(i + 1, n):
max_elem = max(max_elem, arr[j])
arr[i] = max_elem

arr[-1] = -1
return arr

## 3. Improved Iterative Approach

One improvement could be to store the maximum elements you find in your iteration, so you don’t have to recompute them for every element.

### Algorithm:

1. Initialize an empty list called max_elements.
2. Loop through the array from right to left, updating the current maximum element.
3. Append the current maximum element to max_elements at each iteration.
4. Reverse max_elements and append -1.
5. Return max_elements.

### Python Code:

def replaceElements(arr):
max_elements = []
max_elem = -1

for num in reversed(arr):
max_elem = max(max_elem, num)
max_elements.append(max_elem)

max_elements.reverse()
max_elements.pop(0)
max_elements.append(-1)

return max_elements

## 4. Right-to-Left Iteration Approach

The optimal approach is to start from the last element and work our way to the first element, keeping track of the maximum element we’ve encountered so far.

### Algorithm:

1. Initialize max_elem as -1.
2. Loop through the array from right to left.
3. At each iteration, replace the current element with max_elem and then update max_elem.

### Python Code:

def replaceElements(arr):
max_elem = -1
for i in range(len(arr) - 1, -1, -1):
new_max = max(max_elem, arr[i])
arr[i] = max_elem
max_elem = new_max
return arr

## 5. Testing and Edge Cases

1. Single-element arrays.
2. Arrays where all elements are equal.
3. Arrays that are sorted in ascending or descending order.

## 6. Time and Space Complexity

1. Brute-force Approach:
• Time Complexity: O(n^2)
• Space Complexity: O(1)
2. Improved Iterative Approach:
• Time Complexity: O(n)
• Space Complexity: O(n)
3. Right-to-Left Iteration Approach:
• Time Complexity: O(n)
• Space Complexity: O(1)

## 7. Conclusion

The problem of replacing elements with the greatest element on the right side showcases the beauty of algorithmic thinking. It allows us to understand how a seemingly naïve problem can have multiple layers of optimization. We covered three different methods, each with its own set of trade-offs in terms of time and space complexity.