codeparrot/xlcost-text-to-code huggingface dataset:
Length of largest subarray whose all elements are Perfect Number | C ++ program to find the length of the largest sub - array of an array every element of whose is a perfect number ; Function that returns true if n is perfect ; Variable to store sum of divisors ; Find all divisors and add them ; Check if sum of divisors is equal to n , then n is a perfect number ; Function to return the length of the largest sub - array of an array every element of whose is a perfect number ; Check if arr [ i ] is a perfect number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfect ( long long int n ) { long long int sum = 1 ; for ( long long int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i * i != n ) sum = sum + i + n / i ; else sum = sum + i ; } } if ( sum == n && n != 1 ) return true ; return false ; } int contiguousPerfectNumber ( int arr [ ] , int n ) { int current_length = 0 ; int max_length = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPerfect ( arr [ i ] ) ) current_length ++ ; else current_length = 0 ; max_length = max ( max_length , current_length ) ; } return max_length ; } int main ( ) { int arr [ ] = { 1 , 7 , 36 , 4 , 6 , 28 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << contiguousPerfectNumber ( arr , n ) ; return 0 ; }
Length of largest subarray whose all elements Powerful number | C ++ program to find the length of the largest sub - array of an array every element of whose is a powerful number ; Function to check if the number is powerful ; First divide the number repeatedly by 2 ; If only 2 ^ 1 divides n ( not higher powers ) , then return false ; If n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n ( not higher powers ) , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Function to return the length of the largest sub - array of an array every element of whose is a powerful number ; If arr [ i ] is a Powerful number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPowerful ( int n ) { while ( n % 2 == 0 ) { int power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( int factor = 3 ; factor <= sqrt ( n ) ; factor += 2 ) { int power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } int contiguousPowerfulNumber ( int arr [ ] , int n ) { int current_length = 0 ; int max_length = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPowerful ( arr [ i ] ) ) current_length ++ ; else current_length = 0 ; max_length = max ( max_length , current_length ) ; } return max_length ; } int main ( ) { int arr [ ] = { 1 , 7 , 36 , 4 , 6 , 28 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << contiguousPowerfulNumber ( arr , n ) ; return 0 ; }
Maximize sum of K corner elements in Array | C ++ program to maximize the sum of K elements in the array by taking only corner elements ; Function to return maximum sum ; Base case ; Pick the start index ; Pick the end index ; Recursive function call ; Return the final answer ; Function to find the maximized sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSum ( int arr [ ] , int K , int start , int end , int max_sum ) { if ( K == 0 ) return max_sum ; int max_sum_start = max_sum + arr [ start ] ; int max_sum_end = max_sum + arr [ end ] ; int ans = max ( maxSum ( arr , K - 1 , start + 1 , end , max_sum_start ) , maxSum ( arr , K - 1 , start , end - 1 , max_sum_end ) ) ; return ans ; } void maximizeSum ( int arr [ ] , int K , int n ) { int max_sum = 0 ; int start = 0 ; int end = n - 1 ; cout << maxSum ( arr , K , start , end , max_sum ) ; } int main ( ) { int arr [ ] = { 8 , 4 , 4 , 8 , 12 , 3 , 2 , 9 } ; int K = 3 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maximizeSum ( arr , K , n ) ; return 0 ; }
Maximize sum of K corner elements in Array | C ++ program to maximize the sum of K elements in the array by taking only corner elements ; Function to return maximum sum ; Initialize variables ; Iterate over first K elements of array and update the value for curr_points ; Update value for max_points ; j points to the end of the array ; Return the final result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxPointCount ( int arr [ ] , int K , int size ) { int curr_points = 0 ; int max_points = 0 ; for ( int i = 0 ; i < K ; i ++ ) curr_points += arr [ i ] ; max_points = curr_points ; int j = size - 1 ; for ( int i = K - 1 ; i >= 0 ; i -- ) { curr_points = curr_points + arr [ j ] - arr [ i ] ; max_points = max ( curr_points , max_points ) ; j -- ; } return max_points ; } int main ( ) { int arr [ ] = { 8 , 4 , 4 , 8 , 12 , 3 , 2 , 9 } ; int K = 3 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxPointCount ( arr , K , n ) ; return 0 ; }
Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9 | C ++ implementation to find the sum of all the N digit palindromic numbers divisible by 9 ; Function for finding count of N digits palindrome which are divisible by 9 ; if N is odd ; if N is even ; Function for finding sum of N digits palindrome which are divisible by 9 ; count the possible number of palindrome ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPalindrome ( int n ) { int count ; if ( n % 2 == 1 ) { count = pow ( 9 , ( n - 1 ) / 2 ) ; } else { count = pow ( 9 , ( n - 2 ) / 2 ) ; } return count ; } int sumPalindrome ( int n ) { int count = countPalindrome ( n ) ; int res = 0 ; if ( n == 1 ) return 9 ; if ( n == 2 ) return 99 ; for ( int i = 0 ; i < n ; i ++ ) { res = res * 10 + count * 5 ; } return res ; } int main ( ) { int n = 3 ; cout << sumPalindrome ( n ) ; return 0 ; }
Find the total count of numbers up to N digits in a given base B | C ++ implementation to find the count of natural numbers upto N digits ; Function to return the count of natural numbers upto N digits ; Loop to iterate from 1 to N and calculating number of natural numbers for every ' i ' th digit . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( int N , int B ) { int sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { sum += ( B - 1 ) * pow ( B , i - 1 ) ; } return sum ; } int main ( ) { int N = 2 , B = 10 ; cout << count ( N , B ) ; return 0 ; }
Find smallest perfect square number A such that N + A is also a perfect square number | C ++ code to find out the smallest perfect square X which when added to N yields another perfect square number . ; X is the smallest perfect square number ; Loop from 1 to square root of N ; Condition to check whether i is factor of N or not ; Condition to check whether factors satisfies the equation or not ; Stores minimum value ; Return if X * X if X is not equal to 1e9 else return - 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long SmallestPerfectSquare ( long N ) { long X = ( long ) 1e9 ; long ans ; for ( int i = 1 ; i < sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { long a = i ; long b = N / i ; if ( ( b - a != 0 ) && ( ( b - a ) % 2 == 0 ) ) { X = min ( X , ( b - a ) / 2 ) ; } } } if ( X != 1e9 ) ans = X * X ; else ans = -1 ; return ans ; } int main ( ) { long N = 3 ; cout << SmallestPerfectSquare ( N ) ; return 0 ; }
Minimum Cost Path to visit all nodes situated at the Circumference of Circular Road | C ++ implementation to find the Minimum Cost Path to visit all nodes situated at the Circumference of Circular Road ; Function to find the minimum cost ; Sort the given array ; Initialise a new array of double size ; Fill the array elements ; Find the minimum path cost ; Return the final result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCost ( int arr [ ] , int n , int circumference ) { sort ( arr , arr + n ) ; int arr2 [ 2 * n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr2 [ i ] = arr [ i ] ; arr2 [ i + n ] = arr [ i ] + circumference ; } int res = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) res = min ( res , arr2 [ i + ( n - 1 ) ] - arr2 [ i ] ) ; return res ; } int main ( ) { int arr [ ] = { 19 , 3 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int circumference = 20 ; cout << minCost ( arr , n , circumference ) ; return 0 ; }
Unique element in an array where all elements occur K times except one | Set 2 | CPP program for the above approach ; Function to find single occurrence element ; By shifting 1 to left ith time and taking and with 1 will give us that ith bit of a [ j ] is 1 or 0 ; Taking modulo of p with k ; Generate result ; Loop for negative numbers ; Check if the calculated value res is present in array , then mark c = 1 and if c = 1 return res else res must be - ve ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findunique ( vector < int > & a , int k ) { int res = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { int p = 0 ; for ( int j = 0 ; j < a . size ( ) ; j ++ ) { p += ( abs ( a [ j ] ) & ( 1 << i ) ) != 0 ? 1 : 0 ; } p %= k ; res += pow ( 2 , i ) * p ; } int c = 0 ; for ( auto x : a ) if ( x == res ) { c = 1 ; break ; } return c == 1 ? res : - res ; } int main ( ) { vector < int > a = { 12 , 12 , 2 , 2 , 3 } ; int k = 2 ; cout << findunique ( a , k ) << " STRNEWLINE " ; }
Find minimum GCD of all pairs in an array | C ++ program to find the minimum GCD of any pair in the array ; Function returns the Minimum GCD of any pair ; Finding GCD of all the elements in the array . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinimumGCD ( int arr [ ] , int n ) { int g = 0 ; for ( int i = 0 ; i < n ; i ++ ) { g = __gcd ( g , arr [ i ] ) ; } return g ; } int main ( ) { int arr [ ] = { 2 , 4 , 6 , 8 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << MinimumGCD ( arr , N ) << endl ; }
Minimum steps to reach the Nth stair in jumps of perfect power of 2 | C ++ program for the above approach ; Function to count the number of jumps required to reach Nth stairs . ; Till N becomes 0 ; Removes the set bits from the right to left ; Driver Code ; Number of stairs ; Function Call
#include " bits / stdc + + . h " NEW_LINE using namespace std ; int stepRequired ( int N ) { int cnt = 0 ; while ( N ) { N = N & ( N - 1 ) ; cnt ++ ; } return cnt ; } int main ( ) { int N = 23 ; cout << stepRequired ( N ) ; return 0 ; }
Count of total subarrays whose sum is a Fibonacci Numbers | C ++ program for the above approach ; Function to check whether a number is perfect square or not ; Function to check whether a number is fibonacci number or not ; If 5 * n * n + 4 or 5 * n * n - 5 is a perfect square , then the number is Fibonacci ; Function to count the subarray with sum fibonacci number ; Traverse the array arr [ ] to find the sum of each subarray ; To store the sum ; Check whether sum of subarray between [ i , j ] is fibonacci or not ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfectSquare ( int x ) { int s = sqrt ( x ) ; return ( s * s == x ) ; } bool isFibonacci ( int n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } void fibonacciSubarrays ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int sum = 0 ; for ( int j = i ; j < n ; ++ j ) { sum += arr [ j ] ; if ( isFibonacci ( sum ) ) { ++ count ; } } } cout << count ; } int main ( ) { int arr [ ] = { 6 , 7 , 8 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; fibonacciSubarrays ( arr , n ) ; return 0 ; }
Count the number of ways to fill K boxes with N distinct items | C ++ program to calculate the above formula ; To store the factorials of all numbers ; Function to calculate factorial of all numbers ; Calculate x to the power y in O ( log n ) time ; Function to find inverse mod of a number x ; Calculate ( n C r ) ; Loop to compute the formula evaluated ; Add even power terms ; Subtract odd power terms ; Choose the k boxes which were used ; Driver code
#include <bits/stdc++.h> NEW_LINE #define mod  1000000007 NEW_LINE #define int  long long NEW_LINE using namespace std ; int factorial [ 100005 ] ; void StoreFactorials ( int n ) { factorial [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { factorial [ i ] = ( i * factorial [ i - 1 ] ) % mod ; } } int Power ( int x , int y ) { int ans = 1 ; while ( y > 0 ) { if ( y % 2 == 1 ) { ans = ( ans * x ) % mod ; } x = ( x * x ) % mod ; y /= 2 ; } return ans ; } int invmod ( int x ) { return Power ( x , mod - 2 ) ; } int nCr ( int n , int r ) { return ( factorial [ n ] * invmod ( ( factorial [ r ] * factorial [ n - r ] ) % mod ) ) % mod ; } int CountWays ( int n , int k ) { StoreFactorials ( n ) ; int ans = 0 ; for ( int i = k ; i >= 0 ; i -- ) { if ( i % 2 == k % 2 ) { ans = ( ans + ( Power ( i , n ) * nCr ( k , i ) ) % mod ) % mod ; } else { ans = ( ans + mod - ( Power ( i , n ) * nCr ( k , i ) ) % mod ) % mod ; } } ans = ( ans * nCr ( n , k ) ) % mod ; return ans ; } signed main ( ) { int N = 5 ; int K = 5 ; cout << CountWays ( N , K ) << " STRNEWLINE " ; return 0 ; }
Program to print numbers from N to 1 in reverse order | C ++ program to print all numbers between 1 to N in reverse order ; Recursive function to print from N to 1 ; if N is less than 1 then return void function ; recursive call of the function ; Driven Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void PrintReverseOrder ( int N ) { if ( N <= 0 ) { return ; } else { cout << N << " ▁ " ; PrintReverseOrder ( N - 1 ) ; } } int main ( ) { int N = 5 ; PrintReverseOrder ( N ) ; return 0 ; }
Find the XOR of first half and second half elements of an array | C ++ program to find the xor of the first half elements and second half elements of an array ; Function to find the xor of the first half elements and second half elements of an array ; xor of elements in FirstHalfXOR ; xor of elements in SecondHalfXOR ; Driver Code ; Function call
#include <iostream> NEW_LINE using namespace std ; void XOROfElements ( int arr [ ] , int n ) { int FirstHalfXOR = 0 ; int SecondHalfXOR = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n / 2 ) FirstHalfXOR ^= arr [ i ] ; else SecondHalfXOR ^= arr [ i ] ; } cout << FirstHalfXOR << " , " << SecondHalfXOR << endl ; } int main ( ) { int arr [ ] = { 20 , 30 , 50 , 10 , 55 , 15 , 42 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; XOROfElements ( arr , N ) ; return 0 ; }
Minimum volume of cone that can be circumscribed about a sphere of radius R | C ++ program to find the minimum volume of the cone that can be circumscribed about a sphere of radius R ; Function to find the volume of the cone ; r = radius of cone h = height of cone Volume of cone = ( 1 / 3 ) * ( 3.14 ) * ( r * r ) * ( h ) we get radius of cone from the derivation is root ( 2 ) times multiple of R we get height of cone from the derivation is 4 times multiple of R ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float Volume_of_cone ( float R ) { float V = ( 1 / 3.0 ) * ( 3.14 ) * ( 2 * ( R * R ) ) * ( 4 * R ) ; return V ; } int main ( ) { float R = 10.0 ; cout << Volume_of_cone ( R ) ; }
Program to find Surface Area and Volume of Octagonal Prism | C ++ program to find the Surface area and volume of octagonal prism ; Function to find the Volume of octagonal prism ; Formula to calculate volume = ( area * h ) ; Display volume ; Function to find the surface area of octagonal prism ; Formula to calculate Surface area ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void find_volume ( float area , float h ) { float Volume = ( area * h ) ; cout << " Volume : ▁ " << Volume << endl ; } void find_Surface_area ( float area , float a , float h ) { float Surface_area = ( 2 * area ) + ( 8 * a * h ) ; cout << " Surface ▁ area : ▁ " << Surface_area << endl ; } int main ( ) { float h = 1 ; float a = 6 ; float d = 2 ; float area = 2 * a * d ; find_Surface_area ( area , a , h ) ; find_volume ( area , h ) ; return 0 ; }
Count of pairs upto N such whose LCM is not equal to their product for Q queries | C ++ program to find the count of pairs from 1 to N such that their LCM is not equal to their product ; To store Euler 's Totient Function ; To store prefix sum table ; Compute Totients of all numbers smaller than or equal to N ; Make phi [ 1 ] = 0 since 1 cannot form any pair ; Initialise all remaining phi [ ] with i ; Compute remaining phi ; If phi [ p ] is not computed already , then number p is prime ; phi of prime number is p - 1 ; Update phi of all multiples of p ; Add the contribution of p to its multiple i by multiplying it with ( 1 - 1 / p ) ; Function to store prefix sum table ; Prefix Sum of all Euler 's Totient Values ; Total number of pairs that can be formed ; Driver Code ; Function call to compute all phi ; Function call to store all prefix sum
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100005 NEW_LINE int phi [ N ] ; int pref [ N ] ; void precompute ( ) { phi [ 1 ] = 0 ; for ( int i = 2 ; i < N ; i ++ ) phi [ i ] = i ; for ( int p = 2 ; p < N ; p ++ ) { if ( phi [ p ] == p ) { phi [ p ] = p - 1 ; for ( int i = 2 * p ; i < N ; i += p ) { phi [ i ] = ( phi [ i ] / p ) * ( p - 1 ) ; } } } } void prefix ( ) { for ( int i = 1 ; i < N ; i ++ ) pref [ i ] = pref [ i - 1 ] + phi [ i ] ; } void find_pairs ( int n ) { int total = ( n * ( n - 1 ) ) / 2 ; int ans = total - pref [ n ] ; cout << " Number ▁ of ▁ pairs ▁ from ▁ 1 ▁ to ▁ " << n << " ▁ are ▁ " << ans << endl ; } int main ( ) { precompute ( ) ; prefix ( ) ; int q [ ] = { 5 , 7 } ; int n = sizeof ( q ) / sizeof ( q [ 0 ] ) ; for ( int i = 0 ; i < n ; i ++ ) { find_pairs ( q [ i ] ) ; } return 0 ; }
Program to balance the given Chemical Equation | C ++ program to balance the given Chemical Equation ; Function to calculate GCD ; Function to calculate b1 , b2 and b3 ; Variable declaration ; temp variable to store gcd ; Computing GCD ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } void balance ( int x , int y , int p , int q ) { int b1 , b2 , b3 ; if ( p % x == 0 && q % y == 0 ) { b1 = p / x ; b2 = q / y ; b3 = 1 ; } else { p = p * y ; q = q * x ; b3 = x * y ; int temp = gcd ( p , gcd ( q , b3 ) ) ; b1 = p / temp ; b2 = q / temp ; b3 = b3 / temp ; } cout << b1 << " ▁ " << b2 << " ▁ " << b3 << endl ; } int main ( ) { int x = 2 , y = 3 , p = 4 , q = 5 ; balance ( x , y , p , q ) ; }
Find the Maximum Alternate Subsequence Sum from a given array | C ++ program to find the maximum alternating subsequence sum for a given array ; Function to find maximum alternating subsequence sum ; Initialize sum to 0 ; Calculate the sum ; Return the final result ; Driver Code ; Array initialization ; Length of array
#include <iostream> NEW_LINE using namespace std ; int maxAlternatingSum ( int arr [ ] , int n ) { int max_sum = 0 ; int i = 0 ; while ( i < n ) { int current_max = arr [ i ] ; int k = i ; while ( k < n && ( ( arr [ i ] > 0 && arr [ k ] > 0 ) || ( arr [ i ] < 0 && arr [ k ] < 0 ) ) ) { current_max = max ( current_max , arr [ k ] ) ; k += 1 ; } max_sum += current_max ; i = k ; } return max_sum ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , -1 , -2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxAlternatingSum ( arr , n ) ; return 0 ; }
Minimum length subarray containing all unique elements after Q operations | C ++ implementation to find the minimum size subarray containing all unique elements after processing the array for K queries of ranges ; Function to find minimum size subarray of all array elements ; Updating the array after processing each query ; Making it to 0 - indexing ; Prefix sum array concept is used to obtain the count array ; Iterating over the array to get the final array ; Variable to get count of all unique elements ; Hash to maintain perviously occurred elements ; Loop to find the all unique elements ; array to maintain counter of encountered elements ; variable to store answer ; Using two pointers approach ; Increment counter if occurred once ; when all unique elements are found ; update answer with minimum size ; decrement count of elements from left ; decrement counter ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int subarrayLength ( int A [ ] , int R [ ] [ 2 ] , int N , int M ) { for ( int i = 0 ; i < M ; ++ i ) { int l = R [ i ] [ 0 ] , r = R [ i ] [ 1 ] + 1 ; l -- ; r -- ; A [ l ] ++ ; if ( r < N ) A [ r ] -- ; } for ( int i = 1 ; i < N ; ++ i ) { A [ i ] += A [ i - 1 ] ; } int count = 0 ; unordered_set < int > s ; for ( int i = 0 ; i < N ; ++ i ) { if ( s . find ( A [ i ] ) == s . end ( ) ) count ++ ; s . insert ( A [ i ] ) ; } vector < int > repeat ( count + 1 , 0 ) ; int ans = N ; int counter = 0 , left = 0 , right = 0 ; while ( right < N ) { int cur_element = A [ right ] ; repeat [ cur_element ] += 1 ; if ( repeat [ cur_element ] == 1 ) ++ counter ; while ( counter == count ) { ans = min ( ans , right - left + 1 ) ; cur_element = A [ left ] ; repeat [ cur_element ] -= 1 ; ++ left ; if ( repeat [ cur_element ] == 0 ) -- counter ; } ++ right ; } return ans ; } int main ( ) { int N = 8 , queries = 6 ; int Q [ ] [ 2 ] = { { 1 , 4 } , { 3 , 4 } , { 4 , 5 } , { 5 , 5 } , { 7 , 8 } , { 8 , 8 } } ; int A [ N ] = { 0 } ; cout << subarrayLength ( A , Q , N , queries ) ; return 0 ; }
Elements of Array which can be expressed as power of prime numbers | C ++ program to print all elements of Array which can be expressed as power of prime numbers ; Function to mark all the exponent of prime numbers ; If number is prime then marking all of its exponent true ; Function to display all required elements ; Function to print the required numbers ; To find the largest number ; Function call to mark all the Exponential prime nos . ; Function call ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void ModifiedSieveOfEratosthenes ( int N , bool Expo_Prime [ ] ) { bool primes [ N ] ; memset ( primes , true , sizeof ( primes ) ) ; for ( int i = 2 ; i < N ; i ++ ) { if ( primes [ i ] ) { int no = i ; while ( no <= N ) { Expo_Prime [ no ] = true ; no *= i ; } for ( int j = i * i ; j < N ; j += i ) primes [ j ] = false ; } } } void Display ( int arr [ ] , bool Expo_Prime [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( Expo_Prime [ arr [ i ] ] ) cout << arr [ i ] << " ▁ " ; } void FindExpoPrime ( int arr [ ] , int n ) { int max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( max < arr [ i ] ) max = arr [ i ] ; } bool Expo_Prime [ max + 1 ] ; memset ( Expo_Prime , false , sizeof ( Expo_Prime ) ) ; ModifiedSieveOfEratosthenes ( max + 1 , Expo_Prime ) ; Display ( arr , Expo_Prime , n ) ; } int main ( ) { int arr [ ] = { 4 , 6 , 9 , 16 , 1 , 3 , 12 , 36 , 625 , 1000 } ; int n = sizeof ( arr ) / sizeof ( int ) ; FindExpoPrime ( arr , n ) ; return 0 ; }
Elements of Array which can be expressed as power of some integer to given exponent K | C ++ 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 ; Smaller eps , denotes more accuracy ; Initializing difference between two roots by INT_MAX ; x ^ K denotes current value of x ; loop untill we reach desired accuracy ; calculating current value from previous value by newton 's method ; Function to check whether its k root is an integer or not ; Function to find the numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE 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 ; }