Given an array arr[] of N integers, the task is to find the count of elements in the array that are present K times and their double are not present in the array.
Examples:
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.
Approach 1:
The task can be solved using a hashmap Follow the below steps to solve the problem:
- Store the occurrences of each of the numbers in a hashmap
- For each element, if the frequency is K, check whether its double is present in the hashmap or not. If it is not present, store it as one of the valid numbers.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int find( int arr[], int N, int K)
{
int ans = 0;
unordered_map< int , int > mp;
for ( int i = 0; i < N; i++) {
mp[arr[i]]++;
}
for ( auto it : mp) {
if (it.second == K) {
if (mp.find(it.first * 2) == mp.end()) {
ans++;
}
}
}
return ans;
}
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
import java.util.*;
class GFG {
public static int find( int [] arr, int N, int K)
{
int ans = 0 ;
HashMap<Integer, Integer> mp = new HashMap<>();
for ( int i = 0 ; i < N; i++) {
if (mp.containsKey(arr[i])) {
mp.put(arr[i], mp.get(arr[i]) + 1 );
}
else {
mp.put(arr[i], 1 );
}
}
for (Map.Entry<Integer, Integer> i :
mp.entrySet()) {
if (i.getValue() == K) {
if (mp.containsKey(i.getKey() * 2 )
== false ) {
ans++;
}
}
}
return ans;
}
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));
}
}
|
Python3
def find(arr, N, K):
ans = 0
mp = {}
for i in range (N):
if arr[i] not in mp:
mp[arr[i]] = 1
else :
mp[arr[i]] + = 1
for i in mp:
if (mp[i] = = K):
if ((i * 2 ) not in mp):
ans + = 1
return ans
arr = [ 10 , 6 , 12 , 8 , 10 , 8 ]
N = len (arr)
K = 2
print (find(arr, N, K))
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static int find( int [] arr, int N, int K)
{
int ans = 0;
Dictionary< int , int > mp
= new Dictionary< int , int >();
for ( int i = 0; i < N; i++) {
if (!mp.ContainsKey(arr[i])) {
mp.Add(arr[i], 1);
}
else {
mp[arr[i]] = mp[arr[i]] + 1;
}
}
foreach (KeyValuePair< int , int > x in mp)
{
if (x.Value == K) {
if (mp.ContainsKey(x.Key * 2) == false ) {
ans++;
}
}
}
return ans;
}
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));
}
}
|
Javascript
<script>
function find(arr, N, K)
{
let ans = 0;
let mp = new Map();
for (let i = 0; i < N; i++) {
if (mp.has(arr[i])) {
mp.set(arr[i], mp.get(arr[i]) + 1)
}
else {
mp.set(arr[i], 1)
}
}
for (let [key, val] of mp) {
if (val == K) {
if (mp.has(key * 2)
== false ) {
ans++;
}
}
}
return ans;
}
let arr = [10, 6, 12, 8, 10, 8];
let N = arr.length;
let K = 2;
document.write(find(arr, N, K));
</script>
|
Time Complexity: O(N), since one loop is used for traversal of the entire array the algorithm takes up linear time O(N)
Auxiliary Space: O(N), since an unordered map is used in the worst case it takes up all the elements of the array and hence takes up linear space O(N)
Approach 2:
We will follow the following steps-
- First sort the input array.
- Make a variable “ans” and initialize it to 0. This variable will contain the final answer.
- After that start traversing the array and find the count of the consecutive elements with the same value.
- If the same consecutive element’s count is greater than K then check by running another loop whether it’s double exists or not. If it’s double does not exist then increment “ans” by 1.
- In the last print the answer.
Code-
C++
#include <bits/stdc++.h>
using namespace std;
int find( int arr[], int N, int K)
{
int ans = 0;
sort(arr, arr + N);
int temp = INT_MIN;
int count = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] == temp) {
count++;
}
else {
if (count >= K) {
int j = i + 1;
while (j < N) {
if (arr[j] == 2 * temp) {
break ;
}
j++;
}
if (j == N) {
ans++;
}
}
temp = arr[i];
count = 1;
}
}
return ans;
}
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
import java.util.*;
public class Main {
static int find( int [] arr, int N, int K)
{
int ans = 0 ;
Arrays.sort(arr);
int temp = Integer.MIN_VALUE;
int count = 0 ;
for ( int i = 0 ; i < N; i++) {
if (arr[i] == temp) {
count++;
}
else {
if (count >= K) {
int j = i + 1 ;
while (j < N) {
if (arr[j] == 2 * temp) {
break ;
}
j++;
}
if (j == N) {
ans++;
}
}
temp = arr[i];
count = 1 ;
}
}
return ans;
}
public static void main(String[] args)
{
int [] arr = { 10 , 6 , 12 , 8 , 10 , 8 };
int N = arr.length;
int K = 2 ;
System.out.println(find(arr, N, K));
}
}
|
Python
def find(arr, N, K):
ans = 0
arr.sort()
temp = float ( '-inf' )
count = 0
for i in range (N):
if arr[i] = = temp:
count + = 1
else :
if count > = K:
j = i + 1
while j < N:
if arr[j] = = 2 * temp:
break
j + = 1
if j = = N:
ans + = 1
temp = arr[i]
count = 1
return ans
arr = [ 10 , 6 , 12 , 8 , 10 , 8 ]
N = len (arr)
K = 2
print (find(arr, N, K))
|
C#
using System;
class GFG {
static int Find( int [] arr, int N, int K)
{
int ans = 0;
Array.Sort(arr);
int temp = int .MinValue;
int count = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] == temp) {
count++;
}
else {
if (count >= K) {
int j = i + 1;
while (j < N) {
if (arr[j] == 2 * temp) {
break ;
}
j++;
}
if (j == N) {
ans++;
}
}
temp = arr[i];
count = 1;
}
}
return ans;
}
public static void Main( string [] args)
{
int [] arr = { 10, 6, 12, 8, 10, 8 };
int N = arr.Length;
int K = 2;
Console.WriteLine(Find(arr, N, K));
}
}
|
Javascript
function find(arr, N, K) {
let ans = 0;
arr.sort((a, b) => a - b);
let temp = -Infinity;
let count = 0;
for (let i = 0; i < N; i++) {
if (arr[i] === temp) {
count++;
} else {
if (count >= K) {
let j = i + 1;
while (j < N && arr[j] <= 2 * temp) {
j++;
}
if (j === N) {
ans++;
}
}
temp = arr[i];
count = 1;
}
}
if (count >= K) {
let j = N;
while (j >= 0 && arr[j] > 2 * temp) {
j--;
}
if (j === 0) {
ans++;
}
}
return ans;
}
let arr = [10, 6, 12, 8, 10, 8];
let N = arr.length;
let K = 2;
console.log(find(arr, N, K));
|
Output-
2
Time Complexity: O(NlogN)+O(N2)=O(N2)
Auxiliary Space: O(1)