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++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find count of elements which
// are a non-negative power of their indices
int indPowEqualsEle(vector<int> arr)
{
// Length of the array
int len = arr.size();
// Initialize the hashmap to store
// the frequency of elements
unordered_map<int, int> map;
// Initialize maximum value
// of integer into long
long limit = INT_MAX;
// Iterate the array arr from index 2
for (int i = 2; i < len; i++)
{
// If current element is equal to 1
// then its equal to index power 0
if (arr[i] == 1)
{
// Increment the frequency of 0 by 1
map[0]++;
continue;
}
// Initialize a variable to index
// which is to be multiplied
// by the index
long indPow = i;
// Initialize a variable to
// store the power of the index
int p = 1;
while (indPow <= limit && indPow <= arr[i])
{
// Element is equal to
// a power of its index
if (arr[i] == indPow)
{
// Increment the frequency
// of p by 1
map[p]++;
break;
}
// Increment power
p++;
// Multiply current value with
// index to get the next power
indPow *= i;
}
}
// If arr[0] == 1, then increment
// the frequency of 0 in the hashmap
map[0]++;
// Initialize maxFreq to 0 to calculate
// maximum frequency of powers
int maxFreq = 0;
// Iterate the hashmap
for (auto it = map.begin(); it != map.end(); it++)
{
int power = it->second;
// If arr[0] == 0, then increment the
// frequency of all powers except 0
if (arr[0] == 0 && power != 0)
{
maxFreq = max(maxFreq,
map[power] + 1);
}
else
{
maxFreq = max(maxFreq,
map[power]);
}
}
// Increment the maximum frequency by 1
// If arr[1] is equal to 1
return arr[1] == 1
? maxFreq + 1
: maxFreq;
}
// Driver function
int main()
{
// Initialize an array
vector<int> arr = {0, 1, 1, 9, 1, 25};
// Call the function
// and print the answer
cout << (indPowEqualsEle(arr));
}
// This code is contributed by Potta Lokesh
Java
// Java implementation for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to find count of elements which
// are a non-negative power of their indices
public static int indPowEqualsEle(int[] arr)
{
// Length of the array
int len = arr.length;
// Initialize the hashmap to store
// the frequency of elements
Map<Integer, Integer> map
= new HashMap<>();
// Initialize maximum value
// of integer into long
long limit = (long)Integer.MAX_VALUE;
// Iterate the array arr from index 2
for (int i = 2; i < len; i++) {
// If current element is equal to 1
// then its equal to index power 0
if (arr[i] == 1) {
// Increment the frequency of 0 by 1
map.put(0,
map.getOrDefault(0, 0) + 1);
continue;
}
// Initialize a variable to index
// which is to be multiplied
// by the index
long indPow = i;
// Initialize a variable to
// store the power of the index
int p = 1;
while (indPow <= limit
&& indPow <= arr[i]) {
// Element is equal to
// a power of its index
if (arr[i] == indPow) {
// Increment the frequency
// of p by 1
map
.put(p,
map.getOrDefault(p, 0) + 1);
break;
}
// Increment power
p++;
// Multiply current value with
// index to get the next power
indPow *= i;
}
}
// If arr[0] == 1, then increment
// the frequency of 0 in the hashmap
map.put(0, map.getOrDefault(0, 0) + 1);
// Initialize maxFreq to 0 to calculate
// maximum frequency of powers
int maxFreq = 0;
// Iterate the hashmap
for (int power : map.keySet()) {
// If arr[0] == 0, then increment the
// frequency of all powers except 0
if (arr[0] == 0 && power != 0) {
maxFreq
= Math.max(maxFreq,
map.get(power) + 1);
}
else {
maxFreq = Math.max(maxFreq,
map.get(power));
}
}
// Increment the maximum frequency by 1
// If arr[1] is equal to 1
return arr[1] == 1
? maxFreq + 1
: maxFreq;
}
// Driver function
public static void main(String[] args)
{
// Initialize an array
int[] arr = { 0, 1, 1, 9, 1, 25 };
// Call the function
// and print the answer
System.out.println(indPowEqualsEle(arr));
}
}
Python3
# Python 3 code for the above approach
from collections import defaultdict
import sys
# Function to find count of elements which
# are a non-negative power of their indices
def indPowEqualsEle(arr):
# Length of the array
length = len(arr)
# Initialize the hashmap to store
# the frequency of elements
map = defaultdict(int)
# Initialize maximum value
# of integer into long
limit = sys.maxsize
# Iterate the array arr from index 2
for i in range(2, length):
# If current element is equal to 1
# then its equal to index power 0
if (arr[i] == 1):
# Increment the frequency of 0 by 1
map[0] += 1
continue
# Initialize a variable to index
# which is to be multiplied
# by the index
indPow = i
# Initialize a variable to
# store the power of the index
p = 1
while (indPow <= limit and indPow <= arr[i]):
# Element is equal to
# a power of its index
if (arr[i] == indPow):
# Increment the frequency
# of p by 1
map[p] += 1
break
# Increment power
p += 1
# Multiply current value with
# index to get the next power
indPow *= i
# If arr[0] == 1, then increment
# the frequency of 0 in the hashmap
map[0] += 1
# Initialize maxFreq to 0 to calculate
# maximum frequency of powers
maxFreq = 0
# Iterate the hashmap
for it in range(len(map)):
power = map[it]
# If arr[0] == 0, then increment the
# frequency of all powers except 0
if (arr[0] == 0 and power != 0):
maxFreq = max(maxFreq,
map[power] + 1)
else:
maxFreq = max(maxFreq,
map[power])
# Increment the maximum frequency by 1
# If arr[1] is equal to 1
if(arr[1] == 1):
return maxFreq + 1
return maxFreq
# Driver function
if __name__ == "__main__":
# Initialize an array
arr = [0, 1, 1, 9, 1, 25]
# Call the function
# and print the answer
print(indPowEqualsEle(arr))
# This code is contributed by ukasp.
C#
// C# implementation for the above approach
using System;
using System.Collections.Generic;
public class GFG {
// Function to find count of elements which
// are a non-negative power of their indices
public static int indPowEqualsEle(int[] arr)
{
// Length of the array
int len = arr.Length;
// Initialize the hashmap to store
// the frequency of elements
Dictionary<int, int> map
= new Dictionary<int, int>();
// Initialize maximum value
// of integer into long
long limit = (long)int.MaxValue;
// Iterate the array arr from index 2
for (int i = 2; i < len; i++) {
// If current element is equal to 1
// then its equal to index power 0
if (arr[i] == 1) {
// Increment the frequency of 0 by 1
if(map.ContainsKey(0))
map[0] = map[0]+1;
else
map.Add(0, 1);
continue;
}
// Initialize a variable to index
// which is to be multiplied
// by the index
long indPow = i;
// Initialize a variable to
// store the power of the index
int p = 1;
while (indPow <= limit
&& indPow <= arr[i]) {
// Element is equal to
// a power of its index
if (arr[i] == indPow) {
// Increment the frequency
// of p by 1
if(map.ContainsKey(p))
map[p] = map[p]+1;
else
map.Add(p, 1);
break;
}
// Increment power
p++;
// Multiply current value with
// index to get the next power
indPow *= i;
}
}
// If arr[0] == 1, then increment
// the frequency of 0 in the hashmap
if(map.ContainsKey(0))
map[0] = map[0]+1;
else
map.Add(0, 1);
// Initialize maxFreq to 0 to calculate
// maximum frequency of powers
int maxFreq = 0;
// Iterate the hashmap
foreach (int power in map.Keys) {
// If arr[0] == 0, then increment the
// frequency of all powers except 0
if (arr[0] == 0 && power != 0) {
maxFreq
= Math.Max(maxFreq,
map[power] + 1);
}
else {
maxFreq = Math.Max(maxFreq,
map[power]);
}
}
// Increment the maximum frequency by 1
// If arr[1] is equal to 1
return arr[1] == 1
? maxFreq + 1
: maxFreq;
}
// Driver function
public static void Main(String[] args)
{
// Initialize an array
int[] arr = { 0, 1, 1, 9, 1, 25 };
// Call the function
// and print the answer
Console.WriteLine(indPowEqualsEle(arr));
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript code for the above approach
// Function to find count of elements which
// are a non-negative power of their indices
function indPowEqualsEle(arr) {
// Length of the array
let len = arr.length;
// Initialize the hashmap to store
// the frequency of elements
let map = new Map();
// Initialize maximum value
// of integer into long
let limit = Number.MAX_SAFE_INTEGER;
// Iterate the array arr from index 2
for (let i = 2; i < len; i++) {
// If current element is equal to 1
// then its equal to index power 0
if (arr[i] == 1) {
// Increment the frequency of 0 by 1
if (map.has(0)) {
map.set(0, map.get(0) + 1)
} else {
map.set(0, 1)
}
continue;
}
// Initialize a variable to index
// which is to be multiplied
// by the index
let indPow = i;
// Initialize a variable to
// store the power of the index
let p = 1;
while (indPow <= limit && indPow <= arr[i]) {
// Element is equal to
// a power of its index
if (arr[i] == indPow) {
// Increment the frequency
// of p by 1
if (map.has(p)) {
map.set(p, map.get(p) + 1)
} else {
map.set(p, 1)
}
break;
}
// Increment power
p++;
// Multiply current value with
// index to get the next power
indPow *= i;
}
}
// If arr[0] == 1, then increment
// the frequency of 0 in the hashmap
if (map.has(0)) {
map.set(0, map.get(0) + 1)
} else {
map.set(0, 1)
}
// Initialize maxFreq to 0 to calculate
// maximum frequency of powers
let maxFreq = 0;
// Iterate the hashmap
for (let power of map.keys()) {
// If arr[0] == 0, then increment the
// frequency of all powers except 0
if (arr[0] == 0 && power != 0) {
maxFreq = Math.max(maxFreq,
map.get(power) + 1);
}
else {
maxFreq = Math.max(maxFreq,
map.get(power));
}
}
// Increment the maximum frequency by 1
// If arr[1] is equal to 1
return arr[1] == 1
? maxFreq + 1
: maxFreq;
}
// Driver function
// Initialize an array
let arr = [0, 1, 1, 9, 1, 25];
// Call the function
// and print the answer
document.write((indPowEqualsEle(arr)))
// This code is contributed by gfgking.
</script>
Time Complexity: O(N * log N)
Auxiliary Space: O(1)
Similar Reads
Find frequency of each element in a limited range array in less than O(n) time Given a sorted array arr[] of positive integers, the task is to find the frequency for each element in the array. Assume all elements in the array are less than some constant MNote: Do this without traversing the complete array. i.e. expected time complexity is less than O(n)Examples: Input: arr[] =
10 min read
Count of elements that are Kth powers of their indices in given Array Given an array arr[] with N non-negative integers, the task is to find the number of elements that are Kth powers of their indices, where K is a non-negative number. arr[i] = iK Example: Input: arr = [1, 1, 4, 3, 16, 125, 1], K = 0Output: 3Explanation: 3 elements are Kth powers of their indices:00 i
9 min read
Count array elements whose highest power of 2 less than or equal to that number is present in the given array Given an array arr[] consisting of N positive integers, the task is to find the number of array elements whose highest power of 2 less than or equal to that number is present in the array. Examples: Input: arr[] = {3, 4, 6, 9}Output: 2Explanation:There are 2 array elements (4 and 6), whose highest p
6 min read
Check if frequency of each element in given array is unique or not Given an array arr[] of N positive integers where the integers are in the range from 1 to N, the task is to check whether the frequency of the elements in the array is unique or not. If all the frequency is unique then print "Yes", else print "No". Examples: Input: N = 5, arr[] = {1, 1, 2, 5, 5}Outp
15 min read
Fill an array based on frequency where elements are in range from 0 to n-1 Given an array of positive integers with duplicate allowed. The array contains elements from range 0 to n-1. The task is to fill the array such that arr[i] contains the frequency of i. Examples : Input : arr[] = {1, 4, 3, 4, 1, 1, 4, 4, 4, 7} Output : arr[] = {0, 3, 0, 1, 5, 0, 0, 1, 0, 0} Here 0 ap
5 min read