Largest number in the Array having frequency same as value
Given an array arr containing N integers, the task is to find the largest number in the array whose frequency is equal to its value. If no such number exists, then print -1.
Examples:
Input: arr = [3, 2, 5, 2, 4, 5]
Output: 2
Explanation:
In this given array frequency of 2 is 2, whereas the frequency of the remaining numbers doesn’t match with itself. So the answer is 2.
Input: arr = [3, 3, 3, 4, 4, 4, 4]
Output: 4
Explanation:
In this given array frequency of 3 is 3 and 4 is 4 but largest number is 4. So the answer is 4.
Input: arr = [1, 1, 1, 2, 3, 3]
Output: -1
Explanation:
There is no such number in the given array whose frequency is equal to itself. Thus the output is -1.
Simple Approach:
- Create a new array to keep the count of the occurrences in the given array.
- Traverse the new array in reverse order.
- Return the first number whose count is equal to itself.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findLargestNumber(vector< int >& arr)
{
int k = *max_element(arr.begin(),
arr.end());
int m[k] = {};
for ( auto n : arr)
++m[n];
for ( auto n = arr.size(); n > 0; --n) {
if (n == m[n])
return n;
}
return -1;
}
int main()
{
vector< int > arr = { 3, 2, 5, 2, 4, 5 };
cout << findLargestNumber(arr) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findLargestNumber( int [] arr)
{
int k = Arrays.stream(arr).max().getAsInt();
int []m = new int [k + 1 ];
for ( int n : arr)
++m[n];
for ( int n = arr.length - 1 ; n > 0 ; --n)
{
if (n == m[n])
return n;
}
return - 1 ;
}
public static void main(String[] args)
{
int [] arr = { 3 , 2 , 5 , 2 , 4 , 5 };
System.out.print(findLargestNumber(arr) + "\n" );
}
}
|
Python3
def findLargestNumber(arr):
k = max (arr);
m = [ 0 ] * (k + 1 );
for n in arr:
m[n] + = 1 ;
for n in range ( len (arr) - 1 , 0 , - 1 ):
if (n = = m[n]):
return n;
return - 1 ;
if __name__ = = '__main__' :
arr = [ 3 , 2 , 5 , 2 , 4 , 5 ];
print (findLargestNumber(arr));
|
C#
using System;
using System.Linq;
class GFG{
static int findLargestNumber( int [] arr)
{
int k = arr.Max();
int []m = new int [k + 1];
foreach ( int n in arr)
++m[n];
for ( int n = arr.Length - 1; n > 0; --n)
{
if (n == m[n])
return n;
}
return -1;
}
public static void Main(String[] args)
{
int [] arr = { 3, 2, 5, 2, 4, 5 };
Console.Write(findLargestNumber(arr) + "\n" );
}
}
|
Javascript
<script>
function findLargestNumber(arr)
{
var k = arr.reduce((a,b)=> Math.max(a,b));
var m = Array(k).fill(0);
arr.forEach(n => {
++m[n];
});
for ( var n = arr.length; n > 0; --n) {
if (n == m[n])
return n;
}
return -1;
}
var arr = [3, 2, 5, 2, 4, 5];
document.write( findLargestNumber(arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space Complexity: O( N)
Note: This approach is valid only when the numbers in the given array are less than 65536 i.e. 216.
- Here, use the input array to store the count.
- Since the values are limited, simply use the first half(first 16 bits) of the integer for keeping the count by adding 65536.
- Use the right shift operator(right shift by 16 bits) while traversing the array in reverse order and return the first number whose count is equal to itself.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findLargestNumber(vector< int >& arr)
{
for ( auto n : arr) {
n &= 0xFFFF;
if (n <= arr.size()) {
arr[n - 1] += 0x10000;
}
}
for ( auto i = arr.size(); i > 0; --i) {
if ((arr[i - 1] >> 16) == i)
return i;
}
return -1;
}
int main()
{
vector< int > arr
= { 3, 2, 5, 5, 2, 4, 5 };
cout << findLargestNumber(arr)
<< endl;
return 0;
}
|
Java
class GFG{
static int findLargestNumber( int [] arr, int n)
{
for ( int i = 0 ; i < n; i++)
{
arr[i] &= 0xFFFF ;
if (arr[i] <= n)
{
arr[i] += 0x10000 ;
}
}
for ( int i = n - 1 ; i > 0 ; --i)
{
if ((arr[i] >> 16 ) == i)
return i + 1 ;
}
return - 1 ;
}
public static void main(String[] args)
{
int []arr = { 3 , 2 , 5 , 5 , 2 , 4 , 5 };
int n = arr.length;
System.out.print(findLargestNumber(arr, n) + "\n" );
}
}
|
Python3
def findLargestNumber(arr, n):
for i in range (n):
arr[i] & = 0xFFFF ;
if (arr[i] < = n):
arr[i] + = 0x10000 ;
for i in range (n - 1 , 0 , - 1 ):
if ((arr[i] >> 16 ) = = i):
return i + 1 ;
return - 1 ;
if __name__ = = '__main__' :
arr = [ 3 , 2 , 5 , 5 , 2 , 4 , 5 ];
n = len (arr);
print (findLargestNumber(arr, n));
|
C#
using System;
class GFG{
static int findLargestNumber( int [] arr, int n)
{
for ( int i = 0; i < n; i++)
{
arr[i] &= 0xFFFF;
if (arr[i] <= n)
{
arr[i] += 0x10000;
}
}
for ( int i = n - 1; i > 0; --i)
{
if ((arr[i] >> 16) == i)
return i + 1;
}
return -1;
}
public static void Main(String[] args)
{
int []arr = { 3, 2, 5, 5, 2, 4, 5 };
int n = arr.Length;
Console.Write(findLargestNumber(arr, n) + "\n" );
}
}
|
Javascript
<script>
function findLargestNumber(arr)
{
arr.forEach(n => {
n &= 0xFFFF;
if (n <= arr.length) {
arr[n - 1] += 0x10000;
}
});
for ( var i = arr.length; i > 0; --i) {
if ((arr[i - 1] >> 16) == i)
return i;
}
return -1;
}
var arr
= [3, 2, 5, 5, 2, 4, 5];
document.write( findLargestNumber(arr))
</script>
|
Time Complexity: O(N)
Auxiliary Space Complexity: O(1)
Last Updated :
23 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...