codeparrot/xlcost-text-to-code huggingface dataset:
Pairs of complete strings in two sets of strings | C ++ program to find count of complete pairs ; Returns count of complete pairs from set [ 0. . n - 1 ] and set2 [ 0. . m - 1 ] ; con_s1 [ i ] is going to store an integer whose set bits represent presence / absence of characters in string set1 [ i ] . Similarly con_s2 [ i ] is going to store an integer whose set bits represent presence / absence of characters in string set2 [ i ] ; Process all strings in set1 [ ] ; initializing all bits to 0 ; Setting the ascii code of char s [ i ] [ j ] to 1 in the compressed integer . ; Process all strings in set2 [ ] ; initializing all bits to 0 ; setting the ascii code of char s [ i ] [ j ] to 1 in the compressed integer . ; assigning a variable whose all 26 ( 0. . 25 ) bits are set to 1 ; Now consider every pair of integer in con_s1 [ ] and con_s2 [ ] and check if the pair is complete . ; if all bits are set , the strings are complete ! ; Driver code
#include <iostream> NEW_LINE using namespace std ; int countCompletePairs ( string set1 [ ] , string set2 [ ] , int n , int m ) { int result = 0 ; int con_s1 [ n ] , con_s2 [ m ] ; for ( int i = 0 ; i < n ; i ++ ) { con_s1 [ i ] = 0 ; for ( int j = 0 ; j < set1 [ i ] . length ( ) ; j ++ ) { con_s1 [ i ] = con_s1 [ i ] | ( 1 << ( set1 [ i ] [ j ] - ' a ' ) ) ; } } for ( int i = 0 ; i < m ; i ++ ) { con_s2 [ i ] = 0 ; for ( int j = 0 ; j < set2 [ i ] . length ( ) ; j ++ ) { con_s2 [ i ] = con_s2 [ i ] | ( 1 << ( set2 [ i ] [ j ] - ' a ' ) ) ; } } long long complete = ( 1 << 26 ) - 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( ( con_s1 [ i ] con_s2 [ j ] ) == complete ) result ++ ; } } return result ; } int main ( ) { string set1 [ ] = { " abcdefgh " , " geeksforgeeks " , " lmnopqrst " , " abc " } ; string set2 [ ] = { " ijklmnopqrstuvwxyz " , " abcdefghijklmnopqrstuvwxyz " , " defghijklmnopqrstuvwxyz " } ; int n = sizeof ( set1 ) / sizeof ( set1 [ 0 ] ) ; int m = sizeof ( set2 ) / sizeof ( set2 [ 0 ] ) ; cout << countCompletePairs ( set1 , set2 , n , m ) ; return 0 ; }
Find all strings that match specific pattern in a dictionary | C ++ program to print all the strings that match the given pattern where every character in the pattern is uniquely mapped to a character in the dictionary ; Function to encode given string ; for each character in given string ; If the character is occurring for the first time , assign next unique number to that char ; append the number associated with current character into the output string ; Function to print all the strings that match the given pattern where every character in the pattern is uniquely mapped to a character in the dictionary ; len is length of the pattern ; Encode the string ; for each word in the dictionary ; If size of pattern is same as size of current dictionary word and both pattern and the word has same hash , print the word ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string encodeString ( string str ) { unordered_map < char , int > map ; string res = " " ; int i = 0 ; for ( char ch : str ) { if ( map . find ( ch ) == map . end ( ) ) map [ ch ] = i ++ ; res += to_string ( map [ ch ] ) ; } return res ; } void findMatchedWords ( unordered_set < string > dict , string pattern ) { int len = pattern . length ( ) ; string hash = encodeString ( pattern ) ; for ( string word : dict ) { if ( word . length ( ) == len && encodeString ( word ) == hash ) cout << word << " ▁ " ; } } int main ( ) { unordered_set < string > dict = { " abb " , " abc " , " xyz " , " xyy " } ; string pattern = " foo " ; findMatchedWords ( dict , pattern ) ; return 0 ; }
Find all strings that match specific pattern in a dictionary | C ++ program to print all the strings that match the given pattern where every character in the pattern is uniquely mapped to a character in the dictionary ; Function to print all the strings that match the given pattern where every character in the pattern is uniquely mapped to a character in the dictionary ; len is length of the pattern ; for each word in the dictionary ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( string pattern , string word ) { if ( pattern . length ( ) != word . length ( ) ) return false ; char ch [ 128 ] = { 0 } ; int len = word . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( ch [ pattern [ i ] ] == 0 ) ch [ pattern [ i ] ] = word [ i ] ; else if ( ch [ pattern [ i ] ] != word [ i ] ) return false ; } return true ; } void findMatchedWords ( unordered_set < string > dict , string pattern ) { int len = pattern . length ( ) ; for ( string word : dict ) { if ( check ( pattern , word ) ) cout << word << " ▁ " ; } } int main ( ) { unordered_set < string > dict = { " abb " , " abc " , " xyz " , " xyy " } ; string pattern = " foo " ; findMatchedWords ( dict , pattern ) ; return 0 ; }
Reverse words in a given string | C ++ program for above approach : ; temp is for word boundary ; STEP 1 of the above algorithm ; This condition is to make sure that the string start with valid character ( not space ) only ; STEP 2 of the above algorithm
void reverseWords ( char * s ) { char * word_begin = NULL ; char * temp = s ; while ( * temp ) { if ( ( word_begin == NULL ) && ( * temp != ' ▁ ' ) ) { word_begin = temp ; } if ( word_begin && ( ( * ( temp + 1 ) == ' ▁ ' ) || ( * ( temp + 1 ) == ' \0' ) ) ) { reverse ( word_begin , temp ) ; word_begin = NULL ; } temp ++ ; } reverse ( s , temp - 1 ) ; }
Reverse words in a given string | C ++ code to reverse a string ; Reverse the string ; Check if number of words is even ; Find the middle word ; Starting from the middle start swapping words at jth position and l - 1 - j position ; Check if number of words is odd ; Find the middle word ; Starting from the middle start swapping the words at jth position and l - 1 - j position ; Return the reversed sentence ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string RevString ( string s [ ] , int l ) { if ( l % 2 == 0 ) { int j = l / 2 ; while ( j <= l - 1 ) { string temp ; temp = s [ l - j - 1 ] ; s [ l - j - 1 ] = s [ j ] ; s [ j ] = temp ; j += 1 ; } } else { int j = ( l / 2 ) + 1 ; while ( j <= l - 1 ) { string temp ; temp = s [ l - j - 1 ] ; s [ l - j - 1 ] = s [ j ] ; s [ j ] = temp ; j += 1 ; } } string S = s [ 0 ] ; for ( int i = 1 ; i < 9 ; i ++ ) { S = S + " ▁ " + s [ i ] ; } return S ; } int main ( ) { string s = " getting ▁ good ▁ at ▁ coding ▁ " " needs ▁ a ▁ lot ▁ of ▁ practice " ; string words [ ] = { " getting " , " good " , " at " , " coding " , " needs " , " a " , " lot " , " of " , " practice " } ; cout << RevString ( words , 9 ) << endl ; return 0 ; }
Print path from root to all nodes in a Complete Binary Tree | C ++ program to print path from root to all nodes in a complete binary tree . ; Function to print path of all the nodes nth node represent as given node kth node represents as left and right node ; base condition if kth node value is greater then nth node then its means kth node is not valid so we not store it into the res simply we just return ; Storing node into res ; Print the path from root to node ; store left path of a tree So for left we will go node ( kThNode * 2 ) ; right path of a tree and for right we will go node ( kThNode * 2 + 1 ) ; Function to print path from root to all of the nodes ; res is for store the path from root to particulate node ; Print path from root to all node . third argument 1 because of we have to consider root node is 1 ; Driver Code ; Given Node ; Print path from root to all node .
#include <iostream> NEW_LINE #include <vector> NEW_LINE using namespace std ; void printPath ( vector < int > res , int nThNode , int kThNode ) { if ( kThNode > nThNode ) return ; res . push_back ( kThNode ) ; for ( int i = 0 ; i < res . size ( ) ; i ++ ) cout << res [ i ] << " ▁ " ; cout << " STRNEWLINE " ; printPath ( res , nThNode , kThNode * 2 ) ; printPath ( res , nThNode , kThNode * 2 + 1 ) ; } void printPathToCoverAllNodeUtil ( int nThNode ) { vector < int > res ; printPath ( res , nThNode , 1 ) ; } int main ( ) { int nThNode = 7 ; printPathToCoverAllNodeUtil ( nThNode ) ; return 0 ; }
Maximum number of region in which N non | C ++ program to implement the above problem ; Function to find the maximum number of regions on a plane ; print the maximum number of regions ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxRegions ( int n ) { int num ; num = n * ( n + 1 ) / 2 + 1 ; cout << num ; } int main ( ) { int n = 10 ; maxRegions ( n ) ; return 0 ; }
Check whether jigsaw puzzle solveable or not | C ++ program for the above approach ; Function to check if the jigsaw Puzzle is solveable or not ; Base Case ; By placing the blank tabs as a chain ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkSolveable ( int n , int m ) { if ( n == 1 or m == 1 ) cout << " YES " ; else if ( m == 2 and n == 2 ) cout << " YES " ; else cout << " NO " ; } int main ( ) { int n = 1 , m = 3 ; checkSolveable ( n , m ) ; }
Check if it is possible to reach ( X , Y ) from ( 1 , 0 ) by given steps | C ++ program for the above approach ; Function to find the GCD of two numbers a and b ; Base Case ; Recursively find the GCD ; Function to check if ( x , y ) can be reached from ( 1 , 0 ) from given moves ; If GCD is 1 , then print " Yes " ; Driver Code ; Given X and Y ; Function call
#include <iostream> NEW_LINE using namespace std ; int GCD ( int a , int b ) { if ( b == 0 ) return a ; else return GCD ( b , a % b ) ; } void check ( int x , int y ) { if ( GCD ( x , y ) == 1 ) { cout << " Yes " ; } else { cout << " No " ; } } int main ( ) { int X = 2 , Y = 7 ; check ( X , Y ) ; return 0 ; }
Probability of Euler 's Totient Function in a range [L, R] to be divisible by M | C ++ Program to implement the above approach ; Seieve of Erotosthenes to compute all primes ; If prime ; Mark all its multiples as non - prime ; Function to find the probability of Euler 's Totient Function in a given range ; Initializing two arrays with values from L to R for Euler 's totient ; Indexing from 0 ; If the current number is prime ; Checking if i is prime factor of numbers in range L to R ; Update all the numbers which has prime factor i ; If number in range has a prime factor > sqrt ( number ) ; Count those which are divisible by M ; Return the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define size  1000001 NEW_LINE void seiveOfEratosthenes ( int * prime ) { prime [ 0 ] = 1 , prime [ 1 ] = 0 ; for ( int i = 2 ; i * i < 1000001 ; i ++ ) { if ( prime [ i ] == 0 ) { for ( int j = i * i ; j < 1000001 ; j += i ) { prime [ j ] = 1 ; } } } } float probabiltyEuler ( int * prime , int L , int R , int M ) { int * arr = new int [ size ] { 0 } ; int * eulerTotient = new int [ size ] { 0 } ; int count = 0 ; for ( int i = L ; i <= R ; i ++ ) { eulerTotient [ i - L ] = i ; arr [ i - L ] = i ; } for ( int i = 2 ; i < 1000001 ; i ++ ) { if ( prime [ i ] == 0 ) { for ( int j = ( L / i ) * i ; j <= R ; j += i ) { if ( j - L >= 0 ) { eulerTotient [ j - L ] = eulerTotient [ j - L ] / i * ( i - 1 ) ; while ( arr [ j - L ] % i == 0 ) { arr [ j - L ] /= i ; } } } } } for ( int i = L ; i <= R ; i ++ ) { if ( arr [ i - L ] > 1 ) { eulerTotient [ i - L ] = ( eulerTotient [ i - L ] / arr [ i - L ] ) * ( arr [ i - L ] - 1 ) ; } } for ( int i = L ; i <= R ; i ++ ) { if ( ( eulerTotient [ i - L ] % M ) == 0 ) { count ++ ; } } return ( 1.0 * count / ( R + 1 - L ) ) ; } int main ( ) { int * prime = new int [ size ] { 0 } ; seiveOfEratosthenes ( prime ) ; int L = 1 , R = 7 , M = 3 ; cout << probabiltyEuler ( prime , L , R , M ) ; return 0 ; }
Largest odd divisor Game to check which player wins | C ++ implementation to find the Largest Odd Divisor Game to check which player wins ; Function to find the Largest Odd Divisor Game to check which player wins ; Check if n == 1 then player 2 will win ; Check if n == 2 or n is odd ; While n is greater than k and divisible by 2 keep incrementing tha val ; Loop to find greatest odd divisor ; Check if n is a power of 2 ; Check if cnt is not one then player 1 wins ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findWinner ( int n , int k ) { int cnt = 0 ; if ( n == 1 ) cout << " No " << endl ; else if ( ( n & 1 ) or n == 2 ) cout << " Yes " << endl ; else { int tmp = n ; int val = 1 ; while ( tmp > k and tmp % 2 == 0 ) { tmp /= 2 ; val *= 2 ; } for ( int i = 3 ; i <= sqrt ( tmp ) ; i ++ ) { while ( tmp % i == 0 ) { cnt ++ ; tmp /= i ; } } if ( tmp > 1 ) cnt ++ ; if ( val == n ) cout << " No " << endl ; else if ( n / tmp == 2 and cnt == 1 ) cout << " No " << endl ; else cout << " Yes " << endl ; } } int main ( ) { long long n = 1 , k = 1 ; findWinner ( n , k ) ; return 0 ; }
Find all numbers up to N which are both Pentagonal and Hexagonal | C ++ Program of the above approach ; Function to print numbers upto N which are both pentagonal as well as hexagonal numbers ; Calculate i - th pentagonal number ; Check if the pentagonal number pn is hexagonal or not ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void pen_hex ( long long n ) { long long pn = 1 ; for ( long long int i = 1 ; ; i ++ ) { pn = i * ( 3 * i - 1 ) / 2 ; if ( pn > n ) break ; long double seqNum = ( 1 + sqrt ( 8 * pn + 1 ) ) / 4 ; if ( seqNum == long ( seqNum ) ) cout << pn << " , ▁ " ; } } int main ( ) { long long int N = 1000000 ; pen_hex ( N ) ; return 0 ; }
Check if row | C ++ implementation to check if row - major order traversal of matrix is palindrome or not ; Function to check if row - major order traversal of the matrix is is palindrome ; Loop to check if the matrix is matrix is palindrome or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPal ( int a [ 3 ] [ 3 ] , int n , int m ) { for ( int i = 0 ; i < n / 2 ; i ++ ) { for ( int j = 0 ; j < m - 1 ; j ++ ) { if ( a [ i ] [ j ] != a [ n - 1 - i ] [ m - 1 - j ] ) return false ; } } return true ; } int main ( ) { int n = 3 , m = 3 ; int a [ 3 ] [ 3 ] = { { 1 , 2 , 3 } , { 4 , 5 , 4 } , { 3 , 2 , 1 } } ; if ( isPal ( a , n , m ) ) { cout << " YES " << endl ; } else { cout << " NO " << endl ; } }
Find the smallest number whose sum of digits is N | C ++ program to find the smallest number whose sum of digits is also N ; Function to get sum of digits ; Function to find the smallest number whose sum of digits is also N ; Checking if number has sum of digits = N ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int getSum ( int n ) { int sum = 0 ; while ( n != 0 ) { sum = sum + n % 10 ; n = n / 10 ; } return sum ; } void smallestNumber ( int N ) { int i = 1 ; while ( 1 ) { if ( getSum ( i ) == N ) { cout << i ; break ; } i ++ ; } } int main ( ) { int N = 10 ; smallestNumber ( N ) ; return 0 ; }
Rare Numbers | C ++ implementation to check if N is a Rare number ; Iterative function to reverse digits of num ; Function to check if N is perfect square ; Find floating point value of square root of x . ; If square root is an integer ; Function to check if N is an Rare number ; Find reverse of N ; Number should be non - palindromic ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int reversDigits ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } bool isPerfectSquare ( long double x ) { long double sr = sqrt ( x ) ; return ( ( sr - floor ( sr ) ) == 0 ) ; } bool isRare ( int N ) { int reverseN = reversDigits ( N ) ; if ( reverseN == N ) return false ; return isPerfectSquare ( N + reverseN ) && isPerfectSquare ( N - reverseN ) ; } int main ( ) { int n = 65 ; if ( isRare ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2 | C ++ program to count the elements in the range [ L , R ] whose prime factors are only 2 and 3. ; Function which will calculate the elements in the given range ; Store the current power of 2 ; Store the current power of 3 ; power23 [ ] will store pairwise product of elements of power2 and power3 that are <= r ; Insert in power23 ] [ ] only if mul <= r ; Store the required answer ; Print the result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE void calc_ans ( ll l , ll r ) { vector < ll > power2 , power3 ; ll mul2 = 1 ; while ( mul2 <= r ) { power2 . push_back ( mul2 ) ; mul2 *= 2 ; } ll mul3 = 1 ; while ( mul3 <= r ) { power3 . push_back ( mul3 ) ; mul3 *= 3 ; } vector < ll > power23 ; for ( int x = 0 ; x < power2 . size ( ) ; x ++ ) { for ( int y = 0 ; y < power3 . size ( ) ; y ++ ) { ll mul = power2 [ x ] * power3 [ y ] ; if ( mul == 1 ) continue ; if ( mul <= r ) power23 . push_back ( mul ) ; } } ll ans = 0 ; for ( ll x : power23 ) { if ( x >= l && x <= r ) ans ++ ; } cout << ans << endl ; } int main ( ) { ll l = 1 , r = 10 ; calc_ans ( l , r ) ; return 0 ; }
Count of K length subsequence whose product is even | C ++ implementation to Count of K length subsequence whose Product is even ; Function to calculate nCr ; Returns factorial of n ; Function for finding number of K length subsequences whose product is even number ; counting odd numbers in the array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) ; int nCr ( int n , int r ) { if ( r > n ) return 0 ; return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } int countSubsequences ( int arr [ ] , int n , int k ) { int countOdd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & 1 ) countOdd ++ ; } int ans = nCr ( n , k ) - nCr ( countOdd , k ) ; return ans ; } int main ( ) { int arr [ ] = { 2 , 4 } ; int K = 1 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countSubsequences ( arr , N , K ) ; return 0 ; }
Find most significant bit of a number X in base Y | C ++ Program to find the first digit of X in base Y ; Function to find the first digit of X in base Y ; calculating number of digits of x in base y ; finding first digit of x in base y ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void first_digit ( int x , int y ) { int length = log ( x ) / log ( y ) + 1 ; int first_digit = x / pow ( y , length - 1 ) ; cout << first_digit ; } int main ( ) { int X = 55 , Y = 3 ; first_digit ( X , Y ) ; return 0 ; }
Curzon Numbers | C ++ implementation of the approach ; Function to check if a number is a Curzon number or not ; Find 2 ^ N + 1 ; Find 2 * N + 1 ; Check for divisibility ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkIfCurzonNumber ( int N ) { long int powerTerm , productTerm ; powerTerm = pow ( 2 , N ) + 1 ; productTerm = 2 * N + 1 ; if ( powerTerm % productTerm == 0 ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; } int main ( ) { long int N = 5 ; checkIfCurzonNumber ( N ) ; N = 10 ; checkIfCurzonNumber ( N ) ; return 0 ; }
Minimum count of numbers required ending with 7 to sum as a given number | C ++ implementation of the approach ; Function to return the count of minimum numbers ending with 7 required such that the sum of these numbers is n ; hasharr [ i ] will store the minimum numbers ending with 7 so that it sums to number ending with digit i ; Its always possible to write numbers > 69 to write as numbers ending with 7 ; If the number is atleast equal to the sum of minimum numbers ending with 7 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int TEN = 10 ; int minCount ( int n ) { int hasharr [ TEN ] = { 10 , 3 , 6 , 9 , 2 , 5 , 8 , 1 , 4 , 7 } ; if ( n > 69 ) return hasharr [ n % TEN ] ; else { if ( n >= hasharr [ n % TEN ] * 7 ) return ( hasharr [ n % TEN ] ) ; else return -1 ; } } int main ( ) { int n = 38 ; cout << minCount ( n ) ; return 0 ; }
Program to print modified Binary triangle pattern | C ++ implementation to print the modified binary triangle pattern ; Function to print the modified binary pattern ; Loop to traverse the rows ; Loop to traverse the numbers in each row ; Check if j is 1 or i In either case print 1 ; Else print 0 ; Change the cursor to next line after each row ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void modifiedBinaryPattern ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { if ( j == 1 j == i ) cout << 1 ; else cout << 0 ; } cout << endl ; } } int main ( ) { int n = 7 ; modifiedBinaryPattern ( n ) ; }
Find the real and imaginary part of a Complex number | C ++ program to find the real and imaginary parts of a Complex Number ; Function to find real and imaginary parts of a complex number ; string length stored in variable l ; variable for the index of the separator ; Storing the index of ' + ' ; else storing the index of ' - ' ; Finding the real part of the complex number ; Finding the imaginary part of the complex number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findRealAndImag ( string s ) { int l = s . length ( ) ; int i ; if ( s . find ( ' + ' ) < l ) { i = s . find ( ' + ' ) ; } else { i = s . find ( ' - ' ) ; } string real = s . substr ( 0 , i ) ; string imaginary = s . substr ( i + 1 , l - i - 2 ) ; cout << " Real ▁ part : ▁ " << real << " STRNEWLINE " ; cout << " Imaginary ▁ part : ▁ " << imaginary << " STRNEWLINE " ; } int main ( ) { string s = "3 + 4i " ; findRealAndImag ( s ) ; return 0 ; }
Distinct powers of a number N such that the sum is equal to K | C ++ implementation to find distinct powers of N that add upto K ; Function to return the highest power of N not exceeding K ; Loop to find the highest power less than K ; Initializing the PowerArray with all 0 's. ; Function to print the distinct powers of N that add upto K ; Getting the highest power of n before k ; To check if the power is being used twice or not ; Print - 1 if power is being used twice ; If the power is not visited , then mark the power as visited ; Decrementing the value of K ; Printing the powers of N that sum up to K ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int highestPower ( int n , int k ) { int i = 0 ; int a = pow ( n , i ) ; while ( a <= k ) { i += 1 ; a = pow ( n , i ) ; } return i - 1 ; } int b [ 50 ] = { 0 } ; int PowerArray ( int n , int k ) { while ( k ) { int t = highestPower ( n , k ) ; if ( b [ t ] ) { cout << -1 ; return 0 ; } else b [ t ] = 1 ; k -= pow ( n , t ) ; } for ( int i = 0 ; i < 50 ; i ++ ) { if ( b [ i ] ) { cout << i << " , ▁ " ; } } } int main ( ) { int N = 3 ; int K = 40 ; PowerArray ( N , K ) ; return 0 ; }
Sum of elements in an array having composite frequency | C ++ program to find sum of elements in an array having composite frequency ; Function to create Sieve to check primes ; If composite [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to composite ; Function to return the sum of elements in an array having composite frequency ; Map is used to store element frequencies ; To store sum ; Traverse the map using iterators ; Count the number of elements having composite frequencies ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100005 NEW_LINE void SieveOfEratosthenes ( vector < bool > & composite ) { for ( int i = 0 ; i < N ; i ++ ) composite [ i ] = false ; for ( int p = 2 ; p * p < N ; p ++ ) { if ( ! composite [ p ] ) { for ( int i = p * 2 ; i < N ; i += p ) composite [ i ] = true ; } } } int sumOfElements ( int arr [ ] , int n ) { vector < bool > composite ( N ) ; SieveOfEratosthenes ( composite ) ; unordered_map < int , int > m ; for ( int i = 0 ; i < n ; i ++ ) m [ arr [ i ] ] ++ ; int sum = 0 ; for ( auto it = m . begin ( ) ; it != m . end ( ) ; it ++ ) { if ( composite [ it -> second ] ) { sum += ( it -> first ) ; } } return sum ; } int main ( ) { int arr [ ] = { 1 , 2 , 1 , 1 , 1 , 3 , 3 , 2 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << sumOfElements ( arr , n ) ; return 0 ; }