Given an array arr[] (may contain duplicates) and a positive integer k, count the number of elements whose occurrence is exactly equal to the size of array arr[] divided by k.
Examples:
Input: k = 2, arr[] = [1, 4, 1, 2, 4]
Output: 2
Explanation: In the given array, 1 and 4 occurs floor(5/2) = 2 times.Input: k = 4, arr[] = [1, 1, 7, 1]
Output: 1
Explanation: In the given array, only 7 occurs floor(4/4) = 1 times.
Table of Content
[Naive Approach] Frequency Counting - O(n ^ 2) Time and O(n) Space
The idea is to check the frequency of each distinct element by traversing the array. If its frequency is exactly n / k, count it once.
Working of Approach:
- Find the required frequency as n / k.
- Traverse the array and select each distinct element.
- Count its occurrences by scanning the complete array.
- Mark the counted elements to avoid processing duplicates again.
- If the frequency is n / k, increment the answer.
#include <iostream>
#include <vector>
using namespace std;
int countSpecials(int k, vector<int> &arr)
{
int n = arr.size();
int target_count = n / k;
int special_count = 0;
vector<bool> visited(n, false);
// Check frequency of each distinct element
for (int i = 0; i < n; i++)
{
if (visited[i])
continue;
int count = 0;
// Count occurrences of arr[i]
for (int j = 0; j < n; j++)
{
if (arr[i] == arr[j])
{
count++;
visited[j] = true;
}
}
// Check if frequency is exactly n/k
if (count == target_count)
{
special_count++;
}
}
return special_count;
}
int main()
{
int k = 4;
vector<int> arr = {1, 1, 7, 1};
cout << countSpecials(k, arr) << endl;
return 0;
}
import java.util.*;
class GFG {
public int countSpecials(int k, int[] arr)
{
int n = arr.length;
int target_count = n / k;
int special_count = 0;
boolean[] visited = new boolean[n];
// Check frequency of each distinct element
for (int i = 0; i < n; i++) {
if (visited[i])
continue;
int count = 0;
// Count occurrences of arr[i]
for (int j = 0; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
visited[j] = true;
}
}
// Check if frequency is exactly n/k
if (count == target_count) {
special_count++;
}
}
return special_count;
}
public static void main(String[] args)
{
int k = 4;
int[] arr = { 1, 1, 7, 1 };
GFG obj = new GFG();
System.out.println(obj.countSpecials(k, arr));
}
}
def countSpecials(k, arr):
n = len(arr)
target_count = n // k
special_count = 0
visited = [False] * n
# Check frequency of each distinct element
for i in range(n):
if visited[i]:
continue
count = 0
# Count occurrences of arr[i]
for j in range(n):
if arr[i] == arr[j]:
count += 1
visited[j] = True
# Check if frequency is exactly n/k
if count == target_count:
special_count += 1
return special_count
if __name__ == '__main__':
k = 4
arr = [1, 1, 7, 1]
print(countSpecials(k, arr))
using System;
class GFG {
public int countSpecials(int k, int[] arr)
{
int n = arr.Length;
int target_count = n / k;
int special_count = 0;
bool[] visited = new bool[n];
// Check frequency of each distinct element
for (int i = 0; i < n; i++) {
if (visited[i])
continue;
int count = 0;
// Count occurrences of arr[i]
for (int j = 0; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
visited[j] = true;
}
}
// Check if frequency is exactly n/k
if (count == target_count) {
special_count++;
}
}
return special_count;
}
public static void Main()
{
int k = 4;
int[] arr = { 1, 1, 7, 1 };
GFG obj = new GFG();
Console.WriteLine(obj.countSpecials(k, arr));
}
}
function countSpecials(k, arr)
{
let n = arr.length;
let target_count = Math.floor(n / k);
let special_count = 0;
let visited = new Array(n).fill(false);
// Check frequency of each distinct element
for (let i = 0; i < n; i++) {
if (visited[i])
continue;
let count = 0;
// Count occurrences of arr[i]
for (let j = 0; j < n; j++) {
if (arr[i] === arr[j]) {
count++;
visited[j] = true;
}
}
// Check if frequency is exactly n/k
if (count === target_count) {
special_count++;
}
}
return special_count;
}
// Driver Code
let k = 4;
let arr = [ 1, 1, 7, 1 ];
console.log(countSpecials(k, arr));
Output
1
[Expected Approach] Hashing (Frequency Map) - O(n) Time and O(n) Space
The idea is to store the frequency of every element using a hash map. Then, count the elements whose frequency is exactly floor(n/k).
Working of Approach:
- Find the required frequency as n / k.
- Store the frequency of each element in an unordered_map.
- Traverse the frequency map.
- Check if an element occurs exactly n / k times.
- Increment the answer for every such element.
Let us understand with an example:
Input: k = 4, arr[] = [1, 1, 7, 1]
- n = 4, so target_count = n / k = 4 / 4 = 1.
- Build the frequency map: 1 -> 3, 7 -> 1.
- Check 1: its frequency is 3, which is not equal to 1.
- Check 7: its frequency is 1, so increment special_count to 1.
- Therefore, the final answer is 1.
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int countSpecials(int k, vector<int> &arr)
{
int n = arr.size();
int target_count = n / k;
unordered_map<int, int> freq;
// Count frequency of each element
for (int num : arr)
{
freq[num]++;
}
int special_count = 0;
// Count elements occurring exactly n/k times
for (auto &entry : freq)
{
if (entry.second == target_count)
{
special_count++;
}
}
return special_count;
}
int main()
{
int k = 4;
vector<int> arr = {1, 1, 7, 1};
cout << countSpecials(k, arr) << endl;
return 0;
}
import java.util.*;
class GFG {
public int countSpecials(int k, int[] arr)
{
int n = arr.length;
int target_count = n / k;
HashMap<Integer, Integer> freq = new HashMap<>();
// Count frequency of each element
for (int num : arr) {
freq.put(num, freq.getOrDefault(num, 0) + 1);
}
int special_count = 0;
// Count elements occurring exactly n/k times
for (Map.Entry<Integer, Integer> entry :
freq.entrySet()) {
if (entry.getValue() == target_count) {
special_count++;
}
}
return special_count;
}
public static void main(String[] args)
{
int k = 4;
int[] arr = { 1, 1, 7, 1 };
GFG obj = new GFG();
System.out.println(obj.countSpecials(k, arr));
}
}
from collections import defaultdict
def countSpecials(k, arr):
n = len(arr)
target_count = n // k
freq = defaultdict(int)
# Count frequency of each element
for num in arr:
freq[num] += 1
special_count = 0
# Count elements occurring exactly n/k times
for count in freq.values():
if count == target_count:
special_count += 1
return special_count
if __name__ == "__main__":
k = 4
arr = [1, 1, 7, 1]
print(countSpecials(k, arr))
using System;
using System.Collections.Generic;
class GFG {
public int countSpecials(int k, int[] arr)
{
int n = arr.Length;
int target_count = n / k;
Dictionary<int, int> freq
= new Dictionary<int, int>();
// Count frequency of each element
foreach(int num in arr)
{
if (freq.ContainsKey(num)) {
freq[num]++;
}
else {
freq[num] = 1;
}
}
int special_count = 0;
// Count elements occurring exactly n/k times
foreach(var entry in freq)
{
if (entry.Value == target_count) {
special_count++;
}
}
return special_count;
}
public static void Main()
{
int k = 4;
int[] arr = { 1, 1, 7, 1 };
GFG obj = new GFG();
Console.WriteLine(obj.countSpecials(k, arr));
}
}
function countSpecials(k, arr)
{
const n = arr.length;
const target_count = Math.floor(n / k);
const freq = new Map();
// Count frequency of each element
for (const num of arr) {
freq.set(num, (freq.get(num) || 0) + 1);
}
let special_count = 0;
// Count elements occurring exactly n/k times
for (const [key, value] of freq) {
if (value === target_count) {
special_count++;
}
}
return special_count;
}
// Driver Code
const k = 4;
const arr = [ 1, 1, 7, 1 ];
console.log(countSpecials(k, arr));
Output
1