Wednesday, January 29, 2025
Replace Elements with Greatest Element on Right Side

Updated: January 2025
Replace Elements with Greatest Element on Right Side
easy
💡 Intuition
- When starting from the left, each index i has (n - i - 1) elements to its right, from which we need to determine the maximum. This approach has a time complexity of O(n ^2 ).
- When starting from the right, we can maintain a running maximum, and at each step, we only need to compare two values: num[i+1] and the maximum encountered so far. This approach has a time complexity of O(n).
🚀 Solution
go
⏳ Time Complexity
- Since we are taking single loop for the array of length n, the time complexity will be
O(n)