Maximum Element After Decreasing and Rearranging (LeetCode 1846): Solution and Explanation

Medium
Array
Greedy
Sorting

You may reorder an array of positive integers and lower any value. The result must start at 1 and neighbours may differ by at most 1. Return the largest maximum you can end up with.

Intuition

Picture the final array as a staircase that starts on step 1 and can climb at most one step at a time. The tallest point is the answer, so we want the staircase to climb as often as possible.

Values can only be lowered, never raised. A large value placed early is wasted, because it would have to be cut down to fit the slow climb. So put the small values first: sort the array.

After sorting, walk left to right. The first value must become 1. Every later value can be at most one more than the value before it, so cut it to min(arr[i], arr[i - 1] + 1). If it is already small enough, keep it; the staircase then stays flat or climbs by one.

The last value of this staircase is the largest value we can reach, because each step climbed as high as the rules allowed.

Approach: Sort and clip

  1. Sort arr in ascending order.
  2. Set arr[0] = 1.
  3. For i from 1 to n - 1, set arr[i] = min(arr[i], arr[i - 1] + 1).
  4. Return arr[n - 1].

Step-by-step example

arr = [2,2,1,2,1]
  1. Sort: [1,1,2,2,2].
  2. arr[0] is already 1.
  3. i = 1: min(1, 2) = 1. The staircase stays at 1.
  4. i = 2: min(2, 2) = 2. It climbs to 2.
  5. i = 3: min(2, 3) = 2. i = 4: min(2, 3) = 2.
  6. The last value is 2, so the answer is 2.

Code: Python, Java, C++

1class Solution:
2    def maximumElementAfterDecrementingAndRearranging(self, arr: List[int]) -> int:
3        arr.sort()
4        arr[0] = 1
5        for i in range(1, len(arr)):
6            arr[i] = min(arr[i], arr[i - 1] + 1)
7        return arr[-1]
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Counting without sorting O(n)
Sort and clip O(n log n)
Time

O(n log n)

Sorting dominates. The clipping pass is a single O(n) loop.

Space

O(1)

The work happens in place, apart from whatever the sort itself uses.

Other approaches

Counting without sorting (time O(n), space O(n))

The answer is never larger than n, so clamp each value to n and count how many values land on each number 1..n. Walk the counts upward, raising the running maximum by one for each value that allows it. This avoids the comparison sort.

Common mistakes

  • Forgetting to set the first element to 1. The smallest value may be larger than 1, and every later step depends on it.
  • Trying to raise a value. A value smaller than arr[i - 1] + 1 stays as it is; it cannot be increased.
  • Returning the maximum of the original array instead of the last element after clipping.
  • Writing arr[i - 1] + 1 without the min, which would invent values that were never there.

Frequently asked questions

Why does sorting first give the best answer?

Values can only shrink. If a larger value came before a smaller one, swapping them never lowers what either position can hold, so ascending order is at least as good as any other order.

Can the answer ever exceed n?

No. The array starts at 1 and climbs at most 1 per element, so the last element is at most n.

Does the final array have to be sorted?

No, the problem only checks the first element and adjacent differences. Sorting is just the order that lets us reach the highest maximum.

What is the complexity?

O(n log n) time for the sort and O(1) extra space. A counting approach brings it to O(n) time and O(n) space.