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 <= 1041 <= arr[i] <= 105When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for this problem involves looking at each position one at a time. For each position, we'll find the largest value among all the elements that come after it, and then replace the value at that position with that largest value.
Here's how the algorithm would work step-by-step:
def replace_elements_brute_force(numbers):
list_length = len(numbers)
for current_index in range(list_length - 1):
maximum_value = -1
# Find max from current to the end
for comparison_index in range(current_index + 1, list_length):
maximum_value = max(maximum_value, numbers[comparison_index])
numbers[current_index] = maximum_value
# The last element is always -1, per problem statement
numbers[list_length - 1] = -1
return numbersThe most efficient way is to start from the end and work backward. We keep track of the biggest number we've seen so far as we move left, updating each spot along the way.
Here's how the algorithm would work step-by-step:
def replace_elements(array):
current_greatest = -1
# Iterate backwards
for i in range(len(array) - 1, -1, -1):
# Store current element
current_element = array[i]
# Replace current with greatest
array[i] = current_greatest
# Update greatest if needed
if current_element > current_greatest:
# New greatest found
current_greatest = current_element
return array| Case | How to Handle |
|---|---|
| Empty array input | Return an empty array as specified since no replacement is possible. |
| Array with a single element | Replace the single element with -1 as the rightmost element has no right neighbor. |
| Array with maximum possible integer value repeated multiple times | The algorithm should handle the maximum integer value without overflow, correctly identifying the maximum to the right. |
| Array with minimum possible integer value repeated multiple times | The algorithm should handle the minimum integer value without issue, correctly identifying the maximum to the right. |
| Array containing a mix of positive, negative, and zero values | The algorithm compares all elements regardless of their sign, finding the correct maximum to the right. |
| Array with values already in descending order | The algorithm correctly replaces each element with the next smaller value, culminating in -1 for the last. |
| Large input array (performance) | The solution should iterate through the array efficiently in reverse, so time complexity should be O(n). |
| Input array is null | Return null or throw an IllegalArgumentException as appropriate, according to the problem constraints or API definition. |