Array Transformation
Real-world version: Settling the Gauge RowThe statement here is our own: the same inputs, outputs and technique.
A row of gauges reads arr. Every round, all the gauges are updated at once from the readings the row held at the start of that round:
- a gauge with a neighbour on each side rises by one when both neighbours read higher than it, and falls by one when both read lower;
- otherwise it keeps its reading, and the gauges at the two ends never change.
Rounds continue until a round changes nothing. Return the row as it then stands.
Examples
Example 1
- Input
- arr = [1, 3, 2, 3]
- Output
- [1, 2, 3, 3]
In the first round the gauge reading 3 at position 1 has 1 and 2 either side, both lower, so it falls to 2, while the gauge reading 2 has 3 and 3 either side, both higher, so it rises to 3. The next round changes nothing.
Example 2
- Input
- arr = [5, 1, 5]
- Output
- [5, 5, 5]
The middle gauge rises by one each round while both neighbours stay higher, and it stops once it reaches them.
Example 3
- Input
- arr = [1, 2, 3]
- Output
- [1, 2, 3]
The middle gauge has one neighbour above and one below, so it never moves, and the two ends never move either.
Constraints
- 3 <= arr.length <= 100
- 1 <= arr[i] <= 100
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def settle_gauges(arr: list[int]) -> list[int]:public List<Integer> settleGauges(int[] arr)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 13 test cases: 13 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Concatenation of ArrayReal-world version: Two Cycle PlayoutEasy
- Reshape the MatrixReal-world version: Relaying the Contact SheetEasy
- Separate the Digits in an ArrayReal-world version: Digit Cells for the Ticket CodesEasy
- Distribute Elements Into Two Arrays IReal-world version: Splitting Parcels Onto Two Intake TraysEasy