Count array elements whose highest power of 2 less than or equal to that number is present in the given array | C # program for the above approach ; Function to count array elements whose highest power of 2 is less than or equal to that number is present in the given array ; Stores the resultant count of array elements ; Stores frequency of visited array elements ; Traverse the array ; Calculate log base 2 of the element arr [ i ] ; Highest power of 2 whose value is at most arr [ i ] ; Increment the count by 1 ; Return the resultant count ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countElement ( int [ ] arr , int N ) { int count = 1 ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( m . ContainsKey ( arr [ i ] ) ) m [ arr [ i ] ] ++ ; else m . Add ( arr [ i ] , 1 ) ; } for ( int i = 0 ; i < N ; i ++ ) { int lg = ( int ) Math . Log ( arr [ i ] ) ; int p = ( int ) Math . Pow ( 2 , lg ) ; if ( m . ContainsKey ( p ) ) { count ++ ; } } return count ; } public static void Main ( ) { int [ ] arr = { 3 , 4 , 6 , 9 } ; int N = arr . Length ; Console . Write ( countElement ( arr , N ) ) ; } }