codeparrot/xlcost-text-to-code huggingface dataset:
Minimum integer such that it leaves a remainder 1 on dividing with any element from the range [ 2 , N ] | C ++ implementation of the approach ; Function to return the smallest number which on dividing with any element from the range [ 2 , N ] leaves a remainder of 1 ; Find the LCM of the elements from the range [ 2 , N ] ; Return the required number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long getMinNum ( int N ) { int lcm = 1 ; for ( int i = 2 ; i <= N ; i ++ ) lcm = ( ( i * lcm ) / ( __gcd ( i , lcm ) ) ) ; return ( lcm + 1 ) ; } int main ( ) { int N = 5 ; cout << getMinNum ( N ) ; return 0 ; }
Maximum number of edges in Bipartite graph | C ++ implementation of the approach ; Function to return the maximum number of edges possible in a Bipartite graph with N vertices ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxEdges ( int N ) { int edges = 0 ; edges = floor ( ( N * N ) / 4 ) ; return edges ; } int main ( ) { int N = 5 ; cout << maxEdges ( N ) ; return 0 ; }
Check if the robot is within the bounds of the grid after given moves | C ++ implementation of the approach ; Function that returns true if the robot is safe ; If current move is " L " then increase the counter of coll ; If value of coll is equal to column then break ; If current move is " R " then increase the counter of colr ; If value of colr is equal to column then break ; If current move is " U " then increase the counter of rowu ; If value of rowu is equal to row then break ; If current move is " D " then increase the counter of rowd ; If value of rowd is equal to row then break ; If robot is within the bounds of the grid ; Unsafe ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isSafe ( int N , int M , string str ) { int coll = 0 , colr = 0 , rowu = 0 , rowd = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == ' L ' ) { coll ++ ; if ( colr > 0 ) { colr -- ; } if ( coll == M ) { break ; } } else if ( str [ i ] == ' R ' ) { colr ++ ; if ( coll > 0 ) { coll -- ; } if ( colr == M ) { break ; } } else if ( str [ i ] == ' U ' ) { - rowu ++ ; if ( rowd > 0 ) { rowd -- ; } if ( rowu == N ) { break ; } } else if ( str [ i ] == ' D ' ) { rowd ++ ; if ( rowu > 0 ) { rowu -- ; } if ( rowd == N ) { break ; } } } if ( abs ( rowd ) < N && abs ( rowu ) < N && abs ( coll ) < M && abs ( colr ) < M ) { return true ; } return false ; } int main ( ) { int N = 1 , M = 1 ; string str = " R " ; if ( isSafe ( N , M , str ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find sub | C ++ implementation of the approach ; Function to print the valid indices in the array ; Function to find sub - arrays from two different arrays with equal sum ; Map to store the indices in A and B which produce the given difference ; Find the smallest j such that b [ j ] >= a [ i ] ; Difference encountered for the second time ; b [ j ] - a [ i ] = b [ idx . second ] - a [ idx . first ] b [ j ] - b [ idx . second ] = a [ i ] = a [ idx . first ] So sub - arrays are a [ idx . first + 1. . . i ] and b [ idx . second + 1. . . j ] ; Store the indices for difference in the map ; Utility function to calculate the cumulative sum of the array ; Driver code ; Function to update the arrays with their cumulative sum ; Swap is true as a and b are swapped during function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printAns ( int x , int y , int num ) { cout << " Indices ▁ in ▁ array ▁ " << num << " ▁ : ▁ " ; for ( int i = x ; i < y ; ++ i ) { cout << i << " , ▁ " ; } cout << y << " STRNEWLINE " ; } void findSubarray ( int N , int a [ ] , int b [ ] , bool swap ) { std :: map < int , pair < int , int > > index ; int difference ; index [ 0 ] = make_pair ( -1 , -1 ) ; int j = 0 ; for ( int i = 0 ; i < N ; ++ i ) { while ( b [ j ] < a [ i ] ) { j ++ ; } difference = b [ j ] - a [ i ] ; if ( index . find ( difference ) != index . end ( ) ) { if ( swap ) { pair < int , int > idx = index [ b [ j ] - a [ i ] ] ; printAns ( idx . second + 1 , j , 1 ) ; printAns ( idx . first + 1 , i , 2 ) ; } else { pair < int , int > idx = index [ b [ j ] - a [ i ] ] ; printAns ( idx . first + 1 , i , 1 ) ; printAns ( idx . second + 1 , j , 2 ) ; } return ; } index [ difference ] = make_pair ( i , j ) ; } cout << " - 1" ; } void cumulativeSum ( int arr [ ] , int n ) { for ( int i = 1 ; i < n ; ++ i ) arr [ i ] += arr [ i - 1 ] ; } int main ( ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 } ; int b [ ] = { 6 , 2 , 1 , 5 , 4 } ; int N = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cumulativeSum ( a , N ) ; cumulativeSum ( b , N ) ; if ( b [ N - 1 ] > a [ N - 1 ] ) { findSubarray ( N , a , b , false ) ; } else { findSubarray ( N , b , a , true ) ; } return 0 ; }
Find permutation of first N natural numbers that satisfies the given condition | C ++ implementation of the approach ; Function to find permutation ( p ) of first N natural numbers such that there are exactly K elements of permutation such that GCD ( p [ i ] , i ) > 1 ; First place all the numbers in their respective places ; Modify for first n - k integers ; In first index place n - k ; Print the permutation ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Permutation ( int n , int k ) { int p [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) p [ i ] = i ; for ( int i = 1 ; i < n - k ; i ++ ) p [ i + 1 ] = i ; p [ 1 ] = n - k ; for ( int i = 1 ; i <= n ; i ++ ) cout << p [ i ] << " ▁ " ; } int main ( ) { int n = 5 , k = 2 ; Permutation ( n , k ) ; return 0 ; }
Count number of 1 s in the array after N moves | C ++ implementation of the above approach ; Function to count number of 1 's in the array after performing N moves ; If index is multiple of move number ; arr [ j - 1 ] = 1 ; Convert 0 to 1 ; arr [ j - 1 ] = 0 ; Convert 1 to 0 ; Count number of 1 's ; count ++ ; count number of 1 's ; Driver Code ; Initialize all elements to 0
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOnes ( int arr [ ] , int N ) { for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = i ; j <= N ; j ++ ) { if ( j % i == 0 ) { if ( arr [ j - 1 ] == 0 ) else } } } int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( arr [ i ] == 1 ) return count ; } int main ( ) { int arr [ 10 ] = { 0 } ; int ans = countOnes ( arr , N ) ; cout << ans ; return 0 ; }
Number of positions such that adding K to the element is greater than sum of all other elements | C ++ program to implement above approach ; Function that will find out the valid position ; find sum of all the elements ; adding K to the element and check whether it is greater than sum of all other elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int validPosition ( int arr [ ] , int N , int K ) { int count = 0 , sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] + K ) > ( sum - arr [ i ] ) ) count ++ ; } return count ; } int main ( ) { int arr [ ] = { 2 , 1 , 6 , 7 } , K = 4 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << validPosition ( arr , N , K ) ; return 0 ; }
Count unique numbers that can be generated from N by adding one and removing trailing zeros | C ++ implementation of the approach ; Function to count the unique numbers ; If the number has already been visited ; Insert the number to the set ; First step ; Second step remove trailing zeros ; Recur again for the new number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void count_unique ( unordered_set < int > & s , int n ) { if ( s . count ( n ) ) return ; s . insert ( n ) ; n += 1 ; while ( n % 10 == 0 ) { n = n / 10 ; } count_unique ( s , n ) ; } int main ( ) { int n = 10 ; unordered_set < int > s ; count_unique ( s , n ) ; cout << s . size ( ) ; return 0 ; }
Find element with the maximum set bits in an array | C ++ implementation of the approach ; Function to return the element from the array which has the maximum set bits ; To store the required element and the maximum set bits so far ; Count of set bits in the current element ; Update the max ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; ; int maxBitElement ( int arr [ ] , int n ) { int num = 0 , max = -1 ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = __builtin_popcount ( arr [ i ] ) ; if ( cnt > max ) { max = cnt ; num = arr [ i ] ; } } return num ; } int main ( ) { int arr [ ] = { 3 , 2 , 4 , 7 , 1 , 10 , 5 , 8 , 9 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxBitElement ( arr , n ) << endl ; return 0 ; }
Count pairs with average present in the same array | C ++ implementation of the approach ; Function to return the count of valid pairs ; Frequency array Twice the original size to hold negative elements as well ; Update the frequency of each of the array element ; If say x = - 1000 then we will place the frequency of - 1000 at ( - 1000 + 1000 = 0 ) a [ 0 ] index ; To store the count of valid pairs ; Remember we will check only for ( even , even ) or ( odd , odd ) pairs of indexes as the average of two consecutive elements is a floating point number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 1000 ; int countPairs ( int arr [ ] , int n ) { int size = ( 2 * N ) + 1 ; int freq [ size ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { int x = arr [ i ] ; freq [ x + N ] ++ ; } int ans = 0 ; for ( int i = 0 ; i < size ; i ++ ) { if ( freq [ i ] > 0 ) { ans += ( ( freq [ i ] ) * ( freq [ i ] - 1 ) ) / 2 ; for ( int j = i + 2 ; j < 2001 ; j += 2 ) { if ( freq [ j ] > 0 && ( freq [ ( i + j ) / 2 ] > 0 ) ) { ans += ( freq [ i ] * freq [ j ] ) ; } } } } return ans ; } int main ( ) { int arr [ ] = { 4 , 2 , 5 , 1 , 3 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPairs ( arr , n ) ; return 0 ; }
Smallest and Largest sum of two n | C ++ implementation of the approach ; Function to return the smallest sum of 2 n - digit numbers ; Function to return the largest sum of 2 n - digit numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestSum ( int n ) { if ( n == 1 ) return 0 ; return ( 2 * pow ( 10 , n - 1 ) ) ; } int largestSum ( int n ) { return ( 2 * ( pow ( 10 , n ) - 1 ) ) ; } int main ( ) { int n = 4 ; cout << " Largest ▁ = ▁ " << largestSum ( n ) << endl ; cout << " Smallest ▁ = ▁ " << smallestSum ( n ) ; return 0 ; }
Given two arrays count all pairs whose sum is an odd number | C ++ program to implement the above approach ; Function that returns the number of pairs ; Count of odd and even numbers ; Traverse in the first array and count the number of odd and evene numbers in them ; Traverse in the second array and count the number of odd and evene numbers in them ; Count the number of pairs ; Return the number of pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count_pairs ( int a [ ] , int b [ ] , int n , int m ) { int odd1 = 0 , even1 = 0 ; int odd2 = 0 , even2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 ) odd1 ++ ; else even1 ++ ; } for ( int i = 0 ; i < m ; i ++ ) { if ( b [ i ] % 2 ) odd2 ++ ; else even2 ++ ; } int pairs = min ( odd1 , even2 ) + min ( odd2 , even1 ) ; return pairs ; } int main ( ) { int a [ ] = { 9 , 14 , 6 , 2 , 11 } ; int b [ ] = { 8 , 4 , 7 , 20 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int m = sizeof ( b ) / sizeof ( b [ 0 ] ) ; cout << count_pairs ( a , b , n , m ) ; return 0 ; }
Print steps to make a number in form of 2 ^ X | C ++ program to implement the above approach ; Function to find the leftmost unset bit in a number . ; Function that perform the step ; Find the leftmost unset bit ; If the number has no bit unset , it means it is in form 2 ^ x - 1 ; Count the steps ; Iterate till number is of form 2 ^ x - 1 ; At even step increase by 1 ; Odd step xor with any 2 ^ m - 1 ; Find the leftmost unset bit ; 2 ^ m - 1 ; Perform the step ; Increase the steps ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find_leftmost_unsetbit ( int n ) { int ind = -1 ; int i = 1 ; while ( n ) { if ( ! ( n & 1 ) ) ind = i ; i ++ ; n >>= 1 ; } return ind ; } void perform_steps ( int n ) { int left = find_leftmost_unsetbit ( n ) ; if ( left == -1 ) { cout << " No ▁ steps ▁ required " ; return ; } int step = 1 ; while ( find_leftmost_unsetbit ( n ) != -1 ) { if ( step % 2 == 0 ) { n += 1 ; cout << " Step " << step << " : ▁ Increase ▁ by ▁ 1 STRNEWLINE " ; } else { int m = find_leftmost_unsetbit ( n ) ; int num = pow ( 2 , m ) - 1 ; n = n ^ num ; cout << " Step " << step << " : ▁ Xor ▁ with ▁ " << num << endl ; } step += 1 ; } } int main ( ) { int n = 39 ; perform_steps ( n ) ; return 0 ; }
Determine the position of the third person on regular N sided polygon | C ++ implementation of above approach ; Function to find out the number of that vertices ; Another person can 't stand on  vertex on which 2 children stand. ; calculating minimum jumps from each vertex . ; Calculate sum of jumps . ; Driver code ; Calling function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int vertices ( int N , int A , int B ) { int position = 0 ; int minisum = INT_MAX ; int sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i == A i == B ) continue ; else { int x = abs ( i - A ) ; int y = abs ( i - B ) ; sum = x + y ; if ( sum < minisum ) { minisum = sum ; position = i ; } } } return position ; } int main ( ) { int N = 3 , A = 1 , B = 2 ; cout << " Vertex ▁ = ▁ " << vertices ( N , A , B ) ; return 0 ; }
Find sum of factorials in an array | C ++ implementation of the approach ; Function to return the factorial of n ; Function to return the sum of factorials of the array elements ; To store the required sum ; Add factorial of all the elements ; Driver code
#include <iostream> NEW_LINE #include <bits/stdc++.h> NEW_LINE using namespace std ; int factorial ( int n ) { int f = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { f *= i ; } return f ; } int sumFactorial ( int * arr , int n ) { int s = 0 , i ; for ( i = 0 ; i < n ; i ++ ) { s += factorial ( arr [ i ] ) ; } return s ; } int main ( ) { int arr [ ] = { 7 , 3 , 5 , 4 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << sumFactorial ( arr , n ) ; return 0 ; }
Minimum steps to color the tree with given colors | C ++ implementation of the approach ; To store the required answer ; To store the graph ; Function to add edges ; Dfs function ; When there is difference in colors ; For all it 's child nodes ; Driver code ; Here zero is for parent of node 1 ; Adding edges in the graph ; Dfs call ; Required answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ans = 0 ; vector < int > gr [ 100005 ] ; void Add_Edge ( int u , int v ) { gr [ u ] . push_back ( v ) ; gr [ v ] . push_back ( u ) ; } void dfs ( int child , int par , int color [ ] ) { if ( color [ child ] != color [ par ] ) ans ++ ; for ( auto it : gr [ child ] ) { if ( it == par ) continue ; dfs ( it , child , color ) ; } } int main ( ) { int color [ ] = { 0 , 1 , 2 , 3 , 2 , 2 , 3 } ; Add_Edge ( 1 , 2 ) ; Add_Edge ( 1 , 3 ) ; Add_Edge ( 2 , 4 ) ; Add_Edge ( 2 , 5 ) ; Add_Edge ( 3 , 6 ) ; dfs ( 1 , 0 , color ) ; cout << ans ; return 0 ; }
Highest power of 2 that divides a number represented in binary | C ++ implementation of the approach ; Function to return the highest power of 2 which divides the given binary number ; 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 highestPower ( string str , int len ) { int ans = 0 ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == '0' ) ans ++ ; else break ; } return ans ; } int main ( ) { string str = "100100" ; int len = str . length ( ) ; cout << highestPower ( str , len ) ; return 0 ; }
Number of ways to arrange K different objects taking N objects at a time | C ++ implementation of the approach ; Function to return n ! % p ; ll res = 1 ; Initialize result ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Returns n ^ ( - 1 ) mod p ; Returns nCr % p using Fermat 's little theorem. ; Base case ; Fill factorial array so that we can find all factorial of r , n and n - r ; Function to return the number of ways to arrange K different objects taking N objects at a time ; Drivers Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE #define mod  (ll)(1e9 + 7) NEW_LINE ll factorial ( ll n , ll p ) { for ( int i = 2 ; i <= n ; i ++ ) res = ( res * i ) % p ; return res ; } ll power ( ll x , ll y , ll p ) { while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } ll modInverse ( ll n , ll p ) { return power ( n , p - 2 , p ) ; } ll nCrModP ( ll n , ll r , ll p ) { if ( r == 0 ) return 1 ; ll fac [ n + 1 ] ; fac [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fac [ i ] = fac [ i - 1 ] * i % p ; return ( fac [ n ] * modInverse ( fac [ r ] , p ) % p * modInverse ( fac [ n - r ] , p ) % p ) % p ; } ll countArrangements ( ll n , ll k , ll p ) { return ( factorial ( n , p ) * nCrModP ( k , n , p ) ) % p ; } int main ( ) { ll N = 5 , K = 8 ; cout << countArrangements ( N , K , mod ) ; return 0 ; }
Find maximum product of digits among numbers less than or equal to N | C ++ implementation of the approach ; Function that returns the maximum product of digits among numbers less than or equal to N ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxProd ( int N ) { if ( N == 0 ) return 1 ; if ( N < 10 ) return N ; return max ( maxProd ( N / 10 ) * ( N % 10 ) , maxProd ( N / 10 - 1 ) * 9 ) ; } int main ( ) { int N = 390 ; cout << maxProd ( N ) ; return 0 ; }
Check if a number from every row can be selected such that xor of the numbers is greater than zero | C ++ program to implement the above approach ; Function to check if a number from every row can be selected such that xor of the numbers is greater than zero ; Find the xor of first column for every row ; If Xorr is 0 ; Traverse in the matrix ; Check is atleast 2 distinct elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  2 NEW_LINE #define M  3 NEW_LINE bool check ( int mat [ N ] [ M ] ) { int xorr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { xorr ^= mat [ i ] [ 0 ] ; } if ( xorr != 0 ) return true ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] != mat [ i ] [ 0 ] ) return true ; } } return false ; } int main ( ) { int mat [ N ] [ M ] = { { 7 , 7 , 7 } , { 10 , 10 , 7 } } ; if ( check ( mat ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Sum of the series 1 , 2 , 4 , 3 , 5 , 7 , 9 , 6 , 8 , 10 , 11 , 13. . till N | C ++ program to implement the above approach ; Function to find the sum of first N odd numbers ; Function to find the sum of first N even numbers ; Function to overall find the sum of series ; Initial odd numbers ; Initial even numbers ; First power of 2 ; Check for parity for odd / even ; Counts the sum ; Get the minimum out of remaining num or power of 2 ; Decrease that much numbers from num ; If the segment has odd numbers ; Summate the odd numbers By exclusion ; Increase number of odd numbers ; If the segment has even numbers ; Summate the even numbers By exclusion ; Increase number of even numbers ; Next set of numbers ; Change parity for odd / even ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumodd ( int n ) { return ( n * n ) ; } int sumeven ( int n ) { return ( n * ( n + 1 ) ) ; } int findSum ( int num ) { int sumo = 0 ; int sume = 0 ; int x = 1 ; int cur = 0 ; int ans = 0 ; while ( num > 0 ) { int inc = min ( x , num ) ; num -= inc ; if ( cur == 0 ) { ans = ans + sumodd ( sumo + inc ) - sumodd ( sumo ) ; sumo += inc ; } else { ans = ans + sumeven ( sume + inc ) - sumeven ( sume ) ; sume += inc ; } x *= 2 ; cur ^= 1 ; } return ans ; } int main ( ) { int n = 4 ; cout << findSum ( n ) ; return 0 ; }
Find the nth term of the given series | C ++ implementation of the approach ; Function to return the nth term of the given series ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int oddTriangularNumber ( int N ) { return ( N * ( ( 2 * N ) - 1 ) ) ; } int main ( ) { int N = 3 ; cout << oddTriangularNumber ( N ) ; return 0 ; }
Check if given two straight lines are identical or not | C ++ program to check if given two straight lines are identical or not ; Function to check if they are identical ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void idstrt ( double a1 , double b1 , double c1 , double a2 , double b2 , double c2 ) { if ( ( a1 / a2 == b1 / b2 ) && ( a1 / a2 == c1 / c2 ) && ( b1 / b2 == c1 / c2 ) ) cout << " The ▁ given ▁ straight " << " ▁ lines ▁ are ▁ identical " << endl ; else cout << " The ▁ given ▁ straight " << " ▁ lines ▁ are ▁ not ▁ identical " << endl ; } int main ( ) { double a1 = -2 , b1 = 4 , c1 = 3 , a2 = -6 , b2 = 12 , c2 = 9 ; idstrt ( a1 , b1 , c1 , a2 , b2 , c2 ) ; return 0 ; }
Count arrays of length K whose product of elements is same as that of given array | C ++ implementation of the approach ; To store the smallest prime factor for every number ; Initialize map to store count of prime factors ; Function to calculate SPF ( Smallest Prime Factor ) for every number till MAXN ; Marking smallest prime factor for every number to be itself ; Separately marking spf for every even number as 2 ; Checking if i is prime ; Marking SPF for all numbers divisible by i ; Marking spf [ j ] if it is not previously marked ; Function to factorize using spf and store in cnt ; Function to return n ! % p ; Initialize result ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is >= p ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Function that returns n ^ ( - 1 ) mod p ; Function that returns nCr % p using Fermat 's little theorem ; Base case ; Fill factorial array so that we can find all factorial of r , n and n - r ; Function to return the count the number of possible arrays mod P of length K such that the product of all elements of that array is equal to the product of all elements of the given array of length N ; Initialize result ; Call sieve to get spf ; Factorize arr [ i ] , count and store its factors in cnt ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE #define MAXN  (ll)(1e5 + 1) NEW_LINE #define mod  (ll)(1e9 + 7) NEW_LINE ll spf [ MAXN ] ; map < ll , ll > cnt ; void sieve ( ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } void factorize ( ll f ) { while ( f > 1 ) { ll x = spf [ f ] ; while ( f % x == 0 ) { cnt [ x ] ++ ; f /= x ; } } } ll factorial ( ll n , ll p ) { ll res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = ( res * i ) % p ; return res ; } ll power ( ll x , ll y , ll p ) { ll res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } ll modInverse ( ll n , ll p ) { return power ( n , p - 2 , p ) ; } ll nCrModP ( ll n , ll r , ll p ) { if ( r == 0 ) return 1 ; ll fac [ n + 1 ] ; fac [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fac [ i ] = fac [ i - 1 ] * i % p ; return ( fac [ n ] * modInverse ( fac [ r ] , p ) % p * modInverse ( fac [ n - r ] , p ) % p ) % p ; } ll countArrays ( ll arr [ ] , ll N , ll K , ll P ) { ll res = 1 ; sieve ( ) ; for ( int i = 0 ; i < N ; i ++ ) { factorize ( arr [ i ] ) ; } for ( auto i : cnt ) { int ci = i . second ; res = ( res * nCrModP ( ci + K - 1 , K - 1 , P ) ) % P ; } return res ; } int main ( ) { ll arr [ ] = { 1 , 3 , 5 , 2 } , K = 3 ; ll N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countArrays ( arr , N , K , mod ) ; return 0 ; }