codeparrot/xlcost-text-to-code huggingface dataset:
Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix | C # program to find the maximum distance from a 0 - cell to a 1 - cell ; Function to find the maximum distance ; Set up top row and left column ; Pass one : top left to bottom right ; Check if there was no " One " Cell ; Set up top row and left column ; Past two : bottom right to top left ; Driver code
using System ; class GFG { static int maxDistance ( int [ , ] grid ) { if ( grid . GetLength ( 0 ) == 0 ) return - 1 ; int N = grid . GetLength ( 0 ) ; int INF = 1000000 ; grid [ 0 , 0 ] = grid [ 0 , 0 ] == 1 ? 0 : INF ; for ( int i = 1 ; i < N ; i ++ ) grid [ 0 , i ] = grid [ 0 , i ] == 1 ? 0 : grid [ 0 , i - 1 ] + 1 ; for ( int i = 1 ; i < N ; i ++ ) grid [ i , 0 ] = grid [ i , 0 ] == 1 ? 0 : grid [ i - 1 , 0 ] + 1 ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j < N ; j ++ ) { grid [ i , j ] = grid [ i , j ] == 1 ? 0 : Math . Min ( grid [ i - 1 , j ] , grid [ i , j - 1 ] ) + 1 ; } } if ( grid [ N - 1 , N - 1 ] >= INF ) return - 1 ; int maxi = grid [ N - 1 , N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) { grid [ N - 1 , i ] = Math . Min ( grid [ N - 1 , i ] , grid [ N - 1 , i + 1 ] + 1 ) ; maxi = Math . Max ( grid [ N - 1 , i ] , maxi ) ; } for ( int i = N - 2 ; i >= 0 ; i -- ) { grid [ i , N - 1 ] = Math . Min ( grid [ i , N - 1 ] , grid [ i + 1 , N - 1 ] + 1 ) ; maxi = Math . Max ( grid [ i , N - 1 ] , maxi ) ; } for ( int i = N - 2 ; i >= 0 ; i -- ) { for ( int j = N - 2 ; j >= 0 ; j -- ) { grid [ i , j ] = Math . Min ( grid [ i , j ] , Math . Min ( grid [ i + 1 , j ] + 1 , grid [ i , j + 1 ] + 1 ) ) ; maxi = Math . Max ( grid [ i , j ] , maxi ) ; } } return maxi == 0 ? - 1 : maxi ; } public static void Main ( ) { int [ , ] arr = { { 0 , 0 , 1 } , { 0 , 0 , 0 } , { 0 , 0 , 0 } } ; Console . WriteLine ( maxDistance ( arr ) ) ; } }
Minimize total cost without repeating same task in two consecutive iterations | C # implementation of the above approach ; Function to return the minimum cost for N iterations ; Construct the dp table ; 1 st row of dp table will be equal to the 1 st of cost matrix ; Iterate through all the rows ; To iterate through the columns of current row ; Initialize val as infinity ; To iterate through the columns of previous row ; Fill the dp matrix ; Returning the minimum value ; Driver code ; Number of iterations ; Number of tasks ; Cost matrix
using System ; class GFG { static int findCost ( int [ , ] cost_mat , int N , int M ) { int [ , ] dp = new int [ N , M ] ; for ( int i = 0 ; i < M ; i ++ ) dp [ 0 , i ] = cost_mat [ 0 , i ] ; for ( int row = 1 ; row < N ; row ++ ) { for ( int curr_col = 0 ; curr_col < M ; curr_col ++ ) { int val = 999999999 ; for ( int prev_col = 0 ; prev_col < M ; prev_col ++ ) { if ( curr_col != prev_col ) val = Math . Min ( val , dp [ row - 1 , prev_col ] ) ; } dp [ row , curr_col ] = val + cost_mat [ row , curr_col ] ; } } int ans = int . MaxValue ; for ( int i = 0 ; i < M ; i ++ ) ans = Math . Min ( ans , dp [ N - 1 , i ] ) ; return ans ; } public static void Main ( string [ ] args ) { int N = 4 ; int M = 4 ; int [ , ] cost_mat = { { 4 , 5 , 3 , 2 } , { 6 , 2 , 8 , 1 } , { 6 , 2 , 2 , 1 } , { 0 , 5 , 5 , 1 } } ; Console . WriteLine ( findCost ( cost_mat , N , M ) ) ; } }
Split the given string into Primes : Digit DP | C # implementation of the above approach ; Function to precompute all the primes upto 1000000 and store it in a set using Sieve of Eratosthenes ; Converting int to String ; A function to find the minimum number of segments the given String can be divided such that every segment is a prime ; Declare a splitdp [ ] array and initialize to - 1 ; Call sieve function to store primes in primes array ; Build the DP table in a bottom - up manner ; If the prefix is prime then the prefix will be found in the prime set ; If the given prefix can be split into primes , then for the remaining String from i to j check if prime . If yes calculate the minimum split till j ; To check if the subString from i to j is a prime number or not ; If it is a prime , then update the dp array ; Return the minimum number of splits for the entire String ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void getPrimesFromSeive ( HashSet < String > primes ) { bool [ ] prime = new bool [ 1000001 ] ; for ( int i = 0 ; i < 1000001 ; i ++ ) prime [ i ] = true ; prime [ 0 ] = prime [ 1 ] = false ; for ( int i = 2 ; i * i <= 1000000 ; i ++ ) { if ( prime [ i ] == true ) { for ( int j = i * i ; j <= 1000000 ; j += i ) prime [ j ] = false ; } } for ( int i = 2 ; i <= 1000000 ; i ++ ) { if ( prime [ i ] == true ) primes . Add ( String . Join ( " " , i ) ) ; } } static int splitIntoPrimes ( String number ) { int numLen = number . Length ; int [ ] splitDP = new int [ numLen + 1 ] ; for ( int i = 0 ; i < numLen + 1 ; i ++ ) splitDP [ i ] = - 1 ; HashSet < String > primes = new HashSet < String > ( ) ; getPrimesFromSeive ( primes ) ; for ( int i = 1 ; i <= numLen ; i ++ ) { if ( i <= 6 && ( primes . Contains ( number . Substring ( 0 , i ) ) ) ) splitDP [ i ] = 1 ; if ( splitDP [ i ] != - 1 ) { for ( int j = 1 ; j <= 6 && i + j <= numLen ; j ++ ) { if ( primes . Contains ( number . Substring ( i , j ) ) ) { if ( splitDP [ i + j ] == - 1 ) splitDP [ i + j ] = 1 + splitDP [ i ] ; else splitDP [ i + j ] = Math . Min ( splitDP [ i + j ] , 1 + splitDP [ i ] ) ; } } } } return splitDP [ numLen ] ; } public static void Main ( String [ ] args ) { Console . Write ( splitIntoPrimes ( "13499315" ) + " STRNEWLINE " ) ; Console . Write ( splitIntoPrimes ( "43" ) + " STRNEWLINE " ) ; } }
Connect Nodes at same Level ( Level Order Traversal ) | Connect nodes at same level using level order traversal . ; Node class ; Sets nextRight of all nodes of a tree ; null marker to represent end of current level ; Do Level order of tree using NULL markers ; next element in queue represents next node at current Level ; push left and right children of current node ; if queue is not empty , push NULL to mark nodes at this level are visited ; Driver code ; Constructed binary tree is 10 / \ 8 2 / \ 3 90 ; Populates nextRight pointer in all nodes ; Let us check the values of nextRight pointers
using System ; using System . Collections . Generic ; public class Connect_node_same_level { class Node { public int data ; public Node left , right , nextRight ; public Node ( int data ) { this . data = data ; left = null ; right = null ; nextRight = null ; } } ; static void connect ( Node root ) { Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; q . Enqueue ( null ) ; while ( q . Count != 0 ) { Node p = q . Peek ( ) ; q . Dequeue ( ) ; if ( p != null ) { p . nextRight = q . Peek ( ) ; if ( p . left != null ) q . Enqueue ( p . left ) ; if ( p . right != null ) q . Enqueue ( p . right ) ; } else if ( q . Count != 0 ) q . Enqueue ( null ) ; } } public static void Main ( ) { Node root = new Node ( 10 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 2 ) ; root . left . left = new Node ( 3 ) ; root . right . right = new Node ( 90 ) ; connect ( root ) ; Console . WriteLine ( " Following ▁ are ▁ populated ▁ nextRight ▁ pointers ▁ in ▁ STRNEWLINE " + " the ▁ tree ▁ ( -1 ▁ is ▁ printed ▁ if ▁ there ▁ is ▁ no ▁ nextRight ) " ) ; Console . WriteLine ( " nextRight ▁ of ▁ " + root . data + " ▁ is ▁ " + ( ( root . nextRight != null ) ? root . nextRight . data : - 1 ) ) ; Console . WriteLine ( " nextRight ▁ of ▁ " + root . left . data + " ▁ is ▁ " + ( ( root . left . nextRight != null ) ? root . left . nextRight . data : - 1 ) ) ; Console . WriteLine ( " nextRight ▁ of ▁ " + root . right . data + " ▁ is ▁ " + ( ( root . right . nextRight != null ) ? root . right . nextRight . data : - 1 ) ) ; Console . WriteLine ( " nextRight ▁ of ▁ " + root . left . left . data + " ▁ is ▁ " + ( ( root . left . left . nextRight != null ) ? root . left . left . nextRight . data : - 1 ) ) ; Console . WriteLine ( " nextRight ▁ of ▁ " + root . right . right . data + " ▁ is ▁ " + ( ( root . right . right . nextRight != null ) ? root . right . right . nextRight . data : - 1 ) ) ; } }
Number of ways to convert a character X to a string Y | C # implementation of the approach ; Function to find the modular - inverse ; While power > 1 ; Updating s and a ; Updating power ; Return the final answer ; Function to return the count of ways ; To store the final answer ; To store pre - computed factorials ; Computing factorials ; Loop to find the occurrences of x and update the ans ; Multiplying the answer by ( n - 1 ) ! ; Return the final answer ; Driver code
using System ; class GFG { static int MOD = 1000000007 ; static long modInv ( long a ) { long p = MOD - 2 ; long s = 1 ; while ( p != 1 ) { if ( p % 2 == 1 ) s = ( s * a ) % MOD ; a = ( a * a ) % MOD ; p /= 2 ; } return ( a * s ) % MOD ; } static long findCnt ( char x , String y ) { long ans = 0 ; long [ ] fact = new long [ y . Length + 1 ] ; for ( int i = 0 ; i < y . Length + 1 ; i ++ ) fact [ i ] = 1 ; for ( int i = 1 ; i <= y . Length ; i ++ ) fact [ i ] = ( fact [ i - 1 ] * i ) % MOD ; for ( int i = 0 ; i < y . Length ; i ++ ) { if ( y [ i ] == x ) { ans += ( modInv ( fact [ i ] ) * modInv ( fact [ y . Length - i - 1 ] ) ) % MOD ; ans %= MOD ; } } ans *= fact [ ( y . Length - 1 ) ] ; ans %= MOD ; return ans ; } public static void Main ( ) { char x = ' a ' ; string y = " xxayy " ; Console . WriteLine ( findCnt ( x , y ) ) ; } }
Find the Largest divisor Subset in the Array | C # implementation of the approach ; Function to find the required subsequence ; Sort the array ; Keep a count of the length of the subsequence and the previous element ; Set the initial values ; Maximum length of the subsequence and the last element ; Run a loop for every element ; Check for all the divisors ; If the element is a divisor and the length of subsequence will increase by adding j as previous element of i ; Increase the count ; Update the max count ; Get the last index of the subsequence ; Print the element ; Move the index to the previous element ; Driver code
using System ; using System . Collections ; class GFG { static void findSubSeq ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int [ ] count = new int [ n ] ; int [ ] prev = new int [ n ] ; int i , j ; for ( i = 0 ; i < n ; i ++ ) count [ i ] = 1 ; for ( j = 0 ; j < n ; j ++ ) prev [ j ] = - 1 ; int max = 0 ; int maxprev = - 1 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] % arr [ j ] == 0 && count [ j ] + 1 > count [ i ] ) { count [ i ] = count [ j ] + 1 ; prev [ i ] = j ; } } if ( max < count [ i ] ) { max = count [ i ] ; maxprev = i ; } } i = maxprev ; while ( i >= 0 ) { if ( arr [ i ] != - 1 ) Console . Write ( arr [ i ] + " ▁ " ) ; i = prev [ i ] ; } } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; findSubSeq ( arr , n ) ; } }
Optimally accommodate 0 s and 1 s from a Binary String into K buckets | C # implementation of the approach ; Function to find the minimum required sum using dynamic programming ; dp [ i , j ] = minimum val of accommodation till j 'th index of the string  using i+1 number of buckets.  Final ans will be in dp[n-1, K-1] ; Corner cases ; Filling first row , if only 1 bucket then simple count number of zeros and ones and do the multiplication ; If k = 0 then this arrangement is not possible ; If no arrangement is possible then our answer will remain INT_MAX so return - 1 ; Driver code ; K buckets
using System ; class GFG { static long solve ( string str , int K ) { int n = str . Length ; long [ , ] dp = new long [ K , n ] ; if ( n < K ) return - 1 ; else if ( n == K ) return 0 ; long zeroes = 0 , ones = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) zeroes ++ ; else ones ++ ; dp [ 0 , i ] = ones * zeroes ; } for ( int s = 1 ; s < K ; s ++ ) { for ( int i = 0 ; i < n ; i ++ ) { dp [ s , i ] = Int32 . MaxValue ; ones = 0 ; zeroes = 0 ; for ( int k = i ; k >= 0 ; k -- ) { if ( str [ k ] == '0' ) zeroes ++ ; else ones ++ ; dp [ s , i ] = Math . Min ( dp [ s , i ] , + ( ( k - 1 >= 0 ) ? ones * zeroes + dp [ s - 1 , k - 1 ] : Int32 . MaxValue ) ) ; } } } return ( dp [ K - 1 , n - 1 ] == Int32 . MaxValue ) ? - 1 : dp [ K - 1 , n - 1 ] ; } public static void Main ( string [ ] args ) { string S = "0101" ; int K = 2 ; Console . WriteLine ( solve ( S , K ) ) ; } }
Queries to check if string B exists as substring in string A |  ; Dp Array ; String to character array ; initialize matrix with 1 ; for r from 1 to string length ; for c from 1 b string length ; For every query ; print true ; print false ; Driver Code
using System ; public class GFG { private static void substringCheck ( string stra , string strb , int [ ] query ) { int [ , ] matrix = new int [ strb . Length , stra . Length ] ; char [ ] charCrr = stra . ToCharArray ( ) ; char [ ] charRrr = strb . ToCharArray ( ) ; for ( int c = 0 ; c < stra . Length ; c ++ ) { if ( charRrr [ 0 ] == charCrr ) { matrix [ 0 , c ] = 1 ; } } for ( int r = 1 ; r < charRrr . Length ; r ++ ) { char ch = charRrr [ r ] ; for ( int c = 1 ; c < charCrr . Length ; c ++ ) { if ( ch == charCrr && matrix [ r - 1 , c - 1 ] == 1 ) { matrix [ r , c ] = 1 ; } } } foreach ( int q in query ) { int matLoc = ( q + ( strb . Length - 1 ) ) ; if ( matLoc >= stra . Length ) { Console . WriteLine ( false ) ; } else { if ( matrix [ strb . Length - 1 , matLoc ] == 1 ) { Console . WriteLine ( true ) ; } else { Console . WriteLine ( false ) ; } } } } public static void Main ( string [ ] args ) { string stra = " GeeksForGeeks " ; string strb = " Geeks " ; int [ ] query = { 0 , 5 , 8 } ; substringCheck ( stra , strb , query ) ; } }
Queries for bitwise AND in the index range [ L , R ] of the given array | C # implementation of the approach ; Array to store bit - wise prefix count ; Function to find the prefix sum ; Loop for each bit ; Loop to find prefix count ; Function to answer query ; To store the answer ; Loop for each bit ; To store the number of variables with ith bit set ; Condition for ith bit of answer to be set ; Driver code
using System ; class GFG { static int MAX = 100000 ; static int bitscount = 32 ; static int [ , ] prefix_count = new int [ bitscount , MAX ] ; static void findPrefixCount ( int [ ] arr , int n ) { for ( int i = 0 ; i < bitscount ; i ++ ) { prefix_count [ i , 0 ] = ( ( arr [ 0 ] >> i ) & 1 ) ; for ( int j = 1 ; j < n ; j ++ ) { prefix_count [ i , j ] = ( ( arr [ j ] >> i ) & 1 ) ; prefix_count [ i , j ] += prefix_count [ i , j - 1 ] ; } } } static int rangeAnd ( int l , int r ) { int ans = 0 ; for ( int i = 0 ; i < bitscount ; i ++ ) { int x ; if ( l == 0 ) x = prefix_count [ i , r ] ; else x = prefix_count [ i , r ] - prefix_count [ i , l - 1 ] ; if ( x == r - l + 1 ) ans = ( ans | ( 1 << i ) ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 5 , 3 , 5 , 2 , 3 } ; int n = arr . Length ; findPrefixCount ( arr , n ) ; int [ , ] queries = { { 1 , 3 } , { 4 , 5 } } ; int q = queries . GetLength ( 0 ) ; for ( int i = 0 ; i < q ; i ++ ) Console . WriteLine ( rangeAnd ( queries [ i , 0 ] , queries [ i , 1 ] ) ) ; } }
Longest path in a directed Acyclic graph | Dynamic Programming | C # program to find the longest path in the DAG ; graph class ; Function to traverse the DAG and apply Dynamic Programming to find the longest path ; Mark as visited ; Traverse for all its children ; If not visited ; Store the max of the paths ; Funciton to add an edge ; Function that returns the longest path ; Dp array ; Visited array to know if the node has been visited previously or not ; Call DFS for every unvisited vertex ; Traverse and find the maximum of all dp [ i ] ; Driver code ; Example - 1
using System ; using System . Collections . Generic ; class Graph { public int vertices ; public List < int > [ ] edge ; public Graph ( int vertices ) { this . vertices = vertices ; edge = new List < int > [ vertices + 1 ] ; for ( int i = 0 ; i <= vertices ; i ++ ) { edge [ i ] = new List < int > ( ) ; } } public void dfs ( int node , List < int > [ ] adj , int [ ] dp , Boolean [ ] visited ) { visited [ node ] = true ; for ( int i = 0 ; i < adj [ node ] . Count ; i ++ ) { if ( ! visited [ adj [ node ] [ i ] ] ) dfs ( adj [ node ] [ i ] , adj , dp , visited ) ; dp [ node ] = Math . Max ( dp [ node ] , 1 + dp [ adj [ node ] [ i ] ] ) ; } } public void addEdge ( int a , int b ) { edge [ a ] . Add ( b ) ; } public int findLongestPath ( int n ) { List < int > [ ] adj = edge ; int [ ] dp = new int [ n + 1 ] ; Boolean [ ] visited = new Boolean [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { if ( ! visited [ i ] ) dfs ( i , adj , dp , visited ) ; } int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { ans = Math . Max ( ans , dp [ i ] ) ; } return ans ; } } class GFG { public static void Main ( String [ ] args ) { int n = 5 ; Graph graph = new Graph ( n ) ; graph . addEdge ( 1 , 2 ) ; graph . addEdge ( 1 , 3 ) ; graph . addEdge ( 3 , 2 ) ; graph . addEdge ( 2 , 4 ) ; graph . addEdge ( 3 , 4 ) ; graph . findLongestPath ( n ) ; Console . WriteLine ( graph . findLongestPath ( n ) ) ; } }
Maximum length subsequence such that adjacent elements in the subsequence have a common factor | C # implementation of the above approach ; to compute least prime divisor of i ; Function that returns the maximum length subsequence such that adjacent elements have a common factor . ; Initialize dp array with 1. ; p has appeared at least once . ; Update latest occurrence of prime p . ; Take maximum value as the answer . ; Driver code
using System ; using System . Collections ; class GFG { static int N = 100005 ; static int MAX = 10000002 ; static int [ ] lpd = new int [ MAX ] ; static void preCompute ( ) { lpd [ 0 ] = lpd [ 1 ] = 1 ; for ( int i = 2 ; i * i < MAX ; i ++ ) { for ( int j = i * 2 ; j < MAX ; j += i ) { if ( lpd [ j ] == 0 ) { lpd [ j ] = i ; } } } for ( int i = 2 ; i < MAX ; i ++ ) { if ( lpd [ i ] == 0 ) { lpd [ i ] = i ; } } } static int maxLengthSubsequence ( int [ ] arr , int n ) { int [ ] dp = new int [ N ] ; Hashtable pos = new Hashtable ( ) ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] = 1 ; for ( int i = 0 ; i <= n ; i ++ ) { while ( arr [ i ] > 1 ) { int p = lpd [ arr [ i ] ] ; if ( pos . ContainsKey ( p ) ) { dp [ i ] = Math . Max ( dp [ i ] , 1 + dp [ Convert . ToInt32 ( pos [ p ] ) ] ) ; } pos [ p ] = i ; while ( arr [ i ] % p == 0 ) arr [ i ] /= p ; } } int ans = 1 ; for ( int i = 0 ; i <= n ; i ++ ) { ans = Math . Max ( ans , dp [ i ] ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 13 , 2 , 8 , 6 , 3 , 1 , 9 } ; int n = arr . Length - 1 ; preCompute ( ) ; Console . WriteLine ( maxLengthSubsequence ( arr , n ) ) ; } }
Number of ways to partition a string into two balanced subsequences | C # implementation of the above approach ; For maximum length of input string ; Declaring the DP table ; Function to calculate the number of valid assignments ; Return 1 if both subsequences are balanced ; Increment the count if it an opening bracket ; Decrement the count if it a closing bracket ; Driver Code ; Initializing the DP table ; Initial value for c_x and c_y is zero
using System ; class GFG { static int MAX = 10 ; static int [ , , ] F = new int [ MAX , MAX , MAX ] ; static int noOfAssignments ( String s , int n , int i , int c_x , int c_y ) { if ( F [ i , c_x , c_y ] != - 1 ) return F [ i , c_x , c_y ] ; if ( i == n ) { F [ i , c_x , c_y ] = ( c_x == 0 && c_y == 0 ) ? 1 : 0 ; return F [ i , c_x , c_y ] ; } if ( s [ i ] == ' ( ' ) { F [ i , c_x , c_y ] = noOfAssignments ( s , n , i + 1 , c_x + 1 , c_y ) + noOfAssignments ( s , n , i + 1 , c_x , c_y + 1 ) ; return F [ i , c_x , c_y ] ; } F [ i , c_x , c_y ] = 0 ; if ( c_x != 0 ) F [ i , c_x , c_y ] += noOfAssignments ( s , n , i + 1 , c_x - 1 , c_y ) ; if ( c_y != 0 ) F [ i , c_x , c_y ] += noOfAssignments ( s , n , i + 1 , c_x , c_y - 1 ) ; return F [ i , c_x , c_y ] ; } public static void Main ( String [ ] args ) { String s = " ( ( ) ) " ; int n = s . Length ; for ( int i = 0 ; i < MAX ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) for ( int k = 0 ; k < MAX ; k ++ ) F [ i , j , k ] = - 1 ; Console . WriteLine ( noOfAssignments ( s , n , 0 , 0 , 0 ) ) ; } }
Gould 's Sequence | C # program to generate Gould 's Sequence ; Utility function to count odd numbers in ith row of Pascals 's triangle ; Initialize count as zero ; Return 2 ^ count ; Function to generate gould 's Sequence ; loop to generate gould 's Sequence up to n ; Driver code ; Get n ; Function call
using System ; class GFG { static int countOddNumber ( int row_num ) { int count = 0 ; while ( row_num > 0 ) { count += row_num & 1 ; row_num >>= 1 ; } return ( 1 << count ) ; } static void gouldSequence ( int n ) { for ( int row_num = 0 ; row_num < n ; row_num ++ ) { Console . Write ( countOddNumber ( row_num ) + " ▁ " ) ; } } public static void Main ( ) { int n = 16 ; gouldSequence ( n ) ; } }
Count numbers ( smaller than or equal to N ) with given digit sum | C # program to count 2 s from 0 to n ; N can be max 10 ^ 18 and hence digitsum will be 162 maximum . ; If sum_so_far equals to given sum then return 1 else 0 ; Our constructed number should not become greater than N . ; If tight is true then it will also be true for ( i + 1 ) digit . ; Driver code
using System ; class GFG { static long [ , , ] dp = new long [ 18 , 2 , 162 ] ; static long solve ( int i , bool tight , int sum_so_far , int Sum , String number , int len ) { if ( i == len ) { if ( sum_so_far == Sum ) return 1 ; else return 0 ; } long ans = dp [ i , 1 , sum_so_far ] ; if ( ans != - 1 ) { return ans ; } ans = 0 ; bool ntight ; int nsum_so_far ; for ( char currdigit = '0' ; currdigit <= '9' ; currdigit ++ ) { if ( ! tight && currdigit > number [ i ] ) { break ; } ntight = tight || currdigit < number [ i ] ; nsum_so_far = sum_so_far + ( currdigit - '0' ) ; ans += solve ( i + 1 , ntight , nsum_so_far , Sum , number , len ) ; } return ans ; } public static void Main ( String [ ] args ) { int sum = 4 ; String number = "100" ; for ( int i = 0 ; i < 18 ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { for ( int k = 0 ; k < 162 ; k ++ ) dp [ i , j , k ] = - 1 ; } } Console . WriteLine ( solve ( 0 , false , 0 , sum , number , number . Length ) ) ; } }
Longest dividing subsequence | Dynamic Programming C # implementation of lds problem ; lds ( ) returns the length of the longest dividing subsequence in arr [ ] of size n ; Compute optimized lds values in bottom up manner ; Return maximum value in lds [ ] ; Driver program to test above function
using System ; using System . Linq ; public class GFG { static int lds ( int [ ] arr , int n ) { int [ ] lds = new int [ n ] ; lds [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { lds [ i ] = 1 ; for ( int j = 0 ; j < i ; j ++ ) if ( lds [ j ] != 0 && arr [ i ] % arr [ j ] == 0 ) lds [ i ] = Math . Max ( lds [ i ] , lds [ j ] + 1 ) ; } int max = lds . Max ( ) ; return max ; } public static void Main ( ) { int [ ] arr = { 2 , 11 , 16 , 12 , 36 , 60 , 71 , 17 , 29 , 144 , 288 , 129 , 432 , 993 } ; int n = arr . Length ; Console . Write ( " Length ▁ of ▁ lds ▁ is ▁ " + lds ( arr , n ) ) ; } }