codeparrot/xlcost-text-to-code huggingface dataset:
Difference between lexicographical ranks of two given permutations | C ++ program for the above approach ; Function the print the difference between the lexicographical ranks ; Store the permutations in lexicographic order ; Intital permutation ; Initial variables ; Check permutation ; Initialize second permutation ; Check permutation ; Print difference ; Driver Code ; Given array P [ ] ; Given array Q [ ] ; Given size ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findDifference ( vector < int > & p , vector < int > & q , int N ) { vector < int > A ( N ) ; for ( int i = 0 ; i < N ; i ++ ) A [ i ] = i + 1 ; bool IsCorrect ; int a = 1 , b = 1 ; do { IsCorrect = true ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] != p [ i ] ) { IsCorrect = false ; break ; } } if ( IsCorrect ) break ; a ++ ; } while ( next_permutation ( A . begin ( ) , A . end ( ) ) ) ; for ( int i = 0 ; i < N ; i ++ ) A [ i ] = i + 1 ; do { IsCorrect = true ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] != q [ i ] ) { IsCorrect = false ; break ; } } if ( IsCorrect ) break ; b ++ ; } while ( next_permutation ( A . begin ( ) , A . end ( ) ) ) ; cout << abs ( a - b ) << endl ; } int main ( ) { vector < int > p = { 1 , 3 , 2 } ; vector < int > q = { 3 , 1 , 2 } ; int n = p . size ( ) ; findDifference ( p , q , n ) ; return 0 ; }
Count of packets placed in each box after performing given operations | C ++ program for the above approach ; Function to print final array after performing all the operations ; Initialize variables ; Traverse through all operations ; Operation Type ; Move left ; Move right ; Pick a packet ; Drop a packet ; Exit ; Print final array ; Driver Code ; Given capacity ; Given array with initial values ; Array size ; Operations ; Number of operations ; Function call
#include <iostream> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE int printFinalArray ( int * a , int n , int * operations , int p , int capacity ) { int i , curr = 0 ; bool picked = false ; for ( i = 0 ; i < p ; i ++ ) { int s = operations [ i ] ; bool flag = false ; switch ( s ) { case 1 : if ( curr != 0 ) curr -- ; break ; case 2 : if ( curr != n - 1 ) curr ++ ; break ; case 3 : if ( picked == false && a [ curr ] != 0 ) { picked = true ; a [ curr ] -- ; } break ; case 4 : if ( picked == true && a [ curr ] != capacity ) { picked = false ; a [ curr ] ++ ; } break ; default : flag = true ; } if ( flag == true ) break ; } for ( i = 0 ; i < n ; i ++ ) { cout << a [ i ] << " ▁ " ; } } int main ( ) { int capacity = 5 ; int a [ ] = { 2 , 5 , 2 } ; int N = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int operations [ ] = { 3 , 2 , 4 , 1 , 4 , 5 } ; int M = sizeof ( operations ) / sizeof ( operations [ 0 ] ) ; printFinalArray ( a , N , operations , M , capacity ) ; return 0 ; }
Check if sum of count of digits of array elements is Prime or not | C ++ program for the above approach ; Function to check whether a number is prime or not ; Corner cases ; If given number is a multiple of 2 or 3 ; Function to check if sum of count of digits of all array elements is prime or not ; Initialize sum with 0 ; Traverse over the array ; Convert array element to string ; Add the count of digits to sum ; Print the result ; Drive Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } void CheckSumPrime ( int A [ ] , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { string s = to_string ( A [ i ] ) ; sum += s . length ( ) ; } if ( isPrime ( sum ) ) { cout << " Yes " << endl ; } else { cout << " No " << endl ; } } int main ( ) { int A [ ] = { 1 , 11 , 12 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; CheckSumPrime ( A , N ) ; return 0 ; }
Probability of obtaining Prime Numbers as product of values obtained by throwing N dices | C ++ program to implement the above approach ; Function to find the value of power ( X , N ) ; Stores the value of ( X ^ N ) ; Calculate the value of power ( x , N ) ; If N is odd ; Update res ; Update x ; Update N ; Function to find the probability of obtaining a prime number as the product of N thrown dices ; Stores count of favorable outcomes ; Stores count of sample space ; Print the required probability ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int power ( long long int x , long long int N ) { long long int res = 1 ; while ( N > 0 ) { if ( N & 1 ) { res = ( res * x ) ; } x = ( x * x ) ; N = N >> 1 ; } return res ; } void probablityPrimeprod ( long long int N ) { long long int N_E = 3 * N ; long long int N_S = power ( 6 , N ) ; cout << N_E << " ▁ / ▁ " << N_S ; } int main ( ) { long long int N = 2 ; probablityPrimeprod ( N ) ; }
Smaller palindromic number closest to N | C ++ program to implement the above approach ; Function to check if a number is palindrome or not ; Stores reverse of N ; Stores the value of N ; Calculate reverse of N ; Update rev ; Update N ; Update N ; if N is equal to rev of N ; Function to find the closest smaller palindromic number to N ; Calculate closest smaller palindromic number to N ; Update N ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkPalindrome ( int N ) { int rev = 0 ; int temp = N ; while ( N != 0 ) { rev = rev * 10 + N % 10 ; N = N / 10 ; } N = temp ; if ( N == rev ) { return true ; } return false ; } int closestSmallerPalindrome ( int N ) { do { N -- ; } while ( N >= 0 && ! checkPalindrome ( N ) ) ; return N ; } int main ( ) { int N = 4000 ; cout << closestSmallerPalindrome ( N ) ; return 0 ; }
Minimize increments or decrements by 2 to convert given value to a perfect square | C ++ program to implement the above approach ; Function to find the minimum number of operations required to make N a perfect square ; Stores count of operations by performing decrements ; Stores value of N ; Decrement the value of temp ; Stores square root of temp ; If temp is a perfect square ; Update temp ; Store count of operations by performing increments ; Increment the value of N ; Stores sqrt of N ; If N is a perfect square ; Update temp ; Return the minimum count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinimumOperationReq ( int N ) { int cntDecr = 0 ; int temp = N ; while ( temp > 0 ) { int X = sqrt ( temp ) ; if ( X * X == temp ) { break ; } temp = temp - 2 ; cntDecr += 1 ; } int cntIncr = 0 ; while ( true ) { int X = sqrt ( N ) ; if ( X * X == N ) { break ; } N = N + 2 ; cntIncr += 1 ; } return min ( cntIncr , cntDecr ) ; } int main ( ) { int N = 15 ; cout << MinimumOperationReq ( N ) ; return 0 ; }
First term from given Nth term of the equation F ( N ) = ( 2 * F ( N | C ++ program to implement the above approach ; Function to find the value of power ( X , N ) % M ; Stores the value of ( X ^ N ) % M ; Calculate the value of power ( x , N ) % M ; If N is odd ; Update res ; Update x ; Update N ; Function to find modulo multiplicative inverse of X under modulo M ; Function to find the value of F ( 1 ) ; Stores power ( 2 , N - 1 ) ; Stores modulo multiplicative inverse of P_2 under modulo M ; Stores the value of F ( 1 ) ; Update res ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define M  1000000007 NEW_LINE long long power ( long long x , long long N ) { long long res = 1 ; while ( N > 0 ) { if ( N & 1 ) { res = ( res * x ) % M ; } x = ( x * x ) % M ; N = N >> 1 ; } return res ; } long long moduloInverse ( long long X ) { return power ( X , M - 2 ) ; } long long F_1 ( long long N , long long F_N ) { long long P_2 = power ( 2 , N - 1 ) ; long long modInv = moduloInverse ( P_2 ) ; long long res ; res = ( ( modInv % M ) * ( F_N % M ) ) % M ; return res ; } int main ( ) { long long N = 3 ; long long F_N = 6 ; cout << F_1 ( N , F_N ) ; }
Queries to replace every array element by its XOR with a given value with updates | C ++ program of the above approach ; Function to insert an element into the array ; Function to update every array element a [ i ] by a [ i ] ^ x ; Function to compute the final results after the operations ; Driver Code ; Queries ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define int  long long NEW_LINE void add ( vector < int > & arr , int x ) { arr . push_back ( x ) ; } void update ( int & effect , int x ) { effect = effect ^ x ; } void computeResults ( vector < int > & arr , int effect ) { for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { arr [ i ] = arr [ i ] ^ effect ; cout << arr [ i ] << " ▁ " ; } } signed main ( ) { vector < int > arr = { 0 } ; int effect = 0 ; add ( arr , 5 ) ; update ( effect , 2 ) ; computeResults ( arr , effect ) ; }
Check if a subarray of size K exists whose elements form a number divisible by 3 | C ++ implementation of the above approach ; Function to find the K size subarray ; Check if the first K elements forms a number which is divisible by 3 ; Using Sliding window technique ; Calculate sum of next K size subarray ; Check if sum is divisible by 3 ; Update the indices of the subarray ; If no such subarray is found ; Print the subarray ; Driver 's code ; Given array and K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSubArray ( vector < int > arr , int k ) { pair < int , int > ans ; int i , sum = 0 ; for ( i = 0 ; i < k ; i ++ ) { sum += arr [ i ] ; } int found = 0 ; if ( sum % 3 == 0 ) { ans = make_pair ( 0 , i - 1 ) ; found = 1 ; } for ( int j = i ; j < arr . size ( ) ; j ++ ) { if ( found == 1 ) break ; sum = sum + arr [ j ] - arr [ j - k ] ; if ( sum % 3 == 0 ) { ans = make_pair ( j - k + 1 , j ) ; found = 1 ; } } if ( found == 0 ) ans = make_pair ( -1 , 0 ) ; if ( ans . first == -1 ) { cout << -1 ; } else { for ( i = ans . first ; i <= ans . second ; i ++ ) { cout << arr [ i ] << " ▁ " ; } } } int main ( ) { vector < int > arr = { 84 , 23 , 45 , 12 , 56 , 82 } ; int K = 3 ; findSubArray ( arr , K ) ; return 0 ; }
Count permutations of given array that generates the same Binary Search Tree ( BST ) | C ++ program to implement the above approach ; Function to precompute the factorial of 1 to N ; Function to get the value of nCr ; nCr = fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; Function to count the number of ways to rearrange the array to obtain same BST ; Store the size of the array ; Base case ; Store the elements of the left subtree of BST ; Store the elements of the right subtree of BST ; Store the root node ; Push all the elements of the left subtree ; Push all the elements of the right subtree ; Store the size of leftSubTree ; Store the size of rightSubTree ; Recurrence relation ; Driver Code ; Store the size of arr ; Store the factorial up to N ; Precompute the factorial up to N
#include <bits/stdc++.h> NEW_LINE using namespace std ; void calculateFact ( int fact [ ] , int N ) { fact [ 0 ] = 1 ; for ( long long int i = 1 ; i < N ; i ++ ) { fact [ i ] = fact [ i - 1 ] * i ; } } int nCr ( int fact [ ] , int N , int R ) { if ( R > N ) return 0 ; int res = fact [ N ] / fact [ R ] ; res /= fact [ N - R ] ; return res ; } int countWays ( vector < int > & arr , int fact [ ] ) { int N = arr . size ( ) ; if ( N <= 2 ) { return 1 ; } vector < int > leftSubTree ; vector < int > rightSubTree ; int root = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] < root ) { leftSubTree . push_back ( arr [ i ] ) ; } else { rightSubTree . push_back ( arr [ i ] ) ; } } int N1 = leftSubTree . size ( ) ; int N2 = rightSubTree . size ( ) ; int countLeft = countWays ( leftSubTree , fact ) ; int countRight = countWays ( rightSubTree , fact ) ; return nCr ( fact , N - 1 , N1 ) * countLeft * countRight ; } int main ( ) { vector < int > arr ; arr = { 3 , 4 , 5 , 1 , 2 } ; int N = arr . size ( ) ; int fact [ N ] ; calculateFact ( fact , N ) ; cout << countWays ( arr , fact ) ; return 0 ; }
Sum of all ordered pair | C ++ program to implement the above approach ; Function to calculate the sum of all pair - products ; Stores sum of array ; Update sum of the array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfProd ( int arr [ ] , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } return sum * sum ; } int main ( ) { int arr [ ] = { 2 , 3 , 1 , 5 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << sumOfProd ( arr , N ) ; return 0 ; }
Check if a given number is one less than twice its reverse | C ++ program of the above approach ; Iterative function to reverse digits of num ; Loop to extract all digits of the number ; Function to check if N satisfies given equation ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int rev ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } bool check ( int n ) { return 2 * rev ( n ) == n + 1 ; } int main ( ) { int n = 73 ; if ( check ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check if a given number is one less than twice its reverse | C ++ program to implement the above approach ; Function to check y is a power of x ; logarithm function to calculate value ; Compare to the result1 or result2 both are equal ; Function to check if N satisfies the equation 2 * reverse ( n ) = n + 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPower ( int x , int y ) { int res1 = log ( y ) / log ( x ) ; double res2 = log ( y ) / log ( x ) ; return ( res1 == res2 ) ; } bool check ( int n ) { int x = ( n + 7 ) / 8 ; if ( ( n + 7 ) % 8 == 0 && isPower ( 10 , x ) ) return true ; else return false ; } int main ( ) { int n = 73 ; if ( check ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Nth term of a recurrence relation generated by two given arrays | C ++ program for the above approach ; Function to calculate Nth term of general recurrence relations ; Stores the generated sequence ; Current term is sum of previous k terms ; Print the nth term ; Driver Code ; Given Array F [ ] and C [ ] ; Given N and K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int mod = 1e9 + 7 ; void NthTerm ( int F [ ] , int C [ ] , int K , int n ) { int ans [ n + 1 ] = { 0 } ; for ( int i = 0 ; i < K ; i ++ ) ans [ i ] = F [ i ] ; for ( int i = K ; i <= n ; i ++ ) { for ( int j = i - K ; j < i ; j ++ ) { ans [ i ] += ans [ j ] ; ans [ i ] %= mod ; } } cout << ans [ n ] << endl ; } int main ( ) { int F [ ] = { 0 , 1 } ; int C [ ] = { 1 , 1 } ; int K = 2 ; int N = 10 ; NthTerm ( F , C , K , N ) ; return 0 ; }
Mode of frequencies of given array elements | C ++ program of the above approach ; Function to find the mode of the frequency of the array ; Stores the frequencies of array elements ; Traverse through array elements and count frequencies ; Stores the frequencies 's of  frequencies of array elements ; Stores the minimum value ; Find the Mode in 2 nd map ; search for this Mode ; When mode is find then return to main function . ; If mode is not found ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countFreq ( int arr [ ] , int n ) { unordered_map < int , int > mp1 ; for ( int i = 0 ; i < n ; ++ i ) { mp1 [ arr [ i ] ] ++ ; } unordered_map < int , int > mp2 ; for ( auto it : mp1 ) { mp2 [ it . second ] ++ ; } int M = INT_MIN ; for ( auto it : mp2 ) { M = max ( M , it . second ) ; } for ( auto it : mp2 ) { if ( M == it . second ) { return it . first ; } } return 0 ; } int main ( ) { int arr [ ] = { 6 , 10 , 3 , 10 , 8 , 3 , 6 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countFreq ( arr , n ) ; return 0 ; }
Maximize count of nodes disconnected from all other nodes in a Graph | C ++ implementation of the above approach ; Function which returns the maximum number of isolated nodes ; Used nodes ; Remaining edges ; Count nodes used ; If given edges are non - zero ; Driver Code ; Given N and E ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxDisconnected ( int N , int E ) { int curr = 1 ; int rem = E ; while ( rem > 0 ) { rem = rem - min ( curr , rem ) ; curr ++ ; } if ( curr > 1 ) { return N - curr ; } else { return N ; } } int main ( ) { int N = 5 , E = 1 ; cout << maxDisconnected ( N , E ) ; return 0 ; }
Check if number is palindrome or not in base B | C ++ program to implement the above approach ; Function to check if N in base B is palindrome or not ; Stores the reverse of N ; Stores the value of N ; Extract all the digits of N ; Generate its reverse ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int checkPalindromeB ( int N , int B ) { int rev = 0 ; int N1 = N ; while ( N1 ) { rev = rev * B + N1 % B ; N1 = N1 / B ; } return N == rev ; } int main ( ) { int N = 5 , B = 2 ; if ( checkPalindromeB ( N , B ) ) { cout << " Yes " ; } else { cout << " No " ; } }
Product of all numbers up to N that are co | C ++ program for the above approach ; Function to return gcd of a and b ; Base Case ; Recursive GCD ; Function to find the product of all the numbers till N that are relatively prime to N ; Stores the resultant product ; Iterate over [ 2 , N ] ; If gcd is 1 , then find the product with result ; Return the final product ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } int findProduct ( unsigned int N ) { unsigned int result = 1 ; for ( int i = 2 ; i < N ; i ++ ) { if ( gcd ( i , N ) == 1 ) { result *= i ; } } return result ; } int main ( ) { int N = 5 ; cout << findProduct ( N ) ; return 0 ; }
Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum | C ++ implementation of the above approach ; Function to count maximum number of array elements equal ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countEqual ( int n ) { return ( n + 1 ) / 2 ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countEqual ( n ) ; return 0 ; }
Minimize count of unequal elements at corresponding indices between given arrays | C ++ program for the above approach ; Function that count of the mismatched pairs in bot the array ; Create a parent array and initialize it ; Preprocessing of the given pairs of indices ; 1 - based indexing ; If both indices doesn 't belong  to same component ; Insert the indices in same component ; Map to get the indices of array A ; If current element is not present in array B then count this as mismatched ; Get the index of the element in array B ; Check if both indices belong to same connected component if not increment the count ; Return answer ; Function that gives the connected component of each index ; Function that creates the connected components ; Find parent of a and b recursively ; Update the parent of a ; Driver Code ; Given arrays A [ ] , B [ ] ; List of indices ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find ( int par [ ] , int x ) ; void unionn ( int par [ ] , int a , int b ) ; int countPairs ( int A [ ] , int B [ ] , int N , int M , int List [ ] [ 2 ] ) { int count = 0 ; int par [ N + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) par [ i ] = i ; for ( int i = 0 ; i < M ; i ++ ) { int index1 = find ( par , List [ i ] [ 0 ] - 1 ) ; int index2 = find ( par , List [ i ] [ 1 ] - 1 ) ; if ( index1 != index2 ) { unionn ( par , index1 , index2 ) ; } } map < int , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { mp [ A [ i ] ] = i ; } for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] != B [ i ] ) { if ( mp . find ( B [ i ] ) == mp . end ( ) ) { count ++ ; continue ; } int j = mp [ B [ i ] ] ; if ( find ( par , i ) != find ( par , j ) ) count ++ ; } } return count ; } int find ( int par [ ] , int x ) { if ( par [ x ] == x ) return x ; else return par [ x ] = find ( par , par [ x ] ) ; } void unionn ( int par [ ] , int a , int b ) { a = find ( par , a ) ; b = find ( par , b ) ; if ( a == b ) return ; par [ a ] = b ; } int main ( ) { int N = 5 ; int M = 4 ; int A [ ] = { 1 , 5 , 9 , 2 , 3 } ; int B [ ] = { 2 , 4 , 5 , 1 , 3 } ; int List [ ] [ 2 ] = { { 1 , 4 } , { 2 , 3 } , { 3 , 5 } , { 2 , 5 } } ; cout << countPairs ( A , B , N , M , List ) ; return 0 ; }
Maximum value of expression ( arr [ i ] + arr [ j ] * arr [ k ] ) formed from a valid Triplet | C ++ program for the above approach ; Function that generate all valid triplets and calculate the value of the valid triplets ; Generate all triplets ; Check whether the triplet is valid or not ; Update the value ; Print the maximum value ; Driver Code ; Given array arr [ ] ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void max_valid_triplet ( int A [ ] , int n ) { int ans = -1 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { for ( int j = i + 1 ; j < n - 1 ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { if ( A [ i ] < A [ j ] && A [ j ] < A [ k ] ) { int value = A [ i ] + A [ j ] * A [ k ] ; if ( value > ans ) { ans = value ; } } } } } cout << ( ans ) ; } int main ( ) { int arr [ ] = { 7 , 9 , 3 , 8 , 11 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; max_valid_triplet ( arr , n ) ; return 0 ; }
Product of proper divisors of a number for Q queries | C ++ implementation of the above approach ; Function to precompute the product of proper divisors of a number at it 's corresponding index ; Returning the pre - computed values ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE #define mod  1000000007 NEW_LINE using namespace std ; vector < ll > ans ( 100002 , 1 ) ; void preCompute ( ) { for ( int i = 2 ; i <= 100000 / 2 ; i ++ ) { for ( int j = 2 * i ; j <= 100000 ; j += i ) { ans [ j ] = ( ans [ j ] * i ) % mod ; } } } int productOfProperDivi ( int num ) { return ans [ num ] ; } int main ( ) { preCompute ( ) ; int queries = 5 ; int a [ queries ] = { 4 , 6 , 8 , 16 , 36 } ; for ( int i = 0 ; i < queries ; i ++ ) { cout << productOfProperDivi ( a [ i ] ) << " , ▁ " ; } return 0 ; }
Highest power of 2 that divides the LCM of first N Natural numbers . | C ++ implementation of the approach ; Function to find LCM of first N natural numbers ; Initialize result ; Ans contains LCM of 1 , 2 , 3 , . . i after i 'th iteration ; Function to find the highest power of 2 which divides LCM of first n natural numbers ; Find lcm of first N natural numbers ; To store the highest required power of 2 ; Counting number of consecutive zeros from the end in the given binary string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findlcm ( int n ) { int ans = 1 ; for ( int i = 1 ; i <= n ; i ++ ) ans = ( ( ( i * ans ) ) / ( __gcd ( i , ans ) ) ) ; return ans ; } int highestPower ( int n ) { int lcm = findlcm ( n ) ; int ans = 0 ; for ( int i = 1 ; ; i ++ ) { int x = pow ( 2 , i ) ; if ( lcm % x == 0 ) { ans = i ; } if ( x > n ) break ; } return ans ; } int main ( ) { int n = 15 ; cout << highestPower ( n ) ; return 0 ; }
Highest power of 2 that divides the LCM of first N Natural numbers . | C ++ implementation of the approach ; Function to find the highest power of 2 which divides LCM of first n natural numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int highestPower ( int n ) { return log ( n ) / log ( 2 ) ; } int main ( ) { int n = 15 ; cout << highestPower ( n ) ; return 0 ; }