Find maximum element among the elements with minimum frequency in given Array
Given an array arr[] consisting of N integers, the task is to find the maximum element with the minimum frequency.
Examples:
Input: arr[] = {2, 2, 5, 50, 1}
Output: 50
Explanation:
The element with minimum frequency is {1, 5, 50}. The maximum element among these element is 50.
Input: arr[] = {3, 2, 5, 6, 1}
Output: 6
Approach: Sort the array in non-decreasing order, which brings elements with the same value together. This allows us to easily count and identify when the frequency of an element changes. When the frequency changes (‘currFreq’ <= ‘minFreq’), we update the minimum frequency and as the array is sorted, the current element (arr[i-1]) becomes the maximum element, representing the highest value within its frequency group.
Follow the steps below to solve the given problem:
- Sort the input array in non-decreasing order.
- Initialize minFreq to array size+1, maxElement to the first element of the sorted array, and currFreq to 1.
- Iterate over the sorted array from the second element to the last element.
- If the current element is the same as the previous element, increment currFreq.
- Otherwise, If currFreq is less than or equal to minFreq, update minFreq with currFreq and update maxElement with the previous element.
- Reset currFreq to 1 for the new element.
- After the loop, check the frequency of the last element and update minFreq and maxElement if necessary.
- Return the maxElement, which represents the maximum element with the minimum frequency in the input array.
C++14
#include <bits/stdc++.h>
using namespace std;
int maxElementWithMinFreq( int *arr, int N)
{
sort(arr, arr + N);
int minFreq = N + 1;
int maxElement = arr[0];
int currFreq = 1;
for ( int i = 1; i < N; i++)
{
if (arr[i] == arr[i - 1])
{
currFreq++;
}
else
{
if (currFreq <= minFreq)
{
minFreq = currFreq;
maxElement = arr[i - 1];
}
currFreq = 1;
}
}
if (currFreq <= minFreq)
{
minFreq = currFreq;
maxElement = arr[N - 1];
}
return maxElement;
}
int main()
{
int arr[] = {2, 2, 5, 50, 1};
int N = sizeof (arr) / sizeof (arr[0]);
cout << maxElementWithMinFreq(arr, N);
return 0;
}
|
Java
import java.util.Arrays;
public class gfg {
public static int maxElementWithMinFreq( int [] arr, int N) {
Arrays.sort(arr);
int minFreq = N + 1 ;
int maxElement = arr[ 0 ];
int currFreq = 1 ;
for ( int i = 1 ; i < N; i++) {
if (arr[i] == arr[i - 1 ]) {
currFreq++;
} else {
if (currFreq <= minFreq) {
minFreq = currFreq;
maxElement = arr[i - 1 ];
}
currFreq = 1 ;
}
}
if (currFreq <= minFreq) {
minFreq = currFreq;
maxElement = arr[N - 1 ];
}
return maxElement;
}
public static void main(String[] args) {
int [] arr = { 2 , 2 , 5 , 50 , 1 };
int N = arr.length;
System.out.println(maxElementWithMinFreq(arr, N));
}
}
|
Python3
def max_element_with_min_freq(arr):
arr.sort()
min_freq = len (arr) + 1
max_element = arr[ 0 ]
curr_freq = 1
for i in range ( 1 , len (arr)):
if arr[i] = = arr[i - 1 ]:
curr_freq + = 1
else :
if curr_freq < = min_freq:
min_freq = curr_freq
max_element = arr[i - 1 ]
curr_freq = 1
if curr_freq < = min_freq:
min_freq = curr_freq
max_element = arr[ - 1 ]
return max_element
arr = [ 2 , 2 , 5 , 50 , 1 ]
print (max_element_with_min_freq(arr))
|
C#
using System;
class GFG
{
static int MaxElementWithMinFreq( int [] arr, int N)
{
Array.Sort(arr);
int minFreq = N + 1;
int maxElement = arr[0];
int currFreq = 1;
for ( int i = 1; i < N; i++)
{
if (arr[i] == arr[i - 1])
{
currFreq++;
}
else
{
if (currFreq <= minFreq)
{
minFreq = currFreq;
maxElement = arr[i - 1];
}
currFreq = 1;
}
}
if (currFreq <= minFreq)
{
minFreq = currFreq;
maxElement = arr[N - 1];
}
return maxElement;
}
static void Main()
{
int [] arr = { 2, 2, 5, 50, 1 };
int N = arr.Length;
Console.WriteLine(MaxElementWithMinFreq(arr, N));
}
}
|
Javascript
function maxElementWithMinFreq(arr) {
arr.sort();
let minFreq = arr.length + 1;
let maxElement = arr[0];
let currFreq = 1;
for (let i = 1; i < arr.length; i++) {
if (arr[i] == arr[i - 1]) {
currFreq++;
} else {
if (currFreq <= minFreq) {
minFreq = currFreq;
maxElement = arr[i - 1];
}
currFreq = 1;
}
}
if (currFreq <= minFreq) {
minFreq = currFreq;
maxElement = arr[arr.length - 1];
}
return maxElement;
}
const arr = [2, 2, 5, 50, 1];
console.log(maxElementWithMinFreq(arr));
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Approach: The given problem can be solved by storing the frequency of the array element in a HashMap and then finding the maximum value having a minimum frequency. Follow the steps below to solve the given problem:
- Store the frequency of each element in a HashMap, say M.
- Initialize two variables, say maxValue as INT_MIN and minFreq as INT_MAX that store the resultant maximum element and stores the minimum frequency among all the frequencies.
- Iterate over the map M and perform the following steps:
- If the frequency of the current element is less than minFreq then update the value of minFreq to the current frequency and the value of maxValue to the current element.
- If the frequency of the current element is equal to the minFreq and the value of maxValue is less than the current value then update the value of maxValue to the current element.
- After completing the above steps, print the value of maxValue as the resultant element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxElementWithMinFreq( int * arr, int N)
{
unordered_map< int , int > mp;
for ( int i = 0; i < N; i++) {
mp[arr[i]]++;
}
int minFreq = INT_MAX;
int maxValue = INT_MIN;
for ( auto x : mp) {
int num = x.first;
int freq = x.second;
if (freq < minFreq) {
minFreq = freq;
maxValue = num;
}
else if (freq == minFreq
&& maxValue < num) {
maxValue = num;
}
}
return maxValue;
}
int main()
{
int arr[] = { 2, 2, 5, 50, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << maxElementWithMinFreq(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int maxElementWithMinFreq( int [] arr, int N)
{
HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>();
for ( int i = 0 ; i < N; i++) {
if (mp.containsKey(arr[i])){
mp.put(arr[i], mp.get(arr[i])+ 1 );
} else {
mp.put(arr[i], 1 );
}
}
int minFreq = Integer.MAX_VALUE;
int maxValue = Integer.MIN_VALUE;
for (Map.Entry<Integer,Integer> x : mp.entrySet()){
int num = x.getKey();
int freq = x.getValue();
if (freq < minFreq) {
minFreq = freq;
maxValue = num;
}
else if (freq == minFreq
&& maxValue < num) {
maxValue = num;
}
}
return maxValue;
}
public static void main(String[] args)
{
int arr[] = { 2 , 2 , 5 , 50 , 1 };
int N = arr.length;
System.out.print(maxElementWithMinFreq(arr, N));
}
}
|
Python3
import sys
from collections import defaultdict
def maxElementWithMinFreq(arr, N):
mp = defaultdict( int )
for i in range (N):
mp[arr[i]] + = 1
minFreq = sys.maxsize
maxValue = - sys.maxsize - 1
for x in mp:
num = x
freq = mp[x]
if (freq < minFreq):
minFreq = freq
maxValue = num
elif (freq = = minFreq
and maxValue < num):
maxValue = num
return maxValue
if __name__ = = "__main__" :
arr = [ 2 , 2 , 5 , 50 , 1 ]
N = len (arr)
print (maxElementWithMinFreq(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int maxElementWithMinFreq( int []arr, int N)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
for ( int i = 0; i < N; i++) {
if (mp.ContainsKey(arr[i]))
mp[arr[i]]++;
else
mp.Add(arr[i],1);
}
int minFreq = Int32.MaxValue;
int maxValue = Int32.MinValue;
foreach (KeyValuePair< int , int > x in mp) {
int num = x.Key;
int freq = x.Value;
if (freq < minFreq) {
minFreq = freq;
maxValue = num;
}
else if (freq == minFreq
&& maxValue < num) {
maxValue = num;
}
}
return maxValue;
}
public static void Main()
{
int []arr = { 2, 2, 5, 50, 1 };
int N = arr.Length;
Console.Write(maxElementWithMinFreq(arr, N));
}
}
|
Javascript
<script>
function maxElementWithMinFreq(arr, N) {
let mp = new Map();
for (let i = 0; i < N; i++) {
if (mp.has(arr[i])) {
mp.set(arr[i], mp.get(arr[i]) + 1);
}
else {
mp.set(arr[i], 1);
}
}
let minFreq = Number.MAX_VALUE
let maxValue = Number.MIN_VALUE;
for (let [key, value] of mp) {
let num = key;
let freq = value;
if (freq < minFreq) {
minFreq = freq;
maxValue = num;
}
else if (freq == minFreq
&& maxValue < num) {
maxValue = num;
}
}
return maxValue;
}
let arr = [2, 2, 5, 50, 1];
let N = arr.length;
document.write(maxElementWithMinFreq(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
23 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...