Elements of Array which can be expressed as power of some integer to given exponent K
Last Updated :
04 Aug, 2023
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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
double nthRoot(ll A, ll N)
{
double xPre = 7;
double eps = 1e-3;
double delX = INT_MAX;
double xK;
while (delX > eps) {
xK = ((N - 1.0) * xPre
+ ( double )A / pow (xPre, N - 1))
/ ( double )N;
delX = abs (xK - xPre);
xPre = xK;
}
return xK;
}
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 ;
}
void printExpo(ll arr[], int n, int k)
{
for ( int i = 0; i < n; i++) {
if (check(arr[i], k))
cout << arr[i] << " " ;
}
}
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
class GFG{
static double nthRoot( long A, long N)
{
double xPre = 7 ;
double eps = 1e- 3 ;
double delX = Integer.MAX_VALUE;
double xK = 0 ;
while (delX > eps) {
xK = ((N - 1.0 ) * xPre
+ ( double )A / Math.pow(xPre, N - 1 ))
/ ( double )N;
delX = Math.abs(xK - xPre);
xPre = xK;
}
return xK;
}
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 ;
}
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]+ " " );
}
}
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);
}
}
|
Python3
def nthRoot(A, N):
xPre = 7
eps = 1e - 3
delX = 10 * * 9
xK = 0
while (delX > eps):
xK = ((N - 1.0 ) * xPre + A / pow (xPre, N - 1 )) / N
delX = abs (xK - xPre)
xPre = xK
return xK
def check(no, k):
kth_root = nthRoot(no, k)
num = int (kth_root)
if ( abs (num - kth_root) < 1e - 4 ):
return True
return False
def printExpo(arr, n, k):
for i in range (n):
if (check(arr[i], k)):
print (arr[i],end = " " )
if __name__ = = '__main__' :
K = 6
arr = [ 46656 , 64 , 256 , 729 , 16 , 1000 ]
n = len (arr)
printExpo(arr, n, K)
|
C#
using System;
class GFG{
static double nthRoot( long A, long N)
{
double xPre = 7;
double eps = 1e-3;
double delX = int .MaxValue;
double xK = 0;
while (delX > eps) {
xK = ((N - 1.0) * xPre
+ ( double )A / Math.Pow(xPre, N - 1))
/ ( double )N;
delX = Math.Abs(xK - xPre);
xPre = xK;
}
return xK;
}
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 ;
}
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]+ " " );
}
}
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);
}
}
|
Javascript
<script>
function nthRoot(A,N)
{
let xPre = 7;
let eps = 1e-3;
let delX = Number.MAX_VALUE;
let xK = 0;
while (delX > eps) {
xK = ((N - 1.0) * xPre
+ A / Math.pow(xPre, N - 1))
/ N;
delX = Math.abs(xK - xPre);
xPre = xK;
}
return xK;
}
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 printExpo(arr,n,k)
{
for (let i = 0; i < n; i++) {
if (check(arr[i], k))
document.write(arr[i]+ " " );
}
}
let K = 6;
let arr=[46656, 64, 256,
729, 16, 1000];
let n = arr.length;
printExpo(arr, n, K);
</script>
|
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>
bool isPower( int num, int base, int power) {
int result = pow (base, power);
return result == num;
}
void printPowerElements( const std::vector< int >& arr, int K) {
for ( int i = 0; i < arr.size(); i++) {
bool found = false ;
for ( int j = 2; j <= sqrt (arr[i]); j++) {
if (isPower(arr[i], j, K)) {
found = true ;
break ;
}
}
if (found)
std::cout << arr[i] << " " ;
}
}
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 {
static boolean isPower( int num, int base, int power) {
int result = ( int ) Math.pow(base, power);
return result == num;
}
static void printPowerElements(List<Integer> arr, int K) {
for ( int num : arr) {
boolean found = false ;
for ( int j = 2 ; j <= Math.sqrt(num); j++) {
if (isPower(num, j, K)) {
found = true ;
break ;
}
}
if (found) {
System.out.print(num + " " );
}
}
}
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);
}
}
|
Python3
import math
def is_power(num, base, power):
result = pow (base, power)
return result = = num
def print_power_elements(arr, K):
for i in range ( len (arr)):
found = False
for j in range ( 2 , int (math.sqrt(arr[i])) + 1 ):
if is_power(arr[i], j, K):
found = True
break
if found:
print (arr[i], end = " " )
if __name__ = = "__main__" :
arr = [ 46656 , 64 , 256 , 729 , 16 , 1000 ]
K = 6
print_power_elements(arr, K)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static bool IsPower( int num, int baseNum, int power)
{
int result = ( int )Math.Pow(baseNum, power);
return result == num;
}
static void PrintPowerElements(List< int > arr, int K)
{
foreach ( int num in arr)
{
bool found = false ;
for ( int j = 2; j <= Math.Sqrt(num); j++)
{
if (IsPower(num, j, K))
{
found = true ;
break ;
}
}
if (found)
Console.Write(num + " " );
}
}
static void Main( string [] args)
{
List< int > arr = new List< int > { 46656, 64, 256, 729, 16, 1000 };
int K = 6;
PrintPowerElements(arr, K);
}
}
|
Javascript
function isPower(num, base, power) {
let result = Math.pow(base, power);
return result === num;
}
function printPowerElements(arr, K) {
for (let i = 0; i < arr.length; i++) {
let found = false ;
for (let j = 2; j <= Math.sqrt(arr[i]); j++) {
if (isPower(arr[i], j, K)) {
found = true ;
break ;
}
}
if (found) {
console.log(arr[i] + " " );
}
}
}
const arr = [46656, 64, 256, 729, 16, 1000];
const K = 6;
printPowerElements(arr, K);
|
Time Complexity: O(n * sqrt(arr[i]))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...