0% completed
Problem Statement
Given an array arr
, you transform it daily based on the following 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 of the array do not change.
Continue transforming the array until no more changes occur. Return the final state of the array.
Examples
Example 1
- Input: arr =
[5, 2, 8, 1, 6]
- Expected Output:
[5, 5, 5, 5, 6]
- Justification:
- On the first day, 2 is incremented to 3, 8 is decremented to 7, and 1 is incremented to 2. The first and last elements remain unchanged. The final state of the array after the first day is [5, 3, 7, 2, 6].
- After second day: [5, 4, 6, 3, 6].
- After third day: [5, 5, 5, 4, 6].
- After forth day: [5, 5, 5, 5, 6].
Example 2
- Input: arr =
[3, 7, 1, 5, 2]
- Expected Output:
[3,3,3,3,2]
- Justification:
- After first day: [3, 6, 2, 4, 2].
- After second day: [3, 5, 3, 3, 2].
- After third day: [3, 4, 3, 3, 2].
- After forth day: [3, 3, 3, 3, 2].
Example 3
- Input: arr =
[9, 9, 9, 9, 9]
- Expected Output:
[9, 9, 9, 9, 9]
- Justification: All elements are the same. So,
arr
remains unchanged.
Constraints:
- 3 <= arr.length <= 100
- 1 <= arr[i] <= 100
Solution
To solve this problem, we simulate the transformation process of the array until it stabilizes. We iterate through the array, adjusting elements based on their neighbors. If an element is smaller than both its neighbors, we increment it. If it is larger, we decrement it. This process continues until no more changes occur, meaning the array has stabilized. This approach is effective as it directly follows the problem constraints and guarantees that the array reaches a stable state.
Step-by-Step Algorithm
-
Initialize Variables:
changed = true
: This flag will be used to determine if any changes were made during an iteration.
-
Loop Until No Changes Occur:
- While
changed
is true:- Set
changed
tofalse
at the start of each iteration. - Initialize
prev
,curr
, andnext
to the first three elements of the array for comparison. Here,prev
will store theleft
neighbor of thecurr
elements, andnext
will store the right neighbor of thecurr
element.
- Set
- While
-
Iterate Through the Array:
- For each element from the second to the second last element:
- If the current element
curr
is less than both its neighbors (prev
andnext
), increment it.- Set
changed
totrue
to indicate a change was made.
- Set
- If the current element
curr
is greater than both its neighbors (prev
andnext
), decrement it.- Set
changed
totrue
to indicate a change was made.
- Set
- If the current element
- For each element from the second to the second last element:
-
Update Elements for Next Iteration:
- Move to the next set of elements:
- Update
prev
to the value ofcurr
. - Update
curr
to the value ofnext
. - Update
next
to the value of the element two positions ahead (arr[i + 2]
), if within bounds.
- Update
- These updates ensure that the comparisons in the next iteration use the original values from the array, maintaining the correct sequence of comparisons.
- Move to the next set of elements:
-
Return the Transformed Array:
- After the loop exits (when no changes occur), return the transformed array
arr
.
- After the loop exits (when no changes occur), return the transformed array
Algorithm Walkthrough
Let's use the input: arr = [5, 2, 8, 1, 6].
-
Initialize Variables:
changed = true
-
First Iteration of While Loop:
-
changed = false
-
Initialize
prev = 5
,curr = 2
,next = 8
-
For Loop (i = 1):
2 < 5
and2 < 8
-> incrementarr[1]
to3
arr = [5, 3, 8, 1, 6]
changed = true
- (i = 1) < (arr.length - 2 = 5 - 2 = 3). So, move to next elements.
- Update
prev = 2
,curr = 8
,next = 1
- Update
-
For Loop (i = 2):
8 > 2
and8 > 1
-> decrementarr[2]
to7
changed = true
arr = [5, 3, 7, 1, 6]
- (i = 2) < (arr.length - 2 = 5 - 2 = 3). So, move to next elements.
- Update
prev = 8
,curr = 1
,next = 6
- Update
-
For Loop (i = 3):
1 < 8
and1 < 6
-> incrementarr[3]
to2
arr = [5, 3, 7, 2, 6]
changed = true
- (i = 3) !< (arr.length - 2 = 3). No further iterations.
-
-
Second Iteration of While Loop:
-
changed = false
-
Initialize
prev = 5
,curr = 3
,next = 7
-
For Loop (i = 1):
3 < 5
and3 < 7
-> incrementarr[1]
to4
arr = [5, 4, 7, 2, 6]
changed = true
- (i = 1) < (arr.length - 2 = 5 - 2 = 3). So, move to next elements.
- Update
prev = 3
,curr = 7
,next = 2
- Update
-
For Loop (i = 2):
7 > 3
and7 > 2
-> decrementarr[2]
to6
arr = [5, 4, 6, 2, 6]
changed = true
- (i = 2) < (arr.length - 2 = 5 - 2 = 3). So, move to next elements.
- Update
prev = 7
,curr = 2
,next = 6
- Update
-
For Loop (i = 3):
2 < 7
and2 < 6
-> incrementarr[3]
to3
arr = [5, 4, 6, 3, 6]
changed = true
- (i = 3) !< (arr.length - 2 = 3). No further iterations.
-
-
Third Iteration of While Loop:
-
changed = false
-
Initialize
prev = 5
,curr = 4
,next = 6
-
For Loop (i = 1):
4 < 5
and4 < 6
-> incrementarr[1]
to5
arr = [5, 5, 6, 3, 6]
changed = true
- (i = 1) < (arr.length - 2 = 5 - 2 = 3). So, move to next elements.
- Update
prev = 4
,curr = 6
,next = 3
- Update
-
For Loop (i = 2):
6 > 4
and6 > 3
-> incrementarr[1]
to5
arr = [5, 5, 5, 3, 6]
changed = true
- (i = 2) < (arr.length - 2 = 5 - 2 = 3). So, move to next elements.
- Update
prev = 6
,curr = 3
,next = 6
- Update
-
For Loop (i = 3):
3 < 6
and3 < 6
-> incrementarr[3]
to4
arr = [5, 5, 5, 4, 6]
changed = true
- (i = 3) !< (arr.length - 2 = 3). No further iterations.
-
-
Fourth Iteration of While Loop:
-
changed = false
-
Initialize
prev = 5
,curr = 5
,next = 5
-
For Loop (i = 1):
- No change (5 is not less than both neighbors or greater than both)
- (i = 1) < (arr.length - 2 = 5 - 2 = 3). So, move to next elements.
- Update
prev = 5
,curr = 5
,next = 4
- Update
-
For Loop (i = 2):
- No change (5 is not less than both neighbors or greater than both)
- (i = 2) < (arr.length - 2 = 5 - 2 = 3). So, move to next elements.
- Update
prev = 5
,curr = 4
,next = 6
- Update
-
For Loop (i = 3):
4 < 5
and4 < 6
-> incrementarr[3]
to5
arr = [5, 5, 5, 5, 6]
changed = true
- (i = 3) !< (arr.length - 2 = 3). No further iterations.
-
-
Fifth Iteration of While Loop:
-
changed = false
-
Initialize
prev = 5
,curr = 5
,next = 5
-
For Loop (i = 1):
- No change (5 is not less than both neighbors or greater than both)
- (i = 1) < (arr.length - 2 = 5 - 2 = 3). So, move to next elements.
- Update
prev = 5
,curr = 5
,next = 5
- Update
-
For Loop (i = 2):
- No change (5 is not less than both neighbors or greater than both)
- (i = 2) < (arr.length - 2 = 5 - 2 = 3). So, move to next elements.
- Update
prev = 5
,curr = 5
,next = 6
- Update
-
For Loop (i = 3):
- No change (5 is not less than both neighbors or greater than both)
- (i = 3) !< (arr.length - 2 = 3). No further iterations.
-
Array After Fifth Iteration:
arr = [5, 5, 5, 5, 6]
-
-
Termination:
- The loop exits as
changed
remainsfalse
, indicating no further changes.
- The loop exits as
-
The final state of the array is [5, 5, 5, 5, 6].
Complexity Analysis
Time Complexity
- The outer loop can run up to 100 times, given the constraint
3 <= arr[i] <= 100
. - The inner loop runs
n-2
times per iteration. - So, the time complexity is: O(n * 100) = O(n).
Space Complexity
The space complexity of this algorithm is O(1), excluding the input array itself. Since the array is being transformed in-place, no extra space proportional to the input size is needed.
Table of Contents
Problem Statement
Examples
Solution
Step-by-Step Algorithm
Algorithm Walkthrough
Complexity Analysis
Time Complexity
Space Complexity