codeparrot/xlcost-text-to-code huggingface dataset:
Modify the string such that it contains all vowels at least once | C # implementation of the above approach ; All vowels ; List to store distinct vowels ; If length of String is less than 5 then always Impossible ; Storing the distinct vowels in the String by checking if it in the list of String and not in the list of distinct vowels ; Storing the vowels which are not present in the String ; No replacement needed condition ; Replacing the characters to get all Vowels ; Copy th rest of the string ; Driver Code
using System ; using System . Collections ; class GFG { static bool find ( char [ ] x , char c ) { for ( int i = 0 ; i < x . Length ; i ++ ) if ( x [ i ] == c ) return true ; return false ; } static bool find ( ArrayList v , char c ) { for ( int i = 0 ; i < v . Count ; i ++ ) if ( ( char ) v [ i ] == c ) return true ; return false ; } static void addAllVowel ( string str ) { char [ ] x = { ' A ' , ' E ' , ' I ' , ' O ' , ' U ' } ; ArrayList y = new ArrayList ( ) ; int length = str . Length ; if ( length < 5 ) Console . Write ( " Impossible " ) ; else { for ( int i = 0 ; i < length ; i ++ ) { if ( find ( x , str [ i ] ) && ! find ( y , str [ i ] ) ) y . Add ( str [ i ] ) ; } ArrayList z = new ArrayList ( ) ; for ( int i = 0 ; i < 5 ; i ++ ) if ( ! find ( y , x [ i ] ) ) z . Add ( x [ i ] ) ; if ( z . Count == 0 ) Console . Write ( str ) ; else { int cc = 0 ; ArrayList y1 = new ArrayList ( ) ; string ans = " " ; for ( int i = 0 ; i < length ; i ++ ) { if ( find ( x , str [ i ] ) && ! find ( y1 , str [ i ] ) ) { ans += str [ i ] ; y1 . Add ( str [ i ] ) ; } else { ans += ( char ) z [ cc ] ; cc ++ ; } if ( cc == z . Count ) { for ( int j = i + 1 ; j < length ; j ++ ) ans += str [ j ] ; break ; } } Console . Write ( ans ) ; } } } public static void Main ( string [ ] args ) { string str = " ABCDEFGHI " ; addAllVowel ( str ) ; } }
Sum of minimum difference between consecutive elements of an array | C # program for finding the minimum sum of difference between consecutive elements ; function to find minimum sum of difference of consecutive element ; ul to store upper limit ll to store lower limit ; storethe lower range in ll and upper range in ul ; initialize the answer with 0 ; iterate for all ranges ; case 1 , in this case the difference will be 0 ; change upper limit and lower limit ; case 2 ; store the difference ; case 3 ; store the difference ; Driver code ; array of range
using System ; class GFG { static int solve ( int [ , ] v , int n ) { int ans , ul , ll ; int first = 0 ; int second = 1 ; ll = v [ 0 , first ] ; ul = v [ 0 , second ] ; ans = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( ( v [ i , first ] <= ul && v [ i , first ] >= ll ) || ( v [ i , second ] >= ll && v [ i , second ] <= ul ) ) { if ( v [ i , first ] > ll ) { ll = v [ i , first ] ; } if ( v [ i , second ] < ul ) { ul = v [ i , second ] ; } } else if ( v [ i , first ] > ul ) { ans += Math . Abs ( ul - v [ i , first ] ) ; ul = v [ i , first ] ; ll = v [ i , first ] ; } else if ( v [ i , second ] < ll ) { ans += Math . Abs ( ll - v [ i , second ] ) ; ul = v [ i , second ] ; ll = v [ i , second ] ; } } return ans ; } static void Main ( ) { int [ , ] v = new int [ 5 , 2 ] { { 1 , 3 } , { 2 , 5 } , { 6 , 8 } , { 1 , 2 } , { 2 , 3 } } ; int n = 5 ; Console . WriteLine ( solve ( v , n ) ) ; } }
Find the Largest Cube formed by Deleting minimum Digits from a number | C # code to implement maximum perfect cube formed after deleting minimum digits ; Returns vector of Pre Processed perfect cubes ; convert the cube to String and push into preProcessedCubes vector ; Utility function for findLargestCube ( ) . Returns the Largest cube number that can be formed ; reverse the preProcessed cubes so that we have the largest cube in the beginning of the vector ; iterate over all cubes ; check if the current digit of the cube matches with that of the number num ; if control reaches here , the its not possible to form a perfect cube ; wrapper for findLargestCubeUtil ( ) ; pre process perfect cubes ; convert number n to String ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static List < String > preProcess ( int n ) { List < String > preProcessedCubes = new List < String > ( ) ; for ( int i = 1 ; i * i * i <= n ; i ++ ) { int iThCube = i * i * i ; String cubeString = String . Join ( " " , iThCube ) ; preProcessedCubes . Add ( cubeString ) ; } return preProcessedCubes ; } static String findLargestCubeUtil ( String num , List < String > preProcessedCubes ) { preProcessedCubes . Reverse ( ) ; int totalCubes = preProcessedCubes . Count ; for ( int i = 0 ; i < totalCubes ; i ++ ) { String currCube = preProcessedCubes [ i ] ; int digitsInCube = currCube . Length ; int index = 0 ; int digitsInNumber = num . Length ; for ( int j = 0 ; j < digitsInNumber ; j ++ ) { if ( num [ j ] == currCube [ index ] ) { index ++ ; } if ( digitsInCube == index ) { return currCube ; } } } return " Not ▁ Possible " ; } static void findLargestCube ( int n ) { List < String > preProcessedCubes = preProcess ( n ) ; String num = String . Join ( " " , n ) ; String ans = findLargestCubeUtil ( num , preProcessedCubes ) ; Console . WriteLine ( " Largest ▁ Cube ▁ that ▁ can ▁ be ▁ formed ▁ from ▁ " + n + " ▁ is ▁ " + ans ) ; } public static void Main ( String [ ] args ) { int n ; n = 4125 ; findLargestCube ( n ) ; n = 876 ; findLargestCube ( n ) ; } }
Water Connection Problem | C # program to find efficient solution for the network ; number of houses and number of pipes ; Array rd stores the ending vertex of pipe ; Array wd stores the value of diameters between two pipes ; Array cd stores the starting end of pipe ; arraylist a , b , c are used to store the final output ; Function to perform calculations . ; If a pipe has no ending vertex but has starting vertex i . e is an outgoing pipe then we need to start DFS with this vertex . ; We put the details of component in final output array ; set the value of the araray to zero
using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static int n , p ; static int [ ] rd = new int [ 1100 ] ; static int [ ] wt = new int [ 1100 ] ; static int [ ] cd = new int [ 1100 ] ; static List < int > a = new List < int > ( ) ; static List < int > b = new List < int > ( ) ; static List < int > c = new List < int > ( ) ; static int ans ; static int dfs ( int w ) { if ( cd [ w ] == 0 ) return w ; if ( wt [ w ] < ans ) ans = wt [ w ] ; return dfs ( cd [ w ] ) ; } static void solve ( int [ , ] arr ) { int i = 0 ; while ( i < p ) { int q = arr [ i , 0 ] ; int h = arr [ i , 1 ] ; int t = arr [ i , 2 ] ; cd [ q ] = h ; wt [ q ] = t ; rd [ h ] = q ; i ++ ; } a = new List < int > ( ) ; b = new List < int > ( ) ; c = new List < int > ( ) ; for ( int j = 1 ; j <= n ; ++ j ) if ( rd [ j ] == 0 && cd [ j ] > 0 ) { ans = 1000000000 ; int w = dfs ( j ) ; a . Add ( j ) ; b . Add ( w ) ; c . Add ( ans ) ; } Console . WriteLine ( a . Count ) ; for ( int j = 0 ; j < a . Count ; ++ j ) Console . WriteLine ( a [ j ] + " ▁ " + b [ j ] + " ▁ " + c [ j ] ) ; } for ( int i = 0 ; i < 1100 ; i ++ ) [ i ] = cd [ i ] = wt [ i ] = 0 ; int [ , ] arr = { { 7 , 4 , 98 } , { 5 , 9 , 72 } , { 4 , 6 , 10 } , { 2 , 8 , 22 } , { 9 , 7 , 17 } , { 3 , 1 , 66 } } ; solve ( arr ) ; } }
Print a closest string that does not contain adjacent duplicates | C # program to print a string with no adjacent duplicates by doing minimum changes to original string ; Function to print simple string ; If any two adjacent characters are equal ; Initialize it to ' a ' ; Traverse the loop until it is different from the left and right letter . ; Driver function
using System ; class GfG { public static String noAdjacentDup ( String s1 ) { int n = s1 . Length ; char [ ] s = s1 . ToCharArray ( ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == s [ i - 1 ] ) { s [ i ] = ' a ' ; while ( s [ i ] == s [ i - 1 ] || ( i + 1 < n && s [ i ] == s [ i + 1 ] ) ) s [ i ] ++ ; i ++ ; } } return ( new String ( s ) ) ; } public static void Main ( String [ ] argc ) { String s = " geeksforgeeks " ; Console . Write ( noAdjacentDup ( s ) ) ; } }
Array element moved by k using single moves | C # program to find winner of game ; if the number of steps is more then n - 1 , ; initially the best is 0 and no of wins is 0. ; traverse through all the numbers ; if the value of array is more then that of previous best ; best is replaced by a [ i ] ; if not the first index ; no of wins is 1 now ; if it wins ; if any position has more then k wins then return ; Maximum element will be winner because we move smaller element at end and repeat the process . ; driver program to test the above function
using System ; class GFG { static int winner ( int [ ] a , int n , int k ) { if ( k >= n - 1 ) return n ; int best = 0 , times = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > best ) { best = a [ i ] ; if ( i == 1 ) times = 1 ; } else times += 1 ; if ( times >= k ) return best ; } return best ; } public static void Main ( ) { int [ ] a = { 2 , 1 , 3 , 4 , 5 } ; int n = a . Length ; int k = 2 ; Console . WriteLine ( winner ( a , n , k ) ) ; } }
Paper Cut into Minimum Number of Squares | C # program to find minimum number of squares to cut a paper . ; To swap two numbers ; Returns min number of squares needed ; swap if a is small size side . ; Iterate until small size side is greater then 0 ; Update result ; Driver code
using System ; class GFG { static void swap ( int a , int b ) { int temp = a ; a = b ; b = temp ; } static int minimumSquare ( int a , int b ) { int result = 0 , rem = 0 ; if ( a < b ) swap ( a , b ) ; while ( b > 0 ) { result += a / b ; rem = a % b ; a = b ; b = rem ; } return result ; } public static void Main ( String [ ] args ) { int n = 13 , m = 29 ; Console . WriteLine ( minimumSquare ( n , m ) ) ; } }
Count of non decreasing Arrays with ith element in range [ A [ i ] , B [ i ] ] | C # program for the above approach ; Function to count the total number of possible valid arrays ; Make a 2D DP table ; Make a 2D prefix sum table ; Initialize all values to 0 ; Base Case ; Initialize the prefix values ; Iterate over the range and update the dp table accordingly ; Add the dp values to the prefix sum ; Update the prefix sum table ; Find the result count of arrays formed ; Return the total count of arrays ; Driver Code
using System ; class GFG { static int totalValidArrays ( int [ ] a , int [ ] b , int N ) { int [ , ] dp = new int [ N + 1 , b [ N - 1 ] + 1 ] ; int [ , ] pref = new int [ N + 1 , b [ N - 1 ] + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) for ( int j = 0 ; j < b [ N - 1 ] + 1 ; j ++ ) dp [ i , j ] = 0 ; for ( int i = 0 ; i < N + 1 ; i ++ ) for ( int j = 0 ; j < b [ N - 1 ] + 1 ; j ++ ) pref [ i , j ] = 0 ; dp [ 0 , 0 ] = 1 ; for ( int i = 0 ; i <= b [ N - 1 ] ; i ++ ) { pref [ 0 , i ] = 1 ; } for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = a [ i - 1 ] ; j <= b [ i - 1 ] ; j ++ ) { dp [ i , j ] += pref [ i - 1 , j ] ; pref [ i , j ] += dp [ i , j ] ; } for ( int j = 0 ; j <= b [ N - 1 ] ; j ++ ) { if ( j > 0 ) { pref [ i , j ] += pref [ i , j - 1 ] ; } } } int ans = 0 ; for ( int i = a [ N - 1 ] ; i <= b [ N - 1 ] ; i ++ ) { ans += dp [ N , i ] ; } return ans ; } public static void Main ( ) { int [ ] A = { 1 , 1 } ; int [ ] B = { 2 , 3 } ; int N = A . Length ; Console . WriteLine ( totalValidArrays ( A , B , N ) ) ; } }
Count of integers in range [ L , R ] having even frequency of each digit | C # program for the above approach ; Stores the upper limit of the range ; Stores the overlapping states ; Recursive Function to calculate the count of valid integers in the range [ 1 , s ] using memoization ; Base Case ; If current integer has even count of digits and is not repeated ; If current state is already considered ; Stores the maximum valid digit at the current index ; Stores the count of valid integers ; If the current digit is not the most significant digit , i . e , the integer is already started ; Iterate through all valid digits ; Recursive call for ith digit at the current index ; Recursive call for integers having leading zeroes in the beginning ; Iterate through all valid digits as most significant digits ; Recursive call for ith digit at the current index ; Return answer ; Function to calculate valid number in the range [ 1 , X ] ; Initialize dp array with - 1 ; Store the range in form of string ; Return Count ; Function to find the count of integers in the range [ L , R ] such that the frequency of each digit is even ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static string s ; static int [ , , , ] dp = new int [ 1024 , 10 , 2 , 2 ] ; static int calcCnt ( int mask , int len , int smaller , int started ) { if ( len == s . Length ) { if ( mask == 0 && started != 0 ) return 1 ; else return 0 ; } if ( dp [ mask , len , smaller , started ] != - 1 ) return dp [ mask , len , smaller , started ] ; int mx = 9 ; if ( smaller == 0 ) { mx = ( int ) s [ len ] - 48 ; } int ans = 0 ; if ( started != 0 ) { for ( int i = 0 ; i <= mx ; i ++ ) { ans += calcCnt ( mask ^ ( 1 << i ) , len + 1 , Convert . ToBoolean ( smaller ) || ( i < ( int ) s [ len ] - 48 ) ? 1 : 0 , 1 ) ; } } else { ans = calcCnt ( mask , len + 1 , 1 , 0 ) ; for ( int i = 1 ; i <= mx ; i ++ ) { ans += calcCnt ( mask ^ ( 1 << i ) , len + 1 , Convert . ToBoolean ( smaller ) || ( i < ( int ) s [ len ] - 48 ) ? 1 : 0 , 1 ) ; } } dp [ mask , len , smaller , started ] = ans ; return ans ; } static int countInt ( int x ) { for ( int i = 0 ; i < 1024 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) { for ( int l = 0 ; l < 2 ; l ++ ) dp [ i , j , k , l ] = - 1 ; } } } s = x . ToString ( ) ; return calcCnt ( 0 , 0 , 0 , 0 ) ; } static int countIntInRange ( int L , int R ) { return countInt ( R ) - countInt ( L - 1 ) ; } public static void Main ( ) { int L = 32 , R = 1010 ; Console . Write ( countIntInRange ( L , R ) ) ; } }
Queries to find minimum absolute difference between adjacent array elements in given ranges | C # program for the above approach ; Structure for query range ; Function to find the minimum difference between adjacent array element over the given range [ L , R ] for Q Queries ; Find the sum of all queries ; Left and right boundaries of current range ; Print the sum of the current query range ; Function to find the minimum absolute difference of adjacent array elements for the given range ; Stores the absolute difference of adjacent elements ; Find the minimum difference of adjacent elements ; Driver Code
using System ; using System . Collections . Generic ; class GFG { class Query { public int L , R ; public Query ( int l , int r ) { this . L = l ; this . R = r ; } } ; static int MAX = 5000 ; static void minDifference ( int [ ] arr , int n , Query [ ] q , int m ) { for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] . L , R = q [ i ] . R ; int ans = MAX ; for ( int j = L ; j < R ; j ++ ) { ans = Math . Min ( ans , arr [ j ] ) ; } Console . WriteLine ( ans ) ; } } static void minimumDifference ( int [ ] arr , Query [ ] q , int N , int m ) { int [ ] diff = new int [ N ] ; for ( int i = 0 ; i < N - 1 ; i ++ ) diff [ i ] = Math . Abs ( arr [ i ] - arr [ i + 1 ] ) ; minDifference ( diff , N - 1 , q , m ) ; } public static void Main ( ) { int [ ] arr = { 2 , 6 , 1 , 8 , 3 , 4 } ; int N = arr . Length ; Query [ ] Q = { new Query ( 0 , 3 ) , new Query ( 1 , 5 ) , new Query ( 4 , 5 ) } ; int M = Q . Length ; minimumDifference ( arr , Q , N , M ) ; } }
Count of N digit numbers which contains all single digit primes | C # program for the above approach ; Stores the index of prime numbers ; Function ; If index = N + 1 ; Find count of distinct prime numbers by counting number of set bits . ; If count of distinct prime numbers is equal to 4 return 1. ; If the state has already been computed ; If current position is 1 , then any digit from [ 1 - 9 ] can be placed . If N = 1 , 0 can be also placed . ; If the digit is a prime number , set the index of the digit to 1 in the bitmask . ; For remaining positions , any digit from [ 0 - 9 ] can be placed ; If the digit is a prime number , set the index of the digit to 1 in the bitmask . ; Return the answer . ; Driver Code ; Initializing dp array with - 1. ; Indexing prime numbers in ascending order ; Given Input ; Function call
using System ; using System . Collections . Generic ; public class GFG { public static int [ , ] dp = new int [ 100 , ( 1 << 4 ) ] ; public static Dictionary < int , int > primeIndex = new Dictionary < int , int > ( ) ; public static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } public static int countOfNumbers ( int index , int mask , int N ) { if ( index == N + 1 ) { int countOfPrimes = countSetBits ( mask ) ; if ( countOfPrimes == 4 ) { return 1 ; } return 0 ; } int val = dp [ index , mask ] ; if ( val != - 1 ) { return val ; } val = 0 ; if ( index == 1 ) { for ( int digit = ( N == 1 ? 0 : 1 ) ; digit <= 9 ; ++ digit ) { if ( primeIndex . ContainsKey ( digit ) ) { int newMask = mask | ( 1 << primeIndex [ digit ] ) ; val += countOfNumbers ( index + 1 , newMask , N ) ; } else { val += countOfNumbers ( index + 1 , mask , N ) ; } } } else { for ( int digit = 0 ; digit <= 9 ; ++ digit ) { if ( primeIndex . ContainsKey ( digit ) ) { int newMask = mask | ( 1 << primeIndex [ digit ] ) ; val += countOfNumbers ( index + 1 , newMask , N ) ; } else { val += countOfNumbers ( index + 1 , mask , N ) ; } } } return val ; } public static void Main ( String [ ] args ) { for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < ( 1 << 4 ) ; j ++ ) { dp [ i , j ] = - 1 ; } } primeIndex . Add ( 2 , 0 ) ; primeIndex . Add ( 3 , 1 ) ; primeIndex . Add ( 5 , 2 ) ; primeIndex . Add ( 7 , 3 ) ; int N = 4 ; Console . WriteLine ( countOfNumbers ( 1 , 0 , N ) ) ; } }
Count of N | C # program for the above approach ; Stores the dp - states ; Returns number of set bits in a number ; Recursive Function to find number of N - digit numbers which has equal count of distinct odd & even digits ; If index is N + 1 ; Find the count of set bits in the evenMask ; Find the count of set bits in the oddMask ; If the count of set bits in both masks are equal then return 1 as they have equal number of distinct odd and even digits ; If the state has already been computed ; If N = 1 , 0 can be also placed ; If digit is odd ; Set the ( digit / 2 ) th bit of the oddMask ; Set the ( digit / 2 ) th bit of the number evenMask ; For remaining positions , any digit from [ 0 - 9 ] can be placed ; If digit is odd ; Set the ( digit / 2 ) th bit of oddMask ; Set the ( digit / 2 ) th bit of evenMask ; Return the answer ; Function to find number of N - digit numbers which has equal count of distinct odd and even digits ; Initialize dp array with - 1 ; Function Call ; Driver Code
using System ; class GFG { static int [ , , ] dp = new int [ 100 , 1 << 5 , 1 << 5 ] ; static int __builtin_popcount ( int n ) { int d , t = 0 ; while ( n > 0 ) { d = n % 2 ; n = n / 2 ; if ( d == 1 ) t ++ ; } return t ; } static int countOfNumbers ( int index , int evenMask , int oddMask , int N ) { if ( index == N + 1 ) { int countOfEvenDigits = __builtin_popcount ( evenMask ) ; int countOfOddDigits = __builtin_popcount ( oddMask ) ; if ( countOfOddDigits == countOfEvenDigits ) { return 1 ; } return 0 ; } int val = dp [ index , evenMask , oddMask ] ; if ( val != - 1 ) return val ; val = 0 ; if ( index == 1 ) { for ( int digit = ( N == 1 ? 0 : 1 ) ; digit <= 9 ; ++ digit ) { if ( ( digit & 1 ) != 0 ) { val += countOfNumbers ( index + 1 , evenMask , oddMask | ( 1 << ( digit / 2 ) ) , N ) ; } else { val += countOfNumbers ( index + 1 , evenMask | ( 1 << ( digit / 2 ) ) , oddMask , N ) ; } } } else { for ( int digit = 0 ; digit <= 9 ; ++ digit ) { if ( ( digit & 1 ) != 0 ) { val += countOfNumbers ( index + 1 , evenMask , oddMask | ( 1 << ( digit / 2 ) ) , N ) ; } else { val += countOfNumbers ( index + 1 , evenMask | ( 1 << ( digit / 2 ) ) , oddMask , N ) ; } } } return val ; } static void countNDigitNumber ( int N ) { for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < ( 1 << 5 ) ; j ++ ) { for ( int k = 0 ; k < ( 1 << 5 ) ; k ++ ) { dp [ i , j , k ] = - 1 ; } } } Console . Write ( countOfNumbers ( 1 , 0 , 0 , N ) ) ; } public static void Main ( ) { int N = 3 ; countNDigitNumber ( N ) ; } }
Minimum number of flips or swaps of adjacent characters required to make two strings equal | C # program for the above approach ; Function to count the minimum number of operations required to make strings A and B equal ; Stores all dp - states ; Update the value of A [ i ] ; Iterate over the range [ 1 , N ] ; If A [ i - 1 ] equals to B [ i - 1 ] ; Assign Dp [ i - 1 ] to Dp [ i ] ; Otherwise ; Update dp [ i ] ; If swapping is possible ; Update dp [ i ] ; Return the minimum number of steps required ; Driver code ; Given Input ; Function Call
using System ; class GFG { static int countMinSteps ( string A , string B , int N ) { int [ ] dp = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) { dp [ i ] = 0 ; } for ( int i = 1 ; i <= N ; i ++ ) { if ( A [ i - 1 ] == B [ i - 1 ] ) { dp [ i ] = dp [ i - 1 ] ; } else { dp [ i ] = dp [ i - 1 ] + 1 ; } if ( i >= 2 && A [ i - 2 ] == B [ i - 1 ] && A [ i - 1 ] == B [ i - 2 ] ) { dp [ i ] = Math . Min ( dp [ i ] , dp [ i - 2 ] + 1 ) ; } } return dp [ N ] ; } public static void Main ( String [ ] args ) { string A = "0101" ; string B = "0011" ; int N = A . Length ; Console . Write ( countMinSteps ( A , B , N ) ) ; } }
Count of N | C # program for the above approach ; Function to find the number of N digit numbers such that at least one digit occurs more than once ; Base Case ; If repeated is true , then for remaining positions any digit can be placed ; If the current state has already been computed , then return it ; Stores the count of number for the current recursive calls ; If n = 1 , 0 can be also placed ; If a digit has occurred for the second time , then set repeated to 1 ; Otherwise ; For remaining positions any digit can be placed ; If a digit has occurred for the second time , then set repeated to 1 ; Return the resultant count for the current recursive call ; Function to count all the N - digit numbers having at least one digit 's occurrence more than once ; Initialize dp array with - 1 ; Function to count all possible number satisfying the given criteria ; Driver Code
using System ; class GFG { public static int [ , , ] dp = new int [ 50 , 1 << 10 , 2 ] ; public static int countOfNumbers ( int digit , int mask , int repeated , int n ) { if ( digit == n + 1 ) { if ( repeated == 1 ) { return 1 ; } return 0 ; } if ( repeated == 1 ) { return ( int ) Math . Pow ( 10 , n - digit + 1 ) ; } int val = dp [ digit , mask , repeated ] ; if ( val != - 1 ) { return val ; } val = 0 ; if ( digit == 1 ) { for ( int i = ( n == 1 ? 0 : 1 ) ; i <= 9 ; ++ i ) { if ( ( mask & ( 1 << i ) ) > 0 ) { val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 1 , n ) ; } else { val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 0 , n ) ; } } } else { for ( int i = 0 ; i <= 9 ; ++ i ) { if ( ( mask & ( 1 << i ) ) > 0 ) { val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 1 , n ) ; } else { val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 0 , n ) ; } } } return val ; } public static void countNDigitNumber ( int N ) { for ( int i = 0 ; i < 50 ; i ++ ) { for ( int j = 0 ; j < 1 << 10 ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) { dp [ i , j , k ] = - 1 ; } } } Console . Write ( countOfNumbers ( 1 , 0 , 0 , N ) ) ; } public static void Main ( ) { int N = 2 ; countNDigitNumber ( N ) ; } }