Open In App

Count of elements in Array which are present K times & their double isn’t present

Last Updated : 27 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program for the above approach
#include <bits/stdc++.h>
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<int, int> 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<Integer, Integer> 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<Integer, Integer> 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<int, int> mp
            = new Dictionary<int, int>();
 
        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<int, int> 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




<script>
     // JavaScript code for the above approach
 
     // Function to find the count valid numbers
     function find(arr, N, K)
     {
      
         // Store ans
         let ans = 0;
         let mp = new Map();
         for (let i = 0; i < N; i++) {
 
             // Storing frequency of elements
             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) {
 
             // Simply checking the
             // given condition in question
             if (val == K) {
                 if (mp.has(key * 2)
                     == false) {
                     ans++;
                 }
             }
         }
         return ans;
     }
 
     // Driver Code
     let arr = [10, 6, 12, 8, 10, 8];
     let N = arr.length;
     let K = 2;
 
     document.write(find(arr, N, K));
      
    // This code is contributed by Potta Lokesh
 </script>


Output

2

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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the count valid numbers
int find(int arr[], int N, int K)
{
    // Store ans
    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;
}
 
// 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




import java.util.*;
 
public class Main {
    // Function to find the count of valid numbers
    static int find(int[] arr, int N, int K)
    {
        // Store ans
        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;
    }
 
    // Driver Code
    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




# Function to find the count valid numbers
def find(arr, N, K):
    # Store ans
    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
 
 
# Driver Code
arr = [10, 6, 12, 8, 10, 8]
N = len(arr)
K = 2
print(find(arr, N, K))


C#




using System;
 
class GFG {
    // Function to find the count valid numbers
    static int Find(int[] arr, int N, int K)
    {
        // Store ans
        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;
    }
 
    // Driver Code
    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 to find the count of valid numbers
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) 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads