codeparrot/xlcost-text-to-code huggingface dataset:
Maximum Prefix Sum possible by merging two given arrays | C ++ Program to implement the above approach ; Stores the maximum prefix sum of the array A [ ] ; Traverse the array A [ ] ; Stores the maximum prefix sum of the array B [ ] ; Traverse the array B [ ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxPresum ( vector < int > a , vector < int > b ) { int X = max ( a [ 0 ] , 0 ) ; for ( int i = 1 ; i < a . size ( ) ; i ++ ) { a [ i ] += a [ i - 1 ] ; X = max ( X , a [ i ] ) ; } int Y = max ( b [ 0 ] , 0 ) ; for ( int i = 1 ; i < b . size ( ) ; i ++ ) { b [ i ] += b [ i - 1 ] ; Y = max ( Y , b [ i ] ) ; } return X + Y ; } int main ( ) { vector < int > A = { 2 , -1 , 4 , -5 } ; vector < int > B = { 4 , -3 , 12 , 4 , -3 } ; cout << maxPresum ( A , B ) << endl ; }
Check if a number can be represented as sum of two positive perfect cubes | C ++ program for the above approach ; Function to check if N can be represented as sum of two perfect cubes or not ; if it is same return true ; ; if the curr smaller than n increment the lo ; if the curr is greater than curr decrement the hi ; Driver Code ; Function call to check if N can be represented as sum of two perfect cubes or not
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool sumOfTwoCubes ( int n ) { long long int lo = 1 , hi = ( long long int ) cbrt ( n ) ; while ( lo <= hi ) { long long int curr = ( lo * lo * lo + hi * hi * hi ) ; if ( curr == n ) return true ; if ( curr < n ) lo ++ ; else hi -- ; } return false ; } int main ( ) { int N = 28 ; if ( sumOfTwoCubes ( N ) ) { cout << " True " ; } else { cout << " False " ; } return 0 ; }
Generate an N | C ++ program for the above approach ; Function to generate all prime numbers upto 10 ^ 6 ; Initialize sieve [ ] as 1 ; Iterate over the range [ 2 , N ] ; If current element is non - prime ; Make all multiples of i as 0 ; Function to construct an array A [ ] satisfying the given conditions ; Stores the resultant array ; Stores all prime numbers ; Sieve of Erastosthenes ; Append the integer i if it is a prime ; Indicates current position in list of prime numbers ; Traverse the array arr [ ] ; If already filled with another prime number ; If A [ i ] is not filled but A [ ind ] is filled ; Store A [ i ] = A [ ind ] ; If none of them were filled ; To make sure A [ i ] does not affect other values , store next prime number ; Print the resultant array ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sieve [ 1000000 ] ; void sieveOfPrimes ( ) { memset ( sieve , 1 , sizeof ( sieve ) ) ; int N = 1000000 ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( sieve [ i ] == 0 ) continue ; for ( int j = i * i ; j <= N ; j += i ) sieve [ j ] = 0 ; } } void getArray ( int * arr , int N ) { int A [ N ] = { 0 } ; vector < int > v ; sieveOfPrimes ( ) ; for ( int i = 2 ; i <= 1e5 ; i ++ ) if ( sieve [ i ] ) v . push_back ( i ) ; int j = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int ind = arr [ i ] ; if ( A [ i ] != 0 ) continue ; else if ( A [ ind ] != 0 ) A [ i ] = A [ ind ] ; else { int prime = v [ j ++ ] ; A [ i ] = prime ; A [ ind ] = A [ i ] ; } } for ( int i = 0 ; i < N ; i ++ ) { cout << A [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 4 , 1 , 2 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; getArray ( arr , N ) ; return 0 ; }
Nth natural number after removing all numbers consisting of the digit 9 | C ++ implementation of above approach ; Function to find Nth number in base 9 ; Stores the Nth number ; Iterate while N is greater than 0 ; Update result ; Divide N by 9 ; Multiply p by 10 ; Return result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long findNthNumber ( long long N ) { long long result = 0 ; long long p = 1 ; while ( N > 0 ) { result += ( p * ( N % 9 ) ) ; N = N / 9 ; p = p * 10 ; } return result ; } int main ( ) { int N = 9 ; cout << findNthNumber ( N ) ; return 0 ; }
Check if an integer is rotation of another given integer | C ++ implementation of the approach ; Function to check if the integer A is a rotation of the integer B ; Stores the count of digits in A ; Stores the count of digits in B ; If dig1 not equal to dig2 ; Stores position of first digit ; Stores the first digit ; Rotate the digits of the integer ; If A is equal to B ; If A is equal to the initial value of integer A ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int check ( int A , int B ) { if ( A == B ) { return 1 ; } int dig1 = floor ( log10 ( A ) + 1 ) ; int dig2 = floor ( log10 ( B ) + 1 ) ; if ( dig1 != dig2 ) { return 0 ; } int temp = A ; while ( 1 ) { int power = pow ( 10 , dig1 - 1 ) ; int firstdigit = A / power ; A = A - firstdigit * power ; A = A * 10 + firstdigit ; if ( A == B ) { return 1 ; } if ( A == temp ) { return 0 ; } } } int main ( ) { int A = 967 , B = 679 ; if ( check ( A , B ) ) cout << " Yes " ; else cout << " No " << endl ; return 0 ; }
Count of quadruples with product of a pair equal to the product of the remaining pair | C ++ program for the above approach ; Function to count the number of unique quadruples from an array that satisfies the given condition ; Hashmap to store the product of pairs ; Store the count of required quadruples ; Traverse the array arr [ ] and generate all possible pairs ; Store their product ; Pair ( a , b ) can be used to generate 8 unique permutations with another pair ( c , d ) ; Increment um [ prod ] by 1 ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sameProductQuadruples ( int nums [ ] , int N ) { unordered_map < int , int > umap ; int res = 0 ; for ( int i = 0 ; i < N ; ++ i ) { for ( int j = i + 1 ; j < N ; ++ j ) { int prod = nums [ i ] * nums [ j ] ; res += 8 * umap [ prod ] ; ++ umap [ prod ] ; } } cout << res ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sameProductQuadruples ( arr , N ) ; return 0 ; }
Count ways to place M objects in distinct partitions of N boxes | C ++ implementation of the above Approach ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize Result ; Update x if x >= MOD to avoid multiplication overflow ; If y is odd , multiply x with result ; y = y / 2 ; Change x to x ^ 2 ; Utility function to find the Total Number of Ways ; Number of Even Indexed Boxes ; Number of partitions of Even Indexed Boxes ; Number of ways to distribute objects ; Driver Code ; N = number of boxes M = number of distinct objects ; Function call to get Total Number of Ways
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MOD = 1000000007 ; int power ( int x , unsigned int y , int p = MOD ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * 1LL * x ) % p ; y = y >> 1 ; x = ( x * 1LL * x ) % p ; } return res ; } void totalWays ( int N , int M ) { int X = N / 2 ; int S = ( X * 1LL * ( X + 1 ) ) % MOD ; cout << power ( S , M , MOD ) << " STRNEWLINE " ; } int main ( ) { int N = 5 , M = 2 ; totalWays ( N , M ) ; return 0 ; }
Check if a graph constructed from an array based on given conditions consists of a cycle or not | C ++ program for the above approach ; Function to check if the graph constructed from given array contains a cycle or not ; Traverse the array ; If arr [ i ] is less than arr [ i - 1 ] and arr [ i ] ; Driver Code ; Given array ; Size of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void isCycleExists ( int arr [ ] , int N ) { bool valley = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] && arr [ i ] < arr [ i + 1 ] ) { cout << " Yes " << endl ; return ; } } cout << " No " ; } int main ( ) { int arr [ ] = { 1 , 3 , 2 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; isCycleExists ( arr , N ) ; return 0 ; }
Maximize first array element by performing given operations at most K times | C ++ program for the above approach ; Function to maximize the first array element ; Traverse the array ; Initialize cur_val to a [ i ] ; If all operations are not over yet ; If current value is greater than zero ; Incrementing first element of array by 1 ; Decrementing current value of array by 1 ; Decrementing number of operations by i ; If current value is zero , then break ; Print first array element ; Driver Code ; Given array ; Size of the array ; Given K ; Prints the maximum possible value of the first array element
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMax ( int arr [ ] , int N , int K ) { for ( int i = 1 ; i < N ; i ++ ) { int cur_val = arr [ i ] ; while ( K >= i ) { if ( cur_val > 0 ) { arr [ 0 ] = arr [ 0 ] + 1 ; cur_val = cur_val - 1 ; K = K - i ; } else break ; } } cout << arr [ 0 ] ; } int main ( ) { int arr [ ] = { 1 , 0 , 3 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 5 ; getMax ( arr , N , K ) ; return 0 ; }
Count Non | C ++ program of the above approach ; Function to find the gcd of the two numbers ; Function to find distinct elements in the array by repeatidely inserting the absolute difference of all possible pairs ; Stores largest element of the array ; Traverse the array , arr [ ] ; Update max_value ; Stores GCD of array ; Traverse the array , arr [ ] ; Update GCDArr ; Stores distinct elements in the array by repeatidely inserting absolute difference of all possible pairs ; Driver Code ; Given array arr [ ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } int DistinctValues ( int arr [ ] , int N ) { int max_value = INT_MIN ; for ( int i = 0 ; i < N ; ++ i ) { max_value = max ( max_value , arr [ i ] ) ; } int GCDArr = arr [ 0 ] ; for ( int i = 1 ; i < N ; ++ i ) { GCDArr = gcd ( GCDArr , arr [ i ] ) ; } int answer = ( max_value / GCDArr ) + 1 ; return answer ; } int main ( ) { int arr [ ] = { 4 , 12 , 16 , 24 } ; int N = sizeof ( arr ) / sizeof ( int ) ; cout << DistinctValues ( arr , N ) ; return 0 ; }
Minimum row or column swaps required to make every pair of adjacent cell of a Binary Matrix distinct | C ++ program for the above approach ; Function to return number of moves to convert matrix into chessboard ; Size of the matrix ; Traverse the matrix ; Initialize rowSum to count 1 s in row ; Initialize colSum to count 1 s in column ; To store no . of rows to be corrected ; To store no . of columns to be corrected ; Traverse in the range [ 0 , N - 1 ] ; Check if rows is either N / 2 or ( N + 1 ) / 2 and return - 1 ; Check if rows is either N / 2 or ( N + 1 ) / 2 and return - 1 ; Check if N is odd ; Check if column required to be corrected is odd and then assign N - colSwap to colSwap ; Check if rows required to be corrected is odd and then assign N - rowSwap to rowSwap ; Take min of colSwap and N - colSwap ; Take min of rowSwap and N - rowSwap ; Finally return answer ; Driver Code ; Given matrix ; Function Call ; Print answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSwaps ( vector < vector < int > > & b ) { int n = b . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( b [ 0 ] [ 0 ] ^ b [ 0 ] [ j ] ^ b [ i ] [ 0 ] ^ b [ i ] [ j ] ) return -1 ; } } int rowSum = 0 ; int colSum = 0 ; int rowSwap = 0 ; int colSwap = 0 ; for ( int i = 0 ; i < n ; i ++ ) { rowSum += b [ i ] [ 0 ] ; colSum += b [ 0 ] [ i ] ; rowSwap += b [ i ] [ 0 ] == i % 2 ; colSwap += b [ 0 ] [ i ] == i % 2 ; } if ( rowSum != n / 2 && rowSum != ( n + 1 ) / 2 ) return -1 ; if ( colSum != n / 2 && colSum != ( n + 1 ) / 2 ) return -1 ; if ( n % 2 == 1 ) { if ( colSwap % 2 ) colSwap = n - colSwap ; if ( rowSwap % 2 ) rowSwap = n - rowSwap ; } else { colSwap = min ( colSwap , n - colSwap ) ; rowSwap = min ( rowSwap , n - rowSwap ) ; } return ( rowSwap + colSwap ) / 2 ; } int main ( ) { vector < vector < int > > M = { { 0 , 1 , 1 , 0 } , { 0 , 1 , 1 , 0 } , { 1 , 0 , 0 , 1 } , { 1 , 0 , 0 , 1 } } ; int ans = minSwaps ( M ) ; cout << ans ; }
Minimum number of coins having value equal to powers of 2 required to obtain N | C ++ program for above approach ; Function to count of set bit in N ; Stores count of set bit in N ; Iterate over the range [ 0 , 31 ] ; If current bit is set ; Update result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void count_setbit ( int N ) { int result = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { if ( ( 1 << i ) & N ) { result ++ ; } } cout << result << endl ; } int main ( ) { int N = 43 ; count_setbit ( N ) ; return 0 ; }
Evaluate the expression ( N1 * ( N | C ++ program to implement the above approach ; Function to find the value of the expression ( N ^ 1 * ( N 1 ) ^ 2 * ... * 1 ^ N ) % ( 109 + 7 ) . ; factorial [ i ] : Stores factorial of i ; Base Case for factorial ; Precompute the factorial ; dp [ N ] : Stores the value of the expression ( N ^ 1 * ( N 1 ) ^ 2 * ... * 1 ^ N ) % ( 109 + 7 ) . ; Update dp [ i ] ; Return the answer . ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define mod  1000000007 NEW_LINE int ValOfTheExpression ( int n ) { int factorial [ n ] = { 0 } ; factorial [ 0 ] = factorial [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { factorial [ i ] = ( ( factorial [ i - 1 ] % mod ) * ( i % mod ) ) % mod ; } int dp [ n ] = { 0 } ; dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ i ] = ( ( dp [ i - 1 ] % mod ) * ( factorial [ i ] % mod ) ) % mod ; } return dp [ n ] ; } int main ( ) { int n = 4 ; cout << ValOfTheExpression ( n ) << " STRNEWLINE " ; }
Chocolate Distribution Problem | Set 2 | C ++ program for the above approach ; FUnction to print minimum number of candies required ; Distribute 1 chocolate to each ; Traverse from left to right ; Traverse from right to left ; Initialize sum ; Find total sum ; Return sum ; Driver Code ; Given array ; Size of the given array
#include <iostream> NEW_LINE using namespace std ; void minChocolates ( int A [ ] , int N ) { int B [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { B [ i ] = 1 ; } for ( int i = 1 ; i < N ; i ++ ) { if ( A [ i ] > A [ i - 1 ] ) B [ i ] = B [ i - 1 ] + 1 ; else B [ i ] = 1 ; } for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( A [ i ] > A [ i + 1 ] ) B [ i ] = max ( B [ i + 1 ] + 1 , B [ i ] ) ; else B [ i ] = max ( B [ i ] , 1 ) ; } int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += B [ i ] ; } cout << sum << " STRNEWLINE " ; } int main ( ) { int A [ ] = { 23 , 14 , 15 , 14 , 56 , 29 , 14 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; minChocolates ( A , N ) ; }
Construct longest possible sequence of unique elements with given LCM | C ++ program to implement the above approach ; Function to construct an array of unique elements whose LCM is N ; Stores array elements whose LCM is N ; Iterate over the range [ 1 , sqrt ( N ) ] ; If N is divisible by i ; Insert i into newArr [ ] ; If N is not perfect square ; Sort the array newArr [ ] ; Print array elements ; Driver Code ; Given N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void constructArrayWithGivenLCM ( int N ) { vector < int > newArr ; for ( int i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { newArr . push_back ( i ) ; if ( N / i != i ) { newArr . push_back ( N / i ) ; } } } sort ( newArr . begin ( ) , newArr . end ( ) ) ; for ( auto i : newArr ) { cout << i << " ▁ " ; } } int main ( ) { int N = 12 ; constructArrayWithGivenLCM ( N ) ; return 0 ; }
Count numbers from given range having odd digits at odd places and even digits at even places | C ++ program to implement the above approach ; Function to calculate 5 ^ p ; Stores the result ; Multiply 5 p times ; Return the result ; Function to count all numbers upto N having odd digits at odd places and even digits at even places ; Stores the count ; Stores the digits of N ; Insert the digits of N ; Reverse the vector to arrange the digits from first to last ; Stores count of digits of n ; Stores the count of numbers with i digits ; If the last digit is reached , subtract numbers eceeding range ; Iterate over all the places ; Stores the digit in the pth place ; Stores the count of numbers having a digit greater than x in the p - th position ; Calculate the count of numbers exceeding the range if p is even ; Calculate the count of numbers exceeding the range if p is odd ; Subtract the count of numbers exceeding the range from total count ; If the parity of p and the parity of x are not same ; Add count of numbers having i digits and satisfies the given conditions ; Return the total count of numbers till n ; Function to calculate the count of numbers from given range having odd digits places and even digits at even places ; Count of numbers in range [ L , R ] = Count of numbers till R - ; Count of numbers till ( L - 1 ) ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE ll getPower ( int p ) { ll res = 1 ; while ( p -- ) { res *= 5 ; } return res ; } ll countNumbersUtil ( ll N ) { ll count = 0 ; vector < int > digits ; while ( N ) { digits . push_back ( N % 10 ) ; N /= 10 ; } reverse ( digits . begin ( ) , digits . end ( ) ) ; int D = digits . size ( ) ; for ( int i = 1 ; i <= D ; i ++ ) { ll res = getPower ( i ) ; if ( i == D ) { for ( int p = 1 ; p <= D ; p ++ ) { int x = digits [ p - 1 ] ; ll tmp = 0 ; if ( p % 2 == 0 ) { tmp = ( 5 - ( x / 2 + 1 ) ) * getPower ( D - p ) ; } else { tmp = ( 5 - ( x + 1 ) / 2 ) * getPower ( D - p ) ; } res -= tmp ; if ( p % 2 != x % 2 ) { break ; } } } count += res ; } return count ; } void countNumbers ( ll L , ll R ) { cout << ( countNumbersUtil ( R ) - countNumbersUtil ( L - 1 ) ) << endl ; } int main ( ) { ll L = 128 , R = 162 ; countNumbers ( L , R ) ; return 0 ; }
Sum of first N natural numbers with alternate signs | C ++ program to implement the above approach ; Function to find the sum of first N natural numbers with alternate signs ; Stores sum of alternate sign of first N natural numbers ; If is an even number ; Update alternateSum ; If i is an odd number ; Update alternateSum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int alternatingSumOfFirst_N ( int N ) { int alternateSum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) { alternateSum += - i ; } else { alternateSum += i ; } } return alternateSum ; } int main ( ) { int N = 6 ; cout << alternatingSumOfFirst_N ( N ) ; return 0 ; }
Sum 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 calculate the sum of all numbers till N that are coprime with N ; Stores the resultant sum ; Iterate over [ 1 , N ] ; If gcd is 1 ; Update sum ; Return the final sum ; Driver Code ; Given N ; Function Call
#include <iostream> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } int findSum ( unsigned int N ) { unsigned int sum = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( gcd ( i , N ) == 1 ) { sum += i ; } } return sum ; } int main ( ) { int N = 5 ; cout << findSum ( N ) ; return 0 ; }
Count all distinct pairs of repeating elements from the array for every array element | C ++ program for the above approach ; Function to print the required count of pairs excluding the current element ; Store the frequency ; Find all the count ; Delete the contribution of each element for equal pairs ; Print the answer ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE #define int  long long int NEW_LINE using namespace std ; void solve ( int arr [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) { mp [ arr [ i ] ] ++ ; } int cnt = 0 ; for ( auto x : mp ) { cnt += ( ( x . second ) * ( x . second - 1 ) / 2 ) ; } int ans [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { ans [ i ] = cnt - ( mp [ arr [ i ] ] - 1 ) ; } for ( int i = 0 ; i < n ; i ++ ) { cout << ans [ i ] << " ▁ " ; } } int32_t main ( ) { int arr [ ] = { 1 , 1 , 2 , 1 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; solve ( arr , N ) ; return 0 ; }
Mode in a stream of integers ( running integers ) | C ++ program to implement the above approach ; Function that prints the Mode values ; Map used to mp integers to its frequency ; To store the maximum frequency ; To store the element with the maximum frequency ; Loop used to read the elements one by one ; Updates the frequency of that element ; Checks for maximum Number of occurrence ; Updates the maximum frequency ; Updates the Mode ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMode ( int a [ ] , int n ) { map < int , int > mp ; int max = 0 ; int mode = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mp [ a [ i ] ] ++ ; if ( mp [ a [ i ] ] >= max ) { max = mp [ a [ i ] ] ; mode = a [ i ] ; } cout << mode << " ▁ " ; } } int main ( ) { int arr [ ] = { 2 , 7 , 3 , 2 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findMode ( arr , n ) ; return 0 ; }
Count of distinct numbers formed by shuffling the digits of a large number N | C ++ program for the above approach ; Recursive function to return the value of ( x ^ n ) % m ; Base Case ; If N is even ; Else N is odd ; Function to find modular inverse of a number x under modulo m ; Using Fermat 's little theorem ; Function to count of numbers formed by shuffling the digits of a large number N ; Modulo value ; Array to store the factorials upto the maximum value of N ; Store factorial of i at index i ; To store count of occurrence of a digit ; Increment the count of digit occured ; Assign the factorial of length of input ; Multiplying result with the modulo multiplicative inverse of factorial of count of i ; Print the result ; Driver Code ; Given Number as string ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE ll modexp ( ll x , ll n , ll m ) { if ( n == 0 ) { return 1 ; } else if ( n % 2 == 0 ) { return modexp ( ( x * x ) % m , n / 2 , m ) ; } else { return ( x * modexp ( ( x * x ) % m , ( n - 1 ) / 2 , m ) % m ) ; } } ll modInverse ( ll x , ll m ) { return modexp ( x , m - 2 , m ) ; } void countNumbers ( string N ) { ll m = 1000000007 ; ll factorial [ 100001 ] ; factorial [ 0 ] = 1 ; for ( ll i = 1 ; i < 100001 ; i ++ ) { factorial [ i ] = ( factorial [ i - 1 ] * i ) % m ; } ll count [ 10 ] ; for ( ll i = 0 ; i < 10 ; i ++ ) { count [ i ] = 0 ; } ll length = N . length ( ) ; for ( ll i = 0 ; i < length ; i ++ ) count [ N [ i ] - '0' ] ++ ; ll result = factorial [ length ] ; for ( ll i = 0 ; i < 10 ; i ++ ) { result = ( result * modInverse ( factorial [ count [ i ] ] , m ) ) % m ; } cout << result ; } int main ( ) { string N = "0223" ; countNumbers ( N ) ; return 0 ; }