给定一个由N 个正整数组成的数组arr[] ,任务是找到数组中存在小于或等于该数字的 2 的最高幂的数组元素的数量。
例子:
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
朴素的方法:给定的问题可以通过计算给定数组中存在的 2 的最高幂的元素来解决,并且可以通过再次遍历数组找到这些元素。检查所有元素后,打印获得的总数。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效的方法:上述方法也可以通过使用 unordered_map 来优化,以保持访问元素的数量并相应地更新结果元素的数量。请按照以下步骤解决给定的问题:
- 初始化一个变量,比如count ,它存储数组中存在的元素的计数,其最高 2 的幂小于或等于该值。
- 初始化一个映射M并存储数组元素的频率。
- 遍历给定数组,对于每个元素,如果映射中存在不超过元素的arr[i]的 2 的最高幂的频率,则将count的值增加1。
- 完成上述步骤后,打印count的值作为结果元素计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count array elements
// whose highest power of 2 is less
// than or equal to that number is
// present in the given array
int countElement(int arr[], int N)
{
// Stores the resultant count
// of array elements
int count = 0;
// Stores frequency of
// visited array elements
unordered_map m;
// Traverse the array
for (int i = 0; i < N; i++) {
m[arr[i]]++;
}
for (int i = 0; i < N; i++) {
// Calculate log base 2
// of the element arr[i]
int lg = log2(arr[i]);
// Highest power of 2 whose
// value is at most arr[i]
int p = pow(2, lg);
// Increment the count by 1
if (m[p]) {
count++;
}
}
// Return the resultant count
return count;
}
// Driver Code
int main()
{
int arr[] = { 3, 4, 6, 9 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << countElement(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.HashMap;
import java.io.*;
class GFG{
static int log2(int N)
{
// Calculate log2 N indirectly
// using log() method
int result = (int)(Math.log(N) /
Math.log(2));
return result;
}
// Function to count array elements
// whose highest power of 2 is less
// than or equal to that number is
// present in the given array
static int countElement(int arr[], int N)
{
// Stores the resultant count
// of array elements
int count = 0;
// Stores frequency of
// visited array elements
HashMap m = new HashMap<>();
// Traverse the array
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++)
{
// Calculate log base 2
// of the element arr[i]
int lg = log2(arr[i]);
// Highest power of 2 whose
// value is at most arr[i]
int p = (int)Math.pow(2, lg);
// Increment the count by 1
if (m.containsKey(p))
{
count++;
}
}
// Return the resultant count
return count;
}
// Driver Code
public static void main (String[] args)
{
int arr[] = { 3, 4, 6, 9 };
int N = arr.length;
System.out.println(countElement(arr, N));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python program for the above approach
from math import log2
# Function to count array elements
# whose highest power of 2 is less
# than or equal to that number is
# present in the given array
def countElement(arr, N):
# Stores the resultant count
# of array elements
count = 0
# Stores frequency of
# visited array elements
m = {}
# Traverse the array
for i in range(N):
m[arr[i]] = m.get(arr[i], 0) + 1
for i in range(N):
# Calculate log base 2
# of the element arr[i]
lg = int(log2(arr[i]))
# Highest power of 2 whose
# value is at most arr[i]
p = pow(2, lg)
# Increment the count by 1
if (p in m):
count += 1
# Return the resultant count
return count
# Driver Code
if __name__ == '__main__':
arr= [3, 4, 6, 9]
N = len(arr)
print (countElement(arr, N))
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to count array elements
// whose highest power of 2 is less
// than or equal to that number is
// present in the given array
static int countElement(int []arr, int N)
{
// Stores the resultant count
// of array elements
int count = 1;
// Stores frequency of
// visited array elements
Dictionary m = new Dictionary();
// Traverse the array
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++) {
// Calculate log base 2
// of the element arr[i]
int lg = (int)Math.Log(arr[i]);
// Highest power of 2 whose
// value is at most arr[i]
int p = (int)Math.Pow(2, lg);
// Increment the count by 1
if (m.ContainsKey(p)) {
count++;
}
}
// Return the resultant count
return count;
}
// Driver Code
public static void Main()
{
int []arr = { 3, 4, 6, 9 };
int N = arr.Length;
Console.Write(countElement(arr, N));
}
}
// This code is contributed by bgangwar59.
Javascript
输出:
2
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。