Answer: TATA 1MG Hiring Off-Campus OA (2024) | Replace Element with Greatest Element on
Answer · Posted Jun 2026
Approach Traverse the array from right to left while maintaining the maximum element seen so far on the right. For each position: Store the current value. Replace the current element with maxRight. Update maxRight using the stored value. Since each element is processed only once, the solution is highly efficient. Strategy Initialize maxRight = -1. Start from the last index and move backward. Replace each element with the current maxRight. Update maxRight with the maximum value encountered. This avoids using ...
The full answer & interview discussion are available to premium members.
Log in Create a free account