Print all array elements having frequencies equal to powers of K in ascending order
Given an array arr[] consisting of N integers and a positive integer K, the task is to find the array elements having frequencies in the power of K i.e., K1, K2, K3, and so on.
Examples:
Input: arr[] = {1, 3, 2, 1, 2, 2, 2, 3, 3, 4}, K = 2
Output: 1 2
Explanation:
The frequency of 1 is 2, that can be represented as the power of K( = 2), i.e., 21.
The frequency of 2 is 4, that can be represented as the power of K( = 2), i.e., 22.
Input: arr[] = {6, 1, 3, 1, 2, 2, 1}, K = 2
Output: 2 3 6
Naive Approach: The simplest approach is to count the frequencies of each array element and if the frequency of any element is a perfect power of K, then print that element. Otherwise, check for the next element.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by using Hashing for storing the frequency of arrays elements in a HashMap and then check for the required conditions. Follow the steps below to solve the given problem:
- Traverse the given array arr[] and store the frequency of each array element in a Map, say M.
- Now, traverse the map and perform the following steps:
- Store the frequency of each value in the map in a variable, say F.
- If the value of (log F)/(log K) and K(log F)/(log K) are the same, then the current element has the frequency as the perfect power of K. Therefore, print the current element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countFrequency( int arr[], int N,
int K)
{
unordered_map< int , int > freq;
for ( int i = 0; i < N; i++) {
freq[arr[i]]++;
}
for ( auto i : freq) {
int lg = log (i.second) / log (K);
int a = pow (K, lg);
if (a == i.second) {
cout << i.first << " " ;
}
}
}
int main()
{
int arr[] = { 1, 4, 4, 2,
1, 2, 3, 2, 2 };
int K = 2;
int N = sizeof (arr) / sizeof (arr[0]);
countFrequency(arr, N, K);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static void countFrequency( int arr[], int N, int K)
{
HashMap<Integer, Integer> freq = new HashMap<>();
for ( int i = 0 ; i < N; i++) {
freq.put(arr[i],
freq.getOrDefault(arr[i], 0 ) + 1 );
}
for ( int key : freq.keySet()) {
int lg = ( int )(Math.log(freq.get(key))
/ Math.log(K));
int a = ( int )(Math.pow(K, lg));
if (a == freq.get(key)) {
System.out.print(key + " " );
}
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 4 , 4 , 2 , 1 , 2 , 3 , 2 , 2 };
int K = 2 ;
int N = arr.length;
countFrequency(arr, N, K);
}
}
|
Python3
from math import log
def countFrequency(arr, N, K):
freq = {}
for i in range (N):
if (arr[i] in freq):
freq[arr[i]] + = 1
else :
freq[arr[i]] = 1
for key,value in freq.items():
lg = log(value) / / log(K)
a = pow (K, lg)
if (a = = value):
print (key, end = " " )
if __name__ = = '__main__' :
arr = [ 1 , 4 , 4 , 2 , 1 , 2 , 3 , 2 , 2 ]
K = 2
N = len (arr)
countFrequency(arr, N, K)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void countFrequency( int []arr, int N,
int K)
{
Dictionary< int ,
int > freq = new Dictionary< int ,
int >();
for ( int i = 0; i < N; i++)
{
if (freq.ContainsKey(arr[i]))
freq[arr[i]] += 1;
else
freq[arr[i]] = 1;
}
foreach (KeyValuePair< int , int > entry in freq)
{
int temp = entry.Key;
int lg = ( int )(Math.Log(entry.Value) /
Math.Log(K));
int a = ( int )Math.Pow(K, lg);
if (a == entry.Value)
{
Console.Write(entry.Key + " " );
}
}
}
public static void Main()
{
int []arr = { 1, 4, 4, 2,
1, 2, 3, 2, 2 };
int K = 2;
int N = arr.Length;
countFrequency(arr, N, K);
}
}
|
Javascript
<script>
function countFrequency(arr, N, K)
{
let freq = new Map();
key = [3, 2, 1, 4]
for (let i = 0; i < N; i++)
{
if (freq.has(arr[i]))
freq.set(arr[i], freq.get(arr[i]) + 1);
else
freq.set(arr[i], 1);
}
let i = 0;
freq.forEach((values,keys)=>{
let temp = keys;
let lg = parseInt(Math.log(values) /
Math.log(K), 10);
let a = parseInt(Math.pow(K, lg), 10);
if (a == values)
{
document.write(key[i] + " " );
i++;
}
})
}
let arr = [ 1, 4, 4, 2,
1, 2, 3, 2, 2 ];
let K = 2;
let N = arr.length;
countFrequency(arr, N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
28 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...