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.
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 until 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 until 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 print elements 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 until we 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 until 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
<script> // Javascript implementation to print elements of // the Array which can be expressed as // power of some integer to given exponent K // Method returns Nth power of A function nthRoot(A,N) { let xPre = 7; // Smaller eps, denotes more accuracy let eps = 1e-3; // Initializing difference between two // roots by Integer.MAX_VALUE let delX = Number.MAX_VALUE; // x^K denotes current value of x let xK = 0; // loop until we reach desired accuracy while (delX > eps) { // calculating current value from previous // value by newton's method xK = ((N - 1.0) * xPre + A / Math.pow(xPre, N - 1)) / N; delX = Math.abs(xK - xPre); xPre = xK; } return xK; } // Function to check // whether its k root // is an integer or not function check(no,k) { let kth_root = nthRoot(no, k); let num = Math.floor(kth_root); if (Math.abs(num - kth_root) < 1e-4) return true ; return false ; } // Function to find the numbers function printExpo(arr,n,k) { for (let i = 0; i < n; i++) { if (check(arr[i], k)) document.write(arr[i]+ " " ); } } // Driver code let K = 6; let arr=[46656, 64, 256, 729, 16, 1000]; let n = arr.length; printExpo(arr, n, K); // This code is contributed by patel2127 </script> |
46656 64 729
Another Approach:
- The code defines a function “isPower” to check if a number is equal to a specific power of a base.
- The code defines a function “printPowerElements” to print elements in a vector that can be expressed as a specific power of a base.
- The “printPowerElements” function iterates through each element in the input vector.
- For each element, it checks if the element can be expressed as a power of a base value ranging from 2 to the square root of the element.
- It uses the “isPower” function to perform the check.
- If a matching power is found, the element is printed.
- In the “main” function, an example vector “arr” and a specific power “K” are declared.
- The “printPowerElements” function is called with “arr” and “K”.
- The program terminates after the function execution.
Below is the implementation of the above approach:
C++
#include <iostream> #include <vector> #include <cmath> // Function to check if a number is equal to a specific power of a base bool isPower( int num, int base, int power) { int result = pow (base, power); // Calculate the power of the base return result == num; // Check if the calculated result is equal to the number } // Function to print elements in the vector that are a specific power of a base void printPowerElements( const std::vector< int >& arr, int K) { for ( int i = 0; i < arr.size(); i++) { bool found = false ; // Flag to track if a matching power is found for ( int j = 2; j <= sqrt (arr[i]); j++) { // Check if the number in the array can be expressed as a power of 'j' with power 'K' if (isPower(arr[i], j, K)) { found = true ; // Found a matching power, set the flag to true break ; // No need to check for other 'j' values, exit the loop } } if (found) std::cout << arr[i] << " " ; // Print the number if a matching power is found } } int main() { std::vector< int > arr = {46656, 64, 256, 729, 16, 1000}; int K = 6; printPowerElements(arr, K); return 0; } |
Java
import java.util.ArrayList; import java.util.List; class Main { // Function to check if a number is equal to a specific // power of a base static boolean isPower( int num, int base, int power) { int result = ( int ) Math.pow(base, power); // Calculate the power of the base return result == num; // Check if the calculated result is equal to the number } // Function to print elements in the list that are a specific power of a base static void printPowerElements(List<Integer> arr, int K) { for ( int num : arr) { boolean found = false ; // Flag to track if a matching power is found for ( int j = 2 ; j <= Math.sqrt(num); j++) { // Check if the number in the list can be expressed as a power of 'j' with power 'K' if (isPower(num, j, K)) { found = true ; // Found a matching power, set the flag to true break ; // No need to check for other 'j' values, exit the loop } } if (found) { System.out.print(num + " " ); // Print the number if a matching power is found } } } public static void main(String[] args) { List<Integer> arr = new ArrayList<>(); arr.add( 46656 ); arr.add( 64 ); arr.add( 256 ); arr.add( 729 ); arr.add( 16 ); arr.add( 1000 ); int K = 6 ; printPowerElements(arr, K); //This Code Is Contributed By Shubham Tiwari. } } |
Python3
import math # Function to check if a number is equal to a specific power of a base def is_power(num, base, power): result = pow (base, power) # Calculate the power of the base return result = = num # Check if the calculated result is equal to the number # Function to print elements in the list that are a specific power of a base def print_power_elements(arr, K): for i in range ( len (arr)): found = False # Flag to track if a matching power is found for j in range ( 2 , int (math.sqrt(arr[i])) + 1 ): # Check if the number in the list can be expressed as a power of 'j' with power 'K' if is_power(arr[i], j, K): found = True # Found a matching power, set the flag to True break # No need to check for other 'j' values, exit the loop if found: print (arr[i], end = " " ) # Print the number if a matching power is found if __name__ = = "__main__" : arr = [ 46656 , 64 , 256 , 729 , 16 , 1000 ] K = 6 print_power_elements(arr, K) # This code is contributed by Shubham Tiwari. |
C#
using System; using System.Collections.Generic; class GFG { // Function to check if a number is equal to a specific power of a base static bool IsPower( int num, int baseNum, int power) { int result = ( int )Math.Pow(baseNum, power); // Calculate the power of the base return result == num; // Check if the calculated result is equal to the number } // Function to print elements in the list that are a specific power of a base static void PrintPowerElements(List< int > arr, int K) { foreach ( int num in arr) { bool found = false ; // Flag to track if a matching power is found for ( int j = 2; j <= Math.Sqrt(num); j++) { // Check if the number in the list can be expressed as a power of 'j' with power 'K' if (IsPower(num, j, K)) { found = true ; // Found a matching power, set the flag to true break ; // No need to check for other 'j' values, exit the loop } } if (found) Console.Write(num + " " ); // Print the number if a matching power is found } } static void Main( string [] args) { List< int > arr = new List< int > { 46656, 64, 256, 729, 16, 1000 }; int K = 6; PrintPowerElements(arr, K); } } //This code is Contributed By Shubham Tiwari |
Javascript
// Function to check if a number is equal to a specific power of a base function isPower(num, base, power) { let result = Math.pow(base, power); // Calculate the power of the base return result === num; // Check if the calculated result is equal to the number } // Function to print elements in the array that are a specific power of a base function printPowerElements(arr, K) { for (let i = 0; i < arr.length; i++) { let found = false ; // Flag to track if a matching power is found for (let j = 2; j <= Math.sqrt(arr[i]); j++) { // Check if the number in the array can be expressed as a power of 'j' with power 'K' if (isPower(arr[i], j, K)) { found = true ; // Found a matching power, set the flag to true break ; // No need to check for other 'j' values, exit the loop } } if (found) { console.log(arr[i] + " " ); // Print the number if a matching power is found } } } const arr = [46656, 64, 256, 729, 16, 1000]; const K = 6; printPowerElements(arr, K); // This Code Is Contributed By Shubham Tiwari |
46656 64 729
Time Complexity: O(n * sqrt(arr[i]))
Auxiliary Space: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!