- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Program to find maximum element after decreasing and rearranging in Python

Suppose we have an array called arr. We have to perform some operations on arr so that it satisfies these conditions −

The first element in arr must be 1.

The absolute difference between any 2 adjacent elements must be at most 1.

And there are two operations. We can perform these two types of operations any number of times −

Decrease any value of arr to a smaller positive number.

Rearrange the elements of arr to be in any order.

We have to find the maximum possible value in arr after performing the operations to satisfy the given conditions.

So, if the input is like arr = [3,3,2,3,2], then the output will be 3 because, we can decrease the last element to 1, then rearrange them like [1,2,3,3,3], and maximum is 3.

To solve this, we will follow these steps −

sort the list arr

arr[0] := 1

for i in range 1 to size of arr - 1, do

arr[i] := minimum of (arr[i - 1] + 1) and arr[i]

return maximum of arr

## Example

Let us see the following implementation to get better understanding −

def solve(arr): arr.sort() arr[0] = 1 for i in range(1, len(arr)): arr[i] = min(arr[i - 1] + 1, arr[i]) return max(arr) arr = [3,3,2,3,2] print(solve(arr))

## Input

[3,3,2,3,2]

## Output

3

- Related Articles
- Program to find number of sublists containing maximum and minimum after deleting only one element in Python
- Program to find maximum binary string after change in python
- C++ Program to find length of maximum non-decreasing subsegment
- Program to get maximum value of power of a list by rearranging elements in Python
- Program to find minimum possible maximum value after k operations in python
- Program to find maximum profit we can make after k Buy and Sell in python
- Program to find maximum profit after cutting rods and selling same length rods in Python
- Python program to find the Decreasing point in a List
- Find the maximum element in an array which is first increasing and then decreasing in C++\n
- Program to find maximum XOR with an element from array in Python
- Program to find maximum profit after buying and selling stocks at most two times in python
- Program to find maximum difference of adjacent values after deleting k numbers in python
- C++ Program to find array after removal from maximum
- Program to find out the sum of the maximum subarray after a operation in Python
- C# program to find maximum and minimum element in an array\n