Find highest frequency of non-negative powers that are same as indices of elements in given Array
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
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++
// 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 indicesint 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 functionint 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 approachimport 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)); }} |
Javascript
<script>// Javascript code for the above approach// Function to find count of elements which// are a non-negative power of their indicesfunction 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 arraylet arr = [0, 1, 1, 9, 1, 25];// Call the function// and print the answerdocument.write((indPowEqualsEle(arr)))// This code is contributed by gfgking.</script> |
4
Time Complexity: O(N * log N)
Auxiliary Space: O(1)


