Given an array arr[] of size N, and an integer K, the task is to print all the elements of the Array which can be expressed as a power of some integer (X) to the exponent K, i.e. XK.
Examples:
Input: arr[] = {46656, 64, 256, 729, 16, 1000}, K = 6
Output: 46656 64 729
Explanation:
Only numbers 46656, 64, 729 can be expressed as a power of 6.
46656 = 66,
64 = 26,
729 = 36Input: arr[] = {23, 81, 256, 125, 16, 1000}, K = 4
Output: 81 256 16
Explanation:
The number 81, 256, 16 can be expressed as a power of 4.
Approach: To solve the problem mentioned above the main idea is to for each number in the Array, find the N-th root of a number. Then check whether this number is an integer or not. If yes, then print it, else skip to the next number.
Below is the implementation of the above approach:
CPP
// C++ implementation to print elements of // the Array which can be expressed as // power of some integer to given exponent K #include <bits/stdc++.h> using namespace std; #define ll long long // Method returns Nth power of A double nthRoot(ll A, ll N) { double xPre = 7; // Smaller eps, denotes more accuracy double eps = 1e-3; // Initializing difference between two // roots by INT_MAX double delX = INT_MAX; // x^K denotes current value of x double xK; // loop untill we reach desired accuracy while (delX > eps) { // calculating current value from previous // value by newton's method xK = ((N - 1.0) * xPre + (double)A / pow(xPre, N - 1)) / (double)N; delX = abs(xK - xPre); xPre = xK; } return xK; } // Function to check // whether its k root // is an integer or not bool check(ll no, int k) { double kth_root = nthRoot(no, k); ll num = kth_root; if (abs(num - kth_root) < 1e-4) return true; return false; } // Function to find the numbers void printExpo(ll arr[], int n, int k) { for (int i = 0; i < n; i++) { if (check(arr[i], k)) cout << arr[i] << " "; } } // Driver code int main() { int K = 6; ll arr[] = { 46656, 64, 256, 729, 16, 1000 }; int n = sizeof(arr) / sizeof(arr[0]); printExpo(arr, n, K); return 0; } |
Java
// Java implementation to print elements of // the Array which can be expressed as // power of some integer to given exponent K class GFG{ // Method returns Nth power of A static double nthRoot(long A, long N) { double xPre = 7; // Smaller eps, denotes more accuracy double eps = 1e-3; // Initializing difference between two // roots by Integer.MAX_VALUE double delX = Integer.MAX_VALUE; // x^K denotes current value of x double xK = 0; // loop untill we reach desired accuracy while (delX > eps) { // calculating current value from previous // value by newton's method xK = ((N - 1.0) * xPre + (double)A / Math.pow(xPre, N - 1)) / (double)N; delX = Math.abs(xK - xPre); xPre = xK; } return xK; } // Function to check // whether its k root // is an integer or not static boolean check(long no, int k) { double kth_root = nthRoot(no, k); long num = (long) kth_root; if (Math.abs(num - kth_root) < 1e-4) return true; return false; } // Function to find the numbers static void printExpo(long arr[], int n, int k) { for (int i = 0; i < n; i++) { if (check(arr[i], k)) System.out.print(arr[i]+ " "); } } // Driver code public static void main(String[] args) { int K = 6; long arr[] = { 46656, 64, 256, 729, 16, 1000 }; int n = arr.length; printExpo(arr, n, K); } } // This code is contributed by sapnasingh4991 |
Python3
# Python3 implementation to prelements of # the Array which can be expressed as # power of some integer to given exponent K # Method returns Nth power of A def nthRoot(A, N): xPre = 7 # Smaller eps, denotes more accuracy eps = 1e-3 # Initializing difference between two # roots by INT_MAX delX = 10**9 # x^K denotes current value of x xK = 0 # loop untiwe reach desired accuracy while (delX > eps): # calculating current value from previous # value by newton's method xK = ((N - 1.0) * xPre+ A /pow(xPre, N - 1))/ N delX = abs(xK - xPre) xPre = xK return xK # Function to check # whether its k root # is an integer or not def check(no, k): kth_root = nthRoot(no, k) num = int(kth_root) if (abs(num - kth_root) < 1e-4): return True return False # Function to find the numbers def printExpo(arr, n, k): for i in range(n): if (check(arr[i], k)): print(arr[i],end=" ") # Driver code if __name__ == '__main__': K = 6 arr = [46656, 64, 256,729, 16, 1000] n = len(arr) printExpo(arr, n, K) # This code is contributed by mohit kumar 29 |
C#
// C# implementation to print elements of // the Array which can be expressed as // power of some integer to given exponent K using System; class GFG{ // Method returns Nth power of A static double nthRoot(long A, long N) { double xPre = 7; // Smaller eps, denotes more accuracy double eps = 1e-3; // Initializing difference between two // roots by int.MaxValue double delX = int.MaxValue; // x^K denotes current value of x double xK = 0; // loop untill we reach desired accuracy while (delX > eps) { // calculating current value from previous // value by newton's method xK = ((N - 1.0) * xPre + (double)A / Math.Pow(xPre, N - 1)) / (double)N; delX = Math.Abs(xK - xPre); xPre = xK; } return xK; } // Function to check // whether its k root // is an integer or not static bool check(long no, int k) { double kth_root = nthRoot(no, k); long num = (long) kth_root; if (Math.Abs(num - kth_root) < 1e-4) return true; return false; } // Function to find the numbers static void printExpo(long []arr, int n, int k) { for (int i = 0; i < n; i++) { if (check(arr[i], k)) Console.Write(arr[i]+ " "); } } // Driver code public static void Main(String[] args) { int K = 6; long []arr = { 46656, 64, 256, 729, 16, 1000 }; int n = arr.Length; printExpo(arr, n, K); } } // This code is contributed by Princi Singh |
46656 64 729
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.
Recommended Posts:
- Elements of Array which can be expressed as power of prime numbers
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Check if a number can be expressed as power | Set 2 (Using Log)
- Check if a number can be expressed as x^y (x raised to power y)
- Check if an integer can be expressed as a sum of two semi-primes
- Count of primes below N which can be expressed as the sum of two primes
- Check if the given array contains all the divisors of some integer
- Count of primes in a given range that can be expressed as sum of perfect squares
- Check if a given number can be expressed as pair-sum of sum of first X natural numbers
- GCD of a number raised to some power and another number
- Check if a number can be expressed as a sum of consecutive numbers
- Check whether a number can be expressed as a product of single digit numbers
- Check if a number can be expressed as 2^x + 2^y
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check if a number can be expressed as sum two abundant numbers
- Count primes that can be expressed as sum of two consecutive primes and 1
- Check if N can be expressed as product of 3 distinct numbers
- Check if a number can be expressed as a product of exactly K prime divisors
- Check if a number N can be expressed in base B
- Check if a number can be expressed as sum of two Perfect powers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

