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
using System ; using System . Collections . Generic ; class GFG { static int maxPresum ( List < int > a , List < int > b ) { int X = Math . Max ( a [ 0 ] , 0 ) ; for ( int i = 1 ; i < a . Count ; i ++ ) { a [ i ] += a [ i - 1 ] ; X = Math . Max ( X , a [ i ] ) ; } int Y = Math . Max ( b [ 0 ] , 0 ) ; for ( int i = 1 ; i < b . Count ; i ++ ) { b [ i ] += b [ i - 1 ] ; Y = Math . Max ( Y , b [ i ] ) ; } return X + Y ; } static void Main ( ) { List < int > A = new List < int > ( new int [ ] { 2 , - 1 , 4 , - 5 } ) ; List < int > B = new List < int > ( new int [ ] { 4 , - 3 , 12 , 4 , - 3 } ) ; Console . WriteLine ( maxPresum ( A , B ) ) ; } }
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
using System ; class GFG { static bool sumOfTwoCubes ( int n ) { int lo = 1 , hi = ( int ) Math . Pow ( n , ( 1.0 / 3.0 ) ) ; while ( lo <= hi ) { int curr = ( lo * lo * lo + hi * hi * hi ) ; if ( curr == n ) return true ; if ( curr < n ) lo ++ ; else hi -- ; } return false ; } public static void Main ( String [ ] args ) { int N = 28 ; if ( sumOfTwoCubes ( N ) ) { Console . Write ( " True " ) ; } else { Console . Write ( " False " ) ; } } }
Generate an N | C # Program to implement 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
using System ; using System . Collections . Generic ; class GFG { static int [ ] sieve = new int [ 10000000 ] ; static void sieveOfPrimes ( ) { for ( int i = 0 ; i < 10000000 ; i ++ ) { sieve [ i ] = 1 ; } 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 ; } } static void getArray ( int [ ] arr , int N ) { int [ ] A = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { A [ i ] = 0 ; } List < int > v = new List < int > ( ) ; sieveOfPrimes ( ) ; for ( int i = 2 ; i <= 1000000 ; i ++ ) if ( sieve [ i ] != 0 ) v . Add ( 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 ++ ) { Console . Write ( A [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 1 , 2 , 3 , 4 } ; int N = arr . Length ; getArray ( arr , N ) ; } }
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
using System ; class GFG { static long findNthNumber ( long N ) { long result = 0 ; long p = 1 ; while ( N > 0 ) { result += ( p * ( N % 9 ) ) ; N = N / 9 ; p = p * 10 ; } return result ; } static void Main ( ) { int N = 9 ; Console . Write ( findNthNumber ( N ) ) ; } }
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
using System ; public class GFG { static int check ( int A , int B ) { if ( A == B ) { return 1 ; } int dig1 = ( int ) Math . Floor ( Math . Log10 ( A ) + 1 ) ; int dig2 = ( int ) Math . Floor ( Math . Log10 ( B ) + 1 ) ; if ( dig1 != dig2 ) { return 0 ; } int temp = A ; while ( true ) { int power = ( int ) Math . 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 ; } } } public static void Main ( String [ ] args ) { int A = 967 , B = 679 ; if ( check ( A , B ) == 1 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Count of quadruples with product of a pair equal to the product of the remaining pair | C # program to implement 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
using System ; class GFG { static void sameProductQuadruples ( int [ ] nums , int N ) { int [ ] umap = new int [ 10000 ] ; 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 ] ; } } Console . Write ( res ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 6 } ; int N = arr . Length ; sameProductQuadruples ( arr , N ) ; } }
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
using System ; public class GFG { public static int MOD = 1000000007 ; static int power ( int x , int y , int p ) { p = MOD ; int res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static void totalWays ( int N , int M ) { int X = N / 2 ; int S = ( X * ( X + 1 ) ) % MOD ; Console . WriteLine ( power ( S , M , MOD ) ) ; } static public void Main ( ) { int N = 5 , M = 2 ; totalWays ( N , M ) ; } }
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
using System ; class GFG { static void isCycleExists ( int [ ] arr , int N ) { for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] && arr [ i ] < arr [ i + 1 ] ) { Console . WriteLine ( " Yes " ) ; return ; } } Console . WriteLine ( " No " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 2 , 4 , 5 } ; int N = arr . Length ; isCycleExists ( arr , N ) ; } }
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
using System ; class GFG { static void 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 ; } } Console . Write ( arr [ 0 ] ) ; } static void Main ( ) { int [ ] arr = { 1 , 0 , 3 , 2 } ; int N = arr . Length ; int K = 5 ; getMax ( arr , N , K ) ; } }
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 [ ]
using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int DistinctValues ( int [ ] arr , int N ) { int max_value = Int32 . MinValue ; for ( int i = 0 ; i < N ; ++ i ) { max_value = Math . 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 ; } static void Main ( ) { int [ ] arr = { 4 , 12 , 16 , 24 } ; int N = arr . Length ; Console . WriteLine ( DistinctValues ( arr , N ) ) ; } }
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
using System ; class GFG { public static int minSwaps ( int [ , ] b ) { int n = b . GetLength ( 0 ) ; 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 ] ) == 1 ) { 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 ] ; int cond1 = 0 ; int cond2 = 0 ; if ( b [ i , 0 ] == i % 2 ) cond1 = 1 ; if ( b [ 0 , i ] == i % 2 ) cond2 = 1 ; rowSwap += cond1 ; colSwap += cond2 ; } 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 ) == 1 ) colSwap = n - colSwap ; if ( ( rowSwap % 2 ) == 1 ) rowSwap = n - rowSwap ; } else { colSwap = Math . Min ( colSwap , n - colSwap ) ; rowSwap = Math . Min ( rowSwap , n - rowSwap ) ; } return ( rowSwap + colSwap ) / 2 ; } public static void Main ( String [ ] args ) { int [ , ] M = { { 0 , 1 , 1 , 0 } , { 0 , 1 , 1 , 0 } , { 1 , 0 , 0 , 1 } , { 1 , 0 , 0 , 1 } } ; int ans = minSwaps ( M ) ; Console . WriteLine ( 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 setbit in N ; Stores count of setbit in N ; Iterate over the range [ 0 , 31 ] ; If current bit is set ; Update result ; Driver Code
using System ; class GFG { static void count_setbit ( int N ) { int result = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { if ( ( ( 1 << i ) & N ) > 0 ) { result ++ ; } } Console . WriteLine ( result ) ; } static void Main ( ) { int N = 43 ; count_setbit ( N ) ; } }
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
using System ; class GFG { static int mod = 1000000007 ; static int ValOfTheExpression ( int n ) { int [ ] factorial = new int [ n + 1 ] ; factorial [ 0 ] = factorial [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { factorial [ i ] = ( ( factorial [ i - 1 ] % mod ) * ( i % mod ) ) % mod ; } int [ ] dp = new int [ n + 1 ] ; dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ i ] = ( ( dp [ i - 1 ] % mod ) * ( factorial [ i ] % mod ) ) % mod ; } return dp [ n ] ; } static void Main ( ) { int n = 4 ; Console . WriteLine ( ValOfTheExpression ( n ) ) ; } }
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
using System ; public class GFG { static void minChocolates ( int [ ] A , int N ) { int [ ] B = new int [ 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 ] = Math . Max ( B [ i + 1 ] + 1 , B [ i ] ) ; else B [ i ] = Math . Max ( B [ i ] , 1 ) ; } int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += B [ i ] ; } Console . Write ( sum + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 23 , 14 , 15 , 14 , 56 , 29 , 14 } ; int N = A . Length ; 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
using System ; class GFG { static void constructArrayWithGivenLCM ( int N ) { int [ ] newArr = new int [ N ] ; int j = 0 ; for ( int i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { newArr [ j ] = i ; j ++ ; if ( N / i != i ) { newArr [ j ] = N / i ; j ++ ; } } } Array . Sort ( newArr ) ; for ( int i = j ; i < N ; i ++ ) { Console . Write ( newArr [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int N = 12 ; constructArrayWithGivenLCM ( N ) ; } }
Count numbers from given range having odd digits at odd places and even digits at even places | C # program for 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
using System ; using System . Collections . Generic ; class GFG { static int getPower ( int p ) { int res = 1 ; while ( p > 0 ) { res *= 5 ; p -- ; } return res ; } static int countNumbersUtil ( int N ) { int count = 0 ; List < int > digits = new List < int > ( ) ; while ( N > 0 ) { digits . Add ( N % 10 ) ; N /= 10 ; } digits . Reverse ( ) ; int D = digits . Count ; for ( int i = 1 ; i <= D ; i ++ ) { int res = getPower ( i ) ; if ( i == D ) { for ( int p = 1 ; p <= D ; p ++ ) { int x = digits [ p - 1 ] ; int 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 ; } static void countNumbers ( int L , int R ) { Console . WriteLine ( countNumbersUtil ( R ) - countNumbersUtil ( L - 1 ) ) ; } public static void Main ( String [ ] args ) { int L = 128 , R = 162 ; countNumbers ( L , R ) ; } }
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
using System ; class GFG { static 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 ; } public static void Main ( String [ ] args ) { int N = 6 ; Console . Write ( alternatingSumOfFirst_N ( N ) ) ; } }
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 readonly sum ; Driver Code ; Given N ; Function Call
using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int findSum ( int N ) { int sum = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( gcd ( i , N ) == 1 ) { sum += i ; } } return sum ; } public static void Main ( String [ ] args ) { int N = 5 ; Console . Write ( findSum ( N ) ) ; } }
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
using System ; using System . Collections . Generic ; class GFG { static void solve ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } } int cnt = 0 ; foreach ( KeyValuePair < int , int > x in mp ) { cnt += ( ( x . Value ) * ( x . Value - 1 ) / 2 ) ; } int [ ] ans = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { ans [ i ] = cnt - ( mp [ arr [ i ] ] - 1 ) ; } for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 1 , 2 } ; int N = arr . Length ; solve ( arr , N ) ; } }
Mode in a stream of integers ( running integers ) | C # implementation of the above approach ; Function that prints the Mode values ; Map used to map 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
using System ; using System . Collections . Generic ; class GFG { public static void findMode ( int [ ] a , int n ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; int max = 0 ; int mode = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( map . ContainsKey ( a [ i ] ) ) { map [ a [ i ] ] = map [ a [ i ] ] + 1 ; } else { map . Add ( a [ i ] , 1 ) ; } if ( map [ a [ i ] ] >= max ) { max = map [ a [ i ] ] ; mode = a [ i ] ; } Console . Write ( mode ) ; Console . Write ( " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 3 , 2 , 5 } ; int n = arr . Length ; findMode ( arr , n ) ; } }
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
using System . Collections . Generic ; using System ; class GFG { static long modexp ( long x , long n , long 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 ) ; } } static long modInverse ( long x , long m ) { return modexp ( x , m - 2 , m ) ; } static void countNumbers ( string N ) { long m = 1000000007 ; long [ ] factorial = new long [ 100001 ] ; factorial [ 0 ] = 1 ; for ( int i = 1 ; i < 100001 ; i ++ ) { factorial [ i ] = ( factorial [ i - 1 ] * i ) % m ; } long [ ] count = new long [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) { count [ i ] = 0 ; } long length = N . Length ; for ( int i = 0 ; i < length ; i ++ ) count [ N [ i ] - '0' ] ++ ; long result = factorial [ ( int ) length ] ; for ( int i = 0 ; i < 10 ; i ++ ) { result = ( result * modInverse ( factorial [ ( int ) count [ i ] ] , m ) ) % m ; } Console . WriteLine ( result ) ; } public static void Main ( ) { string N = "0223" ; countNumbers ( N ) ; } }