# Maximizing the elements with a[i+1] > a[i]

Given an array of N integers, rearrange the array elements such that the next array element is greater than the previous element ( > ).

**Examples:**

Input : arr[] = {20, 30, 10, 50, 40}

Output : 4

We rearrange the array as 10, 20, 30, 40, 50. As 20 > 10, 30 > 20, 40 > 30, 50 > 40, so we get 4 indices i such that > .

Input : arr[] = {200, 100, 100, 200}

Output : 2

We get optimal arrangement as 100 200 100 200.

If all elements are distinct, then answer is simply n-1 where n is the number of elements in the array. If there are repeating elements, then answer is n – max_freq.

## Java

`import` `java.util.*; ` ` ` `class` `GFG { ` ` ` ` ` `// returns the number of positions where A(i + 1) is ` ` ` `// greater than A(i) after rearrangement of the array ` ` ` `static` `int` `countMaxPos(` `int` `[] arr) ` ` ` `{ ` ` ` `int` `n = arr.length; ` ` ` ` ` `// Creating a HashMap containing char ` ` ` `// as a key and occurrences as a value ` ` ` `HashMap<Integer, Integer> map ` ` ` `= ` `new` `HashMap<Integer, Integer>(); ` ` ` `for` `(` `int` `x : arr) { ` ` ` `if` `(map.containsKey(x)) ` ` ` `map.put(x, map.get(x) + ` `1` `); ` ` ` `else` ` ` `map.put(x, ` `1` `); ` ` ` `} ` ` ` ` ` `// Find the maximum frequency ` ` ` `int` `max_freq = ` `0` `; ` ` ` `for` `(Map.Entry entry : map.entrySet()) ` ` ` `max_freq = Math.max(max_freq, (` `int` `)entry.getValue()); ` ` ` ` ` `return` `n - max_freq; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `[] arr = { ` `20` `, ` `30` `, ` `10` `, ` `50` `, ` `40` `}; ` ` ` `System.out.println(countMaxPos(arr)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 implementation of the above approach

# Returns the number of positions where

# A(i + 1) is greater than A(i) after

# rearrangement of the array

def countMaxPos(arr):

n = len(arr)

# Creating a HashMap containing char

# as a key and occurrences as a value

Map = {}

for x in arr:

if x in Map:

Map[x] += 1

else:

Map[x] = 1

# Find the maximum frequency

max_freq = 0

for entry in Map:

max_freq = max(max_freq, Map[entry])

return n – max_freq

# Driver code

if __name__ == “__main__”:

arr = [20, 30, 10, 50, 40]

print(countMaxPos(arr))

# This code is contributed by Rituraj Jain

**Output:**

4

## Recommended Posts:

- Maximizing array sum with given operation
- Maximizing Unique Pairs from two arrays
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
- Average of remaining elements after removing K largest and K smallest elements from array
- Minimum number of elements to be removed such that the sum of the remaining elements is equal to k
- Find elements larger than half of the elements in an array
- Elements to be added so that all elements of a range are present in array
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Count elements such that there are exactly X elements with values greater than or equal to X
- Generate an array of K elements such that sum of elements is N and the condition a[i] < a[i+1] <= 2*a[i] is met | Set 2
- Count array elements that divide the sum of all other elements
- Find all elements in array which have at-least two greater elements
- Maximum distinct elements after removing k elements
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array
- Move matrix elements in given direction and add elements with same value

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.