Maximum element in an array which is equal to its frequency
Given an array of integers arr[] of size N, the task is to find the maximum element in the array whose frequency equals to it’s value
Examples:
Input: arr[] = {3, 2, 2, 3, 4, 3}
Output: 3
Frequency of element 2 is 2
Frequency of element 3 is 3
Frequency of element 4 is 1
2 and 3 are elements which have same frequency as it’s value and 3 is the maximum.
Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: 1
Approach: Store the frequency of every element of the array using the map, and finally find out the maximum of those element whose frequency is equal to their value.
Algorithm:
Step 1: Start
Step 2: Create a static function with an int return type name “find_maxm” which takes an array and an integer value as input parameter.
a. Create a map “mpp” of integer type to store the frequency of each element in the array arr.
b. start a for loop and traverse through i = 0 to n-1
c. For each element in the array, increment the frequency count in the map “mpp”.
Step 3: Create an int variable “ans” and initialize it with 0.
Step 4: Traverse the map “mpp” using a for-each loop.
a. Set x.first to value and x.second to freq for each key-value pair x in the map.
b. Verify that the value and frequency are equivalent.
c. If the answer is yes, see if it exceeds the current value of ans. Update ans with the value if the answer is yes.
d. Give the ans value back.
Step 5: End
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
int find_maxm( int arr[], int n)
{
map< int , int > mpp;
for ( int i = 0; i < n; i++) {
mpp[arr[i]] += 1;
}
int ans = 0;
for ( auto x : mpp)
{
int value = x.first;
int freq = x.second;
if (value == freq) {
ans = max(ans, value);
}
}
return ans;
}
int main()
{
int arr[] = { 3, 2, 2, 3, 4, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << find_maxm(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int find_maxm( 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 ans = 0 ;
for (Map.Entry<Integer,Integer> x : mp.entrySet())
{
int value = x.getKey();
int freq = x.getValue();
if (value == freq) {
ans = Math.max(ans, value);
}
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 3 , 2 , 2 , 3 , 4 , 3 };
int n = arr.length;
System.out.print(find_maxm(arr, n));
}
}
|
Python3
def find_maxm(arr, n) :
mpp = {}
for i in range ( 0 ,n):
if (arr[i] in mpp):
mpp.update( {arr[i] : mpp[arr[i]] + 1 } )
else :
mpp[arr[i]] = 1
ans = 0
for value,freq in mpp.items():
if (value = = freq):
ans = max (ans, value)
return ans
arr = [ 3 , 2 , 2 , 3 , 4 , 3 ]
n = len (arr)
print (find_maxm(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int find_maxm( 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]] = mp[arr[i]]+1;
} else {
mp.Add(arr[i], 1);
}
}
int ans = 0;
foreach (KeyValuePair< int , int > x in mp)
{
int value = x.Key;
int freq = x.Value;
if (value == freq) {
ans = Math.Max(ans, value);
}
}
return ans;
}
public static void Main(String[] args)
{
int []arr = { 3, 2, 2, 3, 4, 3 };
int n = arr.Length;
Console.Write(find_maxm(arr, n));
}
}
|
Javascript
<script>
function find_maxm(arr, n)
{
var mpp = new Map();
for ( var i = 0; i < n; i++)
{
if (mpp.has(arr[i]))
mpp.set(arr[i], mpp.get(arr[i])+1)
else
mpp.set(arr[i], 1)
}
var ans = 0;
mpp.forEach((value, key) => {
var value = value;
var freq = key;
if (value == freq) {
ans = Math.max(ans, value);
}
});
return ans;
}
var arr = [3, 2, 2, 3, 4, 3 ];
var n = arr.length;
document.write( find_maxm(arr, n));
</script>
|
Time Complexity : O(N)
Space Complexity : O(N) for Hash Map
where N is length of array
Approach : Using Binary Search
The idea of using Binary Search is that we can use the indices values of elements after sorting the array.
If the difference of indices is equal to its value then we can say that the no of appearances of the element is equal to its value.
Below is the implementation of the above idea.
C++
#include <iostream>
#include <algorithm>
using namespace std;
int binarySearch( int arr[], int target, int left, int right) {
int index = -1;
while (left <= right) {
int middle = left + (right - left) / 2;
if (arr[middle] == target) {
index = middle;
left = middle + 1;
} else {
right = middle - 1;
}
}
return index;
}
int find_max( int arr[], int n) {
sort(arr, arr + n);
int max_num = -1;
int i = 0;
while (i < n) {
int current = arr[i];
int j = binarySearch(arr, current, i, n - 1);
if ((j - i) + 1 == current) {
max_num = current;
}
i = j + 1;
}
return max_num;
}
int main() {
int arr[] = { 3, 2, 2, 3, 4, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << find_max(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int find_max( int [] arr, int n) {
Arrays.sort(arr);
int max_num = - 1 ;
int i = 0 ;
while (i < arr.length){
int current = arr[i];
int j = binarySearch(arr, current, i, arr.length - 1 );
if ((j - i) + 1 == current){
max_num = current;
}
i = j + 1 ;
}
return max_num;
}
public static int binarySearch( int [] arr, int target, int left, int right){
int index = - 1 ;
while (left <= right){
int middle = left + (right - left) / 2 ;
if (arr[middle] == target){
index = middle;
left = middle + 1 ;
} else {
right = middle - 1 ;
}
}
return index;
}
public static void main(String[] args)
{
int arr[] = { 3 , 2 , 2 , 3 , 4 , 3 };
int n = arr.length;
System.out.print(find_max(arr, n));
}
}
|
Python3
def binarySearch(arr, target, left, right):
index = - 1
while left < = right:
middle = left + (right - left) / / 2
if arr[middle] = = target:
index = middle
left = middle + 1
else :
right = middle - 1
return index
def find_max(arr, n):
arr.sort()
max_num = - 1
i = 0
while i < n:
current = arr[i]
j = binarySearch(arr, current, i, n - 1 )
if (j - i) + 1 = = current:
max_num = current
i = j + 1
return max_num
if __name__ = = "__main__" :
arr = [ 3 , 2 , 2 , 3 , 4 , 3 ]
n = len (arr)
print (find_max(arr, n))
|
C#
using System;
class Program
{
static void Main( string [] args)
{
int [] arr = { 3, 2, 2, 3, 4, 3 };
Console.WriteLine(FindMax(arr));
}
static int FindMax( int [] arr)
{
Array.Sort(arr);
int maxNum = -1;
int i = 0;
while (i < arr.Length)
{
int current = arr[i];
int j = BinarySearch(arr, current, i, arr.Length - 1);
if ((j - i) + 1 == current)
{
maxNum = current;
}
i = j + 1;
}
return maxNum;
}
static int BinarySearch( int [] arr, int target, int left, int right)
{
int index = -1;
while (left <= right)
{
int middle = left + (right - left) / 2;
if (arr[middle] == target)
{
index = middle;
left = middle + 1;
}
else
{
right = middle - 1;
}
}
return index;
}
}
|
Javascript
function findMax(arr) {
arr.sort((a, b) => a - b);
let maxNum = -1;
let i = 0;
while (i < arr.length) {
const current = arr[i];
const j = binarySearch(arr, current, i, arr.length - 1);
if ((j - i) + 1 === current) {
maxNum = current;
}
i = j + 1;
}
return maxNum;
}
function binarySearch(arr, target, left, right) {
let index = -1;
while (left <= right) {
const middle = left + Math.floor((right - left) / 2);
if (arr[middle] === target) {
index = middle;
left = middle + 1;
} else {
right = middle - 1;
}
}
return index;
}
const arr = [3, 2, 2, 3, 4, 3];
console.log(findMax(arr));
|
Time Complexity : O(N logN)
Space Complexity : O(1)
So far, we have reduced the space complexity from linear to constant.
Last Updated :
29 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...