Given an array arr[] consisting of N positive integers, the task is to find the number of array elements whose highest power of 2 less than or equal to that number is present in the array.
Examples:
Input: arr[] = {3, 4, 6, 9}
Output: 2
Explanation:
There are 2 array elements (4 and 6), whose highest power of 2 is less than it (i.e. 4) are present in the array.
Input: arr[] = {3, 9, 10, 8, 1}
Output: 3
Naive Approach: The given problem can be solved by count those elements whose highest power of 2 exists in the given array and that can be found by traversing the array again. After checking for all the elements, print the total count obtained.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by using an unordered_map to keep the count of visited elements and update the count of resultant elements accordingly. Follow the steps below to solve the given problem:
- Initialize a variable, say count that stores the count of elements whose highest power of 2 less than or equals to that is present in the array.
- Initialize a map M and store the frequency of array elements.
- Traverse the given array and for each element, if the frequency of the highest power of 2 of arr[i] not exceeding the element exists in the map then increment the value of count by 1.
- After completing the above steps, print the value of count as the resultant count of elements.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countElement( int arr[], int N)
{
int count = 0;
unordered_map< int , int > m;
for ( int i = 0; i < N; i++) {
m[arr[i]]++;
}
for ( int i = 0; i < N; i++) {
int lg = log2(arr[i]);
int p = pow (2, lg);
if (m[p]) {
count++;
}
}
return count;
}
int main()
{
int arr[] = { 3, 4, 6, 9 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << countElement(arr, N);
return 0;
}
|
Java
import java.util.HashMap;
import java.io.*;
class GFG{
static int log2( int N)
{
int result = ( int )(Math.log(N) /
Math.log( 2 ));
return result;
}
static int countElement( int arr[], int N)
{
int count = 0 ;
HashMap<Integer, Integer> m = new HashMap<>();
for ( int i = 0 ; i < N; i++)
{
if (m.containsKey(arr[i]))
{
m.put(arr[i], m.get(arr[i]) + 1 );
}
else
{
m.put(arr[i], 1 );
}
}
for ( int i = 0 ; i < N; i++)
{
int lg = log2(arr[i]);
int p = ( int )Math.pow( 2 , lg);
if (m.containsKey(p))
{
count++;
}
}
return count;
}
public static void main (String[] args)
{
int arr[] = { 3 , 4 , 6 , 9 };
int N = arr.length;
System.out.println(countElement(arr, N));
}
}
|
Python3
from math import log2
def countElement(arr, N):
count = 0
m = {}
for i in range (N):
m[arr[i]] = m.get(arr[i], 0 ) + 1
for i in range (N):
lg = int (log2(arr[i]))
p = pow ( 2 , lg)
if (p in m):
count + = 1
return count
if __name__ = = '__main__' :
arr = [ 3 , 4 , 6 , 9 ]
N = len (arr)
print (countElement(arr, N))
|
C#
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));
}
}
|
Javascript
<script>
function countElement(arr, N) {
let count = 0;
let m = new Map();
for (let i = 0; i < N; i++) {
if (m.has(arr[i])) {
m.set(arr[i], m.get(arr[i]) + 1)
} else {
m.set(arr[i], 1)
}
}
for (let i = 0; i < N; i++) {
let lg = Math.floor(Math.log2(arr[i]));
let p = Math.pow(2, lg);
if (m.get(p)) {
count++;
}
}
return count;
}
let arr = [3, 4, 6, 9];
let N = arr.length
document.write(countElement(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)