Back to DSA sheet
Replace Elements With Greatest Element On Right Side
EasyArraysReplace every element of the array with the greatest element among the values to its right, and replace the last element with -1.
Input format (stdin): the first line has n. The second line has n integers. Output the transformed array, space separated.
Examples
Input: 6
17 18 5 4 6 1
Output: 18 6 6 6 1 -1
Each position takes the max to its right; the last is -1.
Input: 1
400
Output: -1
Constraints
- 1 <= n <= 10^4
- 1 <= nums[i] <= 10^5
Sheets
NeetCode 250
replace-elements-with-greatest-element-on-right-side.cpp2 sample tests
Loading editor
Test results
Run the sample tests to check your solution against expected output.
Custom input (stdin)
Output
Run your code to see its output.