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
πŸ”οΈ Mountain Range TransformationDay 0: Initial Terrain [6,2,3,4]6234Valley at position 2 needs fillingDay 1: After Transformation [6,3,3,4]6334βœ“ Stable equilibrium reached!🎯 Final Result: Balanced Landscape[6, 3, 3, 4] - No more changes occur
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)

n
2n
βœ“ Linear Growth
Space Complexity
O(n)

Need extra space to store the new array for each iteration

n
2n
⚑ 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
Asked in
Google 12 Amazon 8 Microsoft 6 Apple 4
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen