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.

## C++

`#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// returns the number of positions where A(i + 1) is ` `// greater than A(i) after rearrangement of the array ` `int` `countMaxPos(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `// Creating a HashMap containing char ` ` ` `// as a key and occurrences as a value ` ` ` `unordered_map<` `int` `, ` `int` `> map; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++ ) { ` ` ` `if` `(map.count(arr[i])) ` ` ` `map.insert({arr[i], (map.count(arr[i]) + 1)}); ` ` ` `else` ` ` `map.insert({arr[i], 1}); ` ` ` `} ` ` ` ` ` `// Find the maximum frequency ` ` ` `int` `max_freq = 0; ` ` ` ` ` `for` `(` `auto` `i : map) { ` ` ` `if` `(max_freq < i.second) ` ` ` `{ ` ` ` `max_freq = i.second; ` ` ` `} ` ` ` `} ` ` ` `return` `n - max_freq; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 20, 30, 10, 50, 40 }; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` `cout << (countMaxPos(arr, n)); ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `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 ` ` ` `Dictionary<` `int` `, ` ` ` `int` `> map = ` `new` `Dictionary<` `int` `, ` ` ` `int` `>(); ` ` ` `foreach` `(` `int` `x ` `in` `arr) ` ` ` `{ ` ` ` `if` `(map.ContainsKey(x)) ` ` ` `map[x] = map[x] + 1; ` ` ` `else` ` ` `map.Add(x, 1); ` ` ` `} ` ` ` ` ` `// Find the maximum frequency ` ` ` `int` `max_freq = 0; ` ` ` `foreach` `(KeyValuePair<` `int` `, ` `int` `> entry ` `in` `map) ` ` ` `max_freq = Math.Max(max_freq, entry.Value); ` ` ` ` ` `return` `n - max_freq; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `int` `[] arr = { 20, 30, 10, 50, 40 }; ` ` ` `Console.WriteLine(countMaxPos(arr)); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximizing Unique Pairs from two arrays
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Sum of all elements between k1'th and k2'th smallest elements
- Find elements larger than half of the elements in an array
- Find all elements in array which have at-least two greater elements
- Elements to be added so that all elements of a range are present in array
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array
- Count elements such that there are exactly X elements with values greater than or equal to X
- Average of remaining elements after removing K largest and K smallest elements from array
- Minimum elements to be removed such that sum of adjacent elements is always odd
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- 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
- Find Kth element in an array containing odd elements first and then even elements
- Value to be subtracted from array elements to make sum of all elements equals K
- Choose X elements from A[] and Y elements from B[] which satisfy the given condition
- Rearrange array elements such that Bitwise AND of first N - 1 elements is equal to last element
- Minimize the sum of differences of consecutive elements after removing exactly K elements
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Split an array containing N elements into K sets of distinct elements
- Count of elements not divisible by any other elements of Array

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.