Replace Each Value by Its Original Suffix Maximum

Quick Overview

Replace Each Value by Its Original Suffix Maximum. Replace arr[i] by the largest original value strictly to its right.

Replace Each Value by Its Original Suffix Maximum

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Replace arr[i] by the largest original value strictly to its right. The final element becomes -1 because it has no right neighbor. Modify the array and return it. ```cpp class Solution { public: vector<int> replaceElements(vector<int>& arr); }; ``` ### Examples **Example 1:** ```text Input: arr = [3,9,2] Output: [9,2,-1] Explanation: The suffixes to the right have maxima9 and2, followed by the empty suffix. ``` **Example 2:** ```text Input: arr = [5,5] Output: [5,-1] Explanation: Equal values are allowed; the first position sees the second5. ``` ### Constraints - There are 1 to 10000 entries. - Every original entry is an integer in `[1,100000]`. ### Contract considerations The native vector reference is modified and returned; all comparisons refer to original values. ```hint Read the boundary condition Check the final position and repeated values. Each replacement must refer to the original array. ``` ### Boundary reminders The native vector reference is modified and returned; all comparisons refer to original values.

Overview: Replace Each Value by Its Original Suffix Maximum. Replace arr[i] by the largest original value strictly to its right.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Replace arr[i] by the largest original value strictly to its right. The final element becomes -1 because it has no right neighbor. Modify the array and return it.

class Solution {
public:
    vector<int> replaceElements(vector<int>& arr);
};

Examples

Example 1:

Input: arr = [3,9,2]
Output: [9,2,-1]
Explanation: The suffixes to the right have maxima9 and2, followed by the empty suffix.

Example 2:

Input: arr = [5,5]
Output: [5,-1]
Explanation: Equal values are allowed; the first position sees the second5.

Constraints

  • There are 1 to 10000 entries.
  • Every original entry is an integer in [1,100000] .

Contract considerations

The native vector reference is modified and returned; all comparisons refer to original values.

Boundary reminders

The native vector reference is modified and returned; all comparisons refer to original values.