> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximum Element After Decreasing and

> Tested Python solution for LeetCode 1846 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 1846, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Greedy](/catalog/topics/greedy), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1846   # by problem number
lcpy gen -s maximum_element_after_decreasing_and_rearranging   # by problem name
```

## Problem

You are given an array of positive integers `arr`. Perform some operations (possibly none) on `arr` so that it satisfies these conditions:

* The value of the first element in `arr` must be 1.
* The absolute difference between any 2 adjacent elements must be less than or equal to 1. In other words, `abs(arr[i] - arr[i - 1]) <= 1` for each `i` where `1 <= i < arr.length` (0-indexed). `abs(x)` is the absolute value of `x`.

There are 2 types of operations that you can perform any number of times:

* Decrease the value of any element of `arr` to a smaller positive integer.
* Rearrange the elements of `arr` to be in any order.

Return *the **maximum** possible value of an element in* `arr` *after performing the operations to satisfy the conditions*.

### Examples

```
Input: arr = [2,2,1,2,1]
Output: 2
```

**Explanation:** We can satisfy the conditions by rearranging arr so it becomes \[1,2,2,2,1]. The largest element in arr is 2.

```
Input: arr = [100,1,1000]
Output: 3
```

**Explanation:** Rearrange arr so it becomes \[1,100,1000]. Decrease the second element to 2 and the third element to 3. Now arr = \[1,2,3], which satisfies the conditions. The largest element in arr is 3.

```
Input: arr = [1,2,3,4,5]
Output: 5
```

**Explanation:** The array already satisfies the conditions, and the largest element is 5.

### Constraints

* 1 \<= arr.length \<= 10^5
* 1 \<= arr\[i] \<= 10^9

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_element_after_decreasing_and_rearranging/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_element_after_decreasing_and_rearranging/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n)
    # Space: O(1) extra (sorting in place)
    def maximum_element(self, arr: list[int]) -> int:
        arr.sort()
        prev = 0
        for value in arr:
            prev = min(prev + 1, value)
        return prev
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) | O(1) extra (sorting in place) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.