Maximizing the elements with a[i+1] > a[i]
Last Updated :
07 Sep, 2022
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 the 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.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int countMaxPos( int arr[], int n)
{
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});
}
int max_freq = 0;
for ( auto i : map) {
if (max_freq < i.second)
{
max_freq = i.second;
}
}
return n - max_freq;
}
int main()
{
int arr[] = { 20, 30, 10, 50, 40 };
int n = sizeof (arr)/ sizeof (arr[0]);
cout << (countMaxPos(arr, n));
}
|
Java
import java.util.*;
class GFG {
static int countMaxPos( int [] arr)
{
int n = arr.length;
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 );
}
int max_freq = 0 ;
for (Map.Entry entry : map.entrySet())
max_freq = Math.max(max_freq, ( int )entry.getValue());
return n - max_freq;
}
public static void main(String[] args)
{
int [] arr = { 20 , 30 , 10 , 50 , 40 };
System.out.println(countMaxPos(arr));
}
}
|
Python3
def countMaxPos(arr):
n = len (arr)
Map = {}
for x in arr:
if x in Map :
Map [x] + = 1
else :
Map [x] = 1
max_freq = 0
for entry in Map :
max_freq = max (max_freq, Map [entry])
return n - max_freq
if __name__ = = "__main__" :
arr = [ 20 , 30 , 10 , 50 , 40 ]
print (countMaxPos(arr))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int countMaxPos( int [] arr)
{
int n = arr.Length;
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);
}
int max_freq = 0;
foreach (KeyValuePair< int , int > entry in map)
max_freq = Math.Max(max_freq, entry.Value);
return n - max_freq;
}
public static void Main(String[] args)
{
int [] arr = { 20, 30, 10, 50, 40 };
Console.WriteLine(countMaxPos(arr));
}
}
|
Javascript
<script>
function countMaxPos(arr) {
let n = arr.length;
let map = new Map();
for (let x of arr) {
if (map.has(x))
map.set(x, map.get(x) + 1);
else
map.set(x, 1);
}
let max_freq = 0;
for (let entry of map)
max_freq = Math.max(max_freq, entry[1]);
console.log(max_freq, n)
return n - max_freq;
}
let arr = [20, 30, 10, 50, 40];
document.write(countMaxPos(arr));
</script>
|
complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...