Array Transformation - Problem
Given an initial array arr, every day you produce a new array using the array of the previous day.
On the i-th day, you do the following operations on the array of day i-1 to produce the array of day i:
- If an element is smaller than both its left neighbor and its right neighbor, then this element is incremented.
- If an element is bigger than both its left neighbor and its right neighbor, then this element is decremented.
- The first and last elements never change.
After some days, the array does not change. Return that final array.
Input & Output
Example 1 — Basic Transformation
$
Input:
arr = [6,2,3,4]
›
Output:
[6,4,4,4]
💡 Note:
Day 1: 2<6 and 2<3, so 2→3. Array becomes [6,3,3,4]. Day 2: 3<6 and 3<3 (false), but middle 3<4, so 3→4. Continue until stable at [6,4,4,4].
Example 2 — Peak Reduction
$
Input:
arr = [1,6,3,4,1]
›
Output:
[1,4,4,4,1]
💡 Note:
6>1 and 6>3, so 6→5. Then 5>4 and 5>4, so 5→4. Process continues until all peaks are flattened to [1,4,4,4,1].
Example 3 — Already Stable
$
Input:
arr = [2,1,1,1]
›
Output:
[2,1,1,1]
💡 Note:
Middle elements 1 are not smaller than both neighbors (1≮1), so no changes occur. Array is already stable.
Constraints
- 1 ≤ arr.length ≤ 100
- 1 ≤ arr[i] ≤ 100
Visualization
Tap to expand
Understanding the Visualization
1
Initial State
Array with peaks and valleys: [6,2,3,4]
2
Transform
Apply increment/decrement rules to middle elements
3
Final State
Stable equilibrium reached: [6,4,4,4]
Key Takeaway
🎯 Key Insight: Elements naturally converge to equilibrium - valleys fill up, peaks flatten down
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code