Find highest frequency of non-negative powers that are same as indices of elements in given Array
Last Updated :
02 Dec, 2021
Given an array arr[] with N non-negative integers, find the maximum number of elements that are the same non-negative powers of their indices.
arr[i] = iX, where X is a non negative number.
The task is to return the maximum frequency of X.
Example:
Input: arr = [1, 1, 4, 17]
Output: 2
Explanation:
The element 1 at index 0, is a power of 0
The element 1 at index 1, is a power of 0
The element 4 at index 2, is a power of 2
The element 17 at index 3, is not a power of its index, so it is not considered
Therefore the maximum frequency is of power 0, which is 2
Input: arr = [0, 1, 1, 9, 1, 25]
Output: 4
Explanation:
The element 0 at index 0, is a power of 2
The element 1 at index 1, is a power of 2
The element 1 at index 2, is a power of 0
The element 9 at index 3, is a power of 2
The element 1 at index 4, is a power of 0
The element 25 at index 5, is a power of 2
Therefore the maximum frequency is of power 2, which is 4
Approach: Given problem can be solved by finding the powers of every index and checking if they are equal to the element present at that index.
Follow the steps below to solve the problem:
- Iterate the array arr from index 2 till the end and at every index:
- Use a loop to multiply the index by itself until the value is less than the maximum value of integer and less than or equal to the element present at that index
- If the power becomes equal to the element then check if its present in the hashmap:
- If the power is not present then add it in the hash map with value 1
- Else if the power is already present then increment its frequency by 1
- If arr[0] = 1, then increment the frequency of 0 in the hashmap by 1
- Iterate the HashMap and find the value with the maximum frequency:
- If arr[0] = 1, then the frequency of all values except 0 is increment by 1
- If arr[1] = 1, then return, maxFreq +1, else return maxFreq
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int indPowEqualsEle(vector< int > arr)
{
int len = arr.size();
unordered_map< int , int > map;
long limit = INT_MAX;
for ( int i = 2; i < len; i++)
{
if (arr[i] == 1)
{
map[0]++;
continue ;
}
long indPow = i;
int p = 1;
while (indPow <= limit && indPow <= arr[i])
{
if (arr[i] == indPow)
{
map[p]++;
break ;
}
p++;
indPow *= i;
}
}
map[0]++;
int maxFreq = 0;
for ( auto it = map.begin(); it != map.end(); it++)
{
int power = it->second;
if (arr[0] == 0 && power != 0)
{
maxFreq = max(maxFreq,
map[power] + 1);
}
else
{
maxFreq = max(maxFreq,
map[power]);
}
}
return arr[1] == 1
? maxFreq + 1
: maxFreq;
}
int main()
{
vector< int > arr = {0, 1, 1, 9, 1, 25};
cout << (indPowEqualsEle(arr));
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int indPowEqualsEle( int [] arr)
{
int len = arr.length;
Map<Integer, Integer> map
= new HashMap<>();
long limit = ( long )Integer.MAX_VALUE;
for ( int i = 2 ; i < len; i++) {
if (arr[i] == 1 ) {
map.put( 0 ,
map.getOrDefault( 0 , 0 ) + 1 );
continue ;
}
long indPow = i;
int p = 1 ;
while (indPow <= limit
&& indPow <= arr[i]) {
if (arr[i] == indPow) {
map
.put(p,
map.getOrDefault(p, 0 ) + 1 );
break ;
}
p++;
indPow *= i;
}
}
map.put( 0 , map.getOrDefault( 0 , 0 ) + 1 );
int maxFreq = 0 ;
for ( int power : map.keySet()) {
if (arr[ 0 ] == 0 && power != 0 ) {
maxFreq
= Math.max(maxFreq,
map.get(power) + 1 );
}
else {
maxFreq = Math.max(maxFreq,
map.get(power));
}
}
return arr[ 1 ] == 1
? maxFreq + 1
: maxFreq;
}
public static void main(String[] args)
{
int [] arr = { 0 , 1 , 1 , 9 , 1 , 25 };
System.out.println(indPowEqualsEle(arr));
}
}
|
Python3
from collections import defaultdict
import sys
def indPowEqualsEle(arr):
length = len (arr)
map = defaultdict( int )
limit = sys.maxsize
for i in range ( 2 , length):
if (arr[i] = = 1 ):
map [ 0 ] + = 1
continue
indPow = i
p = 1
while (indPow < = limit and indPow < = arr[i]):
if (arr[i] = = indPow):
map [p] + = 1
break
p + = 1
indPow * = i
map [ 0 ] + = 1
maxFreq = 0
for it in range ( len ( map )):
power = map [it]
if (arr[ 0 ] = = 0 and power ! = 0 ):
maxFreq = max (maxFreq,
map [power] + 1 )
else :
maxFreq = max (maxFreq,
map [power])
if (arr[ 1 ] = = 1 ):
return maxFreq + 1
return maxFreq
if __name__ = = "__main__" :
arr = [ 0 , 1 , 1 , 9 , 1 , 25 ]
print (indPowEqualsEle(arr))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static int indPowEqualsEle( int [] arr)
{
int len = arr.Length;
Dictionary< int , int > map
= new Dictionary< int , int >();
long limit = ( long ) int .MaxValue;
for ( int i = 2; i < len; i++) {
if (arr[i] == 1) {
if (map.ContainsKey(0))
map[0] = map[0]+1;
else
map.Add(0, 1);
continue ;
}
long indPow = i;
int p = 1;
while (indPow <= limit
&& indPow <= arr[i]) {
if (arr[i] == indPow) {
if (map.ContainsKey(p))
map[p] = map[p]+1;
else
map.Add(p, 1);
break ;
}
p++;
indPow *= i;
}
}
if (map.ContainsKey(0))
map[0] = map[0]+1;
else
map.Add(0, 1);
int maxFreq = 0;
foreach ( int power in map.Keys) {
if (arr[0] == 0 && power != 0) {
maxFreq
= Math.Max(maxFreq,
map[power] + 1);
}
else {
maxFreq = Math.Max(maxFreq,
map[power]);
}
}
return arr[1] == 1
? maxFreq + 1
: maxFreq;
}
public static void Main(String[] args)
{
int [] arr = { 0, 1, 1, 9, 1, 25 };
Console.WriteLine(indPowEqualsEle(arr));
}
}
|
Javascript
<script>
function indPowEqualsEle(arr) {
let len = arr.length;
let map = new Map();
let limit = Number.MAX_SAFE_INTEGER;
for (let i = 2; i < len; i++) {
if (arr[i] == 1) {
if (map.has(0)) {
map.set(0, map.get(0) + 1)
} else {
map.set(0, 1)
}
continue ;
}
let indPow = i;
let p = 1;
while (indPow <= limit && indPow <= arr[i]) {
if (arr[i] == indPow) {
if (map.has(p)) {
map.set(p, map.get(p) + 1)
} else {
map.set(p, 1)
}
break ;
}
p++;
indPow *= i;
}
}
if (map.has(0)) {
map.set(0, map.get(0) + 1)
} else {
map.set(0, 1)
}
let maxFreq = 0;
for (let power of map.keys()) {
if (arr[0] == 0 && power != 0) {
maxFreq = Math.max(maxFreq,
map.get(power) + 1);
}
else {
maxFreq = Math.max(maxFreq,
map.get(power));
}
}
return arr[1] == 1
? maxFreq + 1
: maxFreq;
}
let arr = [0, 1, 1, 9, 1, 25];
document.write((indPowEqualsEle(arr)))
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...