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 2Input: 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 the power becomes equal to the element then check if its present in the hashmap:
- 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 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++ 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 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));
}
} |
# 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# 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 |
<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> |
4
Time Complexity: O(N * log N)
Auxiliary Space: O(1)