- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 := 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
Let us see the following implementation to get better understanding −
def solve(arr): arr.sort() arr = 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))
- Program to find number of sublists containing maximum and minimum after deleting only one element in Python
- C++ Program to find length of maximum non-decreasing subsegment
- Program to find maximum binary string after change in python
- Find the maximum element in an array which is first increasing and then decreasing in C++
- Program to find minimum possible maximum value after k operations in python
- Python program to find the Decreasing point in a List
- 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 Maximum and minimum element’s position in a list?
- C# program to find maximum and minimum element in an array
- C++ Program to find array after removal from maximum
- Program to get maximum value of power of a list by rearranging elements in Python
- Program to find maximum profit after buying and selling stocks at most two times in python
- Program to find maximum XOR with an element from array in Python
- Program to find maximum difference of adjacent values after deleting k numbers in python