# Elements of Array which can be expressed as power of some integer to given exponent K

• Last Updated : 15 Jun, 2021

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 = 36
Input: 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.

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.

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 ``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);` `    ``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`

## Javascript

 ``
Output:
`46656 64 729`

My Personal Notes arrow_drop_up