Array Transformation - Problem
Imagine you have an array that evolves day by day following specific transformation rules. Given an initial array, each day produces a new array based on the previous day's configuration.
Transformation Rules:
- If an element is smaller than both its left and right neighbors, increment it by 1
- If an element is larger than both its left and right neighbors, decrement it by 1
- The first and last elements never change (they act as fixed boundaries)
The array will eventually reach a stable state where no more changes occur. Your task is to simulate this process and return the final stabilized array.
Example: [6,2,3,4] β [6,3,3,4] β [6,3,3,4] (stable)
Input & Output
example_1.py β Basic Transformation
$
Input:
[6,2,3,4]
βΊ
Output:
[6,3,3,4]
π‘ Note:
Element 2 is smaller than both neighbors (6 and 3), so it gets incremented to 3. After this change, no element satisfies the transformation conditions, so the array stabilizes.
example_2.py β Multiple Iterations
$
Input:
[1,6,3,4,1]
βΊ
Output:
[1,4,4,4,1]
π‘ Note:
Day 1: 6>3 and 6>4, so 6β5, 3<6 and 3<4, so 3β4. Array: [1,5,4,4,1]. Day 2: 5>1 and 5>4, so 5β4. Final: [1,4,4,4,1]
example_3.py β Already Stable
$
Input:
[2,1,2,1,2]
βΊ
Output:
[2,1,2,1,2]
π‘ Note:
No element is strictly smaller or larger than both neighbors. Element 1 has neighbors (2,2) but 1<2 and 1<2, so it would increment to 2, making [2,2,2,2,2], then [2,2,2,2,2] is stable.
Visualization
Tap to expand
Understanding the Visualization
1
Initial Landscape
Start with mountain range having peaks and valleys
2
Day 1 Changes
Deep valleys collect sediment, tall peaks lose material to erosion
3
Gradual Smoothing
Landscape gradually smooths out as extreme elevations balance
4
Equilibrium Reached
Final stable terrain where no more erosion or filling occurs
Key Takeaway
π― Key Insight: Like natural erosion, array transformation always converges because local extremes naturally balance toward their neighbors until equilibrium is reached.
Time & Space Complexity
Time Complexity
O(kΓn)
Where k is the number of days until convergence and n is array length. In worst case, k could be O(max_value), making it O(max_valueΓn)
β Linear Growth
Space Complexity
O(n)
Need extra space to store the new array for each iteration
β‘ Linearithmic Space
Constraints
- 1 β€ arr.length β€ 100
- 1 β€ arr[i] β€ 100
- The array will always converge to a stable state
- First and last elements never change
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code