Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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] <= 105Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[17,18,5,4,6,1]
[400]
two-furthest-houses-with-different-colors)next-greater-element-iv)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1299: Replace Elements with Greatest Element on Right Side
class Solution {
public int[] replaceElements(int[] arr) {
for (int i = arr.length - 1, mx = -1; i >= 0; --i) {
int x = arr[i];
arr[i] = mx;
mx = Math.max(mx, x);
}
return arr;
}
}
// Accepted solution for LeetCode #1299: Replace Elements with Greatest Element on Right Side
func replaceElements(arr []int) []int {
for i, mx := len(arr)-1, -1; i >= 0; i-- {
x := arr[i]
arr[i] = mx
mx = max(mx, x)
}
return arr
}
# Accepted solution for LeetCode #1299: Replace Elements with Greatest Element on Right Side
class Solution:
def replaceElements(self, arr: List[int]) -> List[int]:
mx = -1
for i in reversed(range(len(arr))):
x = arr[i]
arr[i] = mx
mx = max(mx, x)
return arr
// Accepted solution for LeetCode #1299: Replace Elements with Greatest Element on Right Side
use std::cmp::max;
impl Solution {
pub fn replace_elements(arr: Vec<i32>) -> Vec<i32> {
let length = arr.len();
let mut ans: Vec<i32> = vec![-1; length];
for i in (1..=(length - 1)).rev() {
ans[i - 1] = max(arr[i], ans[i]);
}
ans
}
}
// Accepted solution for LeetCode #1299: Replace Elements with Greatest Element on Right Side
function replaceElements(arr: number[]): number[] {
for (let i = arr.length - 1, mx = -1; ~i; --i) {
const x = arr[i];
arr[i] = mx;
mx = Math.max(mx, x);
}
return arr;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.