Skip to content

Commit fcd11d8

Browse files
committed
1299. Replace Elements with Greatest Element on Right Side. Description.
1 parent bf7c280 commit fcd11d8

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# 1299. Replace Elements with Greatest Element on Right Side
2+
3+
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`.
4+
5+
After doing so, return the array.
6+
7+
**Example 1:**
8+
9+
> **Input:** arr = [17,18,5,4,6,1]
10+
>
11+
> **Output:** [18,6,6,6,1,-1]
12+
>
13+
> **Explanation:**
14+
>
15+
> - index 0 --> the greatest element to the right of index 0 is index 1 (18).
16+
> - index 1 --> the greatest element to the right of index 1 is index 4 (6).
17+
> - index 2 --> the greatest element to the right of index 2 is index 4 (6).
18+
> - index 3 --> the greatest element to the right of index 3 is index 4 (6).
19+
> - index 4 --> the greatest element to the right of index 4 is index 5 (1).
20+
> - index 5 --> there are no elements to the right of index 5, so we put -1.
21+
22+
**Example 2:**
23+
24+
> **Input:** arr = [400]
25+
>
26+
> **Output:** [-1]
27+
>
28+
> **Explanation:** There are no elements to the right of index 0.
29+
30+
## Constraints
31+
32+
- $1 <= arr.length <= 10^4$
33+
- $1 <= arr[i] <= 10^5$
34+
35+
## Topics
36+
37+
- `Array`
38+
39+
## Hints
40+
41+
1. Loop through the array starting from the end.
42+
2. Keep the maximum value seen so far.
43+
44+
## Similar Questions
45+
46+
Hard
47+
48+
- [2454. Next Greater Element IV](2454_next_greater_element_4.md)
49+
50+
Easy
51+
52+
- [2078. Two Furthest Houses With Different Colors](2078_two_furthest_houses_with_different_colors.md)

0 commit comments

Comments
 (0)