Largest number in the Array having frequency same as value | C ++ code for the above problem ; Function to find the largest number whose frequency is equal to itself . ; Adding 65536 to keep the count of the current number ; right shifting by 16 bits to find the count of the number i ; Driver code
#include <bits/stdc++.h> NEW_LINE 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 ; }