Array 中存在 K 次且不存在双精度的元素的计数
给定一个包含 N 个整数的数组arr[] ,任务是找出数组中存在K 次且它们的 double 不存在于数组中的元素的计数。
例子:
Input: arr[] = {10, 6, 12, 8, 10, 8}, K = 2
Output: 2
Explanation: 10 is a valid number since it appears exactly two times and 20 does not appear in array.
8 is a valid number since it appears two times and 16 does not appear in array.
Input: arr[] = {1, 3, 5, 3}, K = 3
Output: 0
Explanation: No element in the given array satisfy the condition.
Input: arr[] = {1, 2, 2, 3, 3, 4}, K = 2
Output: 1
Explanation: Only 3 is valid element.
Though 2 is present twice but its double is also present.
方法:可以使用hashmap解决任务按照以下步骤解决问题:
- 将每个数字的出现存储在哈希图中
- 对于每个元素,如果频率为K ,请检查其双精度值是否存在于哈希图中。如果不存在,请将其存储为有效数字之一。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the count valid numbers
int find(int arr[], int N, int K)
{
// Store ans
int ans = 0;
unordered_map mp;
for (int i = 0; i < N; i++) {
// Storing frequency of elements
mp[arr[i]]++;
}
for (auto it : mp) {
// Simply checking the
// given condition in question
if (it.second == K) {
if (mp.find(it.first * 2)
== mp.end()) {
ans++;
}
}
}
return ans;
}
// Driver Code
int main()
{
int arr[] = { 10, 6, 12, 8, 10, 8 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 2;
cout << find(arr, N, K);
return 0;
}
Java
// JAVA program for the above approach
import java.util.*;
class GFG
{
// Function to find the count valid numbers
public static int find(int[] arr, int N, int K)
{
// Store ans
int ans = 0;
HashMap mp = new HashMap<>();
for (int i = 0; i < N; i++) {
// Storing frequency of elements
if (mp.containsKey(arr[i])) {
mp.put(arr[i], mp.get(arr[i]) + 1);
}
else {
mp.put(arr[i], 1);
}
}
for (Map.Entry i :
mp.entrySet()) {
// Simply checking the
// given condition in question
if (i.getValue() == K) {
if (mp.containsKey(i.getKey() * 2)
== false) {
ans++;
}
}
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
int[] arr = new int[] { 10, 6, 12, 8, 10, 8 };
int N = arr.length;
int K = 2;
System.out.print(find(arr, N, K));
}
}
// This code is contributed by Taranpreet
Python3
# Python program for the above approach
# Function to find the count valid numbers
def find(arr, N, K):
# Store ans
ans = 0
mp = {}
for i in range(N):
# Storing frequency of elements
if arr[i] not in mp:
mp[arr[i]] = 1
else:
mp[arr[i]]+=1
for i in mp:
# Simply checking the
# given condition in question
if (mp[i] == K):
if ((i * 2) not in mp):
ans += 1
return ans
# Driver Code
arr = [ 10, 6, 12, 8, 10, 8 ]
N = len(arr)
K = 2
print(find(arr, N, K))
# This code is contributed by rohitsingh07052
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the count valid numbers
public static int find(int[] arr, int N, int K)
{
// Store ans
int ans = 0;
Dictionary mp =
new Dictionary();
for (int i = 0; i < N; i++) {
// Storing frequency of elements
if (!mp.ContainsKey(arr[i])) {
mp.Add(arr[i], 1);
}
else {
mp[arr[i]] = mp[arr[i]] + 1;
}
}
foreach(KeyValuePair x in mp)
{
// Simply checking the
// given condition in question
if (x.Value == K) {
if (mp.ContainsKey(x.Key * 2) == false) {
ans++;
}
}
}
return ans;
}
// Driver Code
public static void Main()
{
int[] arr = new int[] { 10, 6, 12, 8, 10, 8 };
int N = arr.Length;
int K = 2;
Console.Write(find(arr, N, K));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
2
时间复杂度: O(N)
辅助空间: 在)