codeparrot/xlcost-text-to-code huggingface dataset:
Program to convert Centimeters to Pixels | C # program to convert centimeter to pixels ; Function to convert centimeter to pixels ; Driver Code
using System ; class GFG { static double Conversion ( double centi ) { double pixels = ( 96 * centi ) / 2.54 ; Console . WriteLine ( pixels ) ; return 0 ; } public static void Main ( ) { double centi = 15 ; Conversion ( centi ) ; } }
Kth array element after M replacements of array elements by XOR of adjacent pairs | C # program for the above approach ; Method that returns the corresponding output by taking the given inputs . ; If this condition is satisfied , value of M is invalid ; Check if index K is valid ; Loop to perform M operations ; Creating a temporary list ; Traversing the array ; Calculate XOR values of adjacent elements ; Adding this value to the temporary list ; Update the original array ; Getting value at index K ; Driver Code ; Number of elements ; Given array [ ] arr ; Function call
using System ; using System . Collections . Generic ; class GFG { static int xor_operations ( int N , int [ ] arr , int M , int K ) { if ( M < 0 M >= N ) return - 1 ; if ( K < 0 K >= N - M ) return - 1 ; for ( int p = 0 ; p < M ; p ++ ) { List < int > temp = new List < int > ( ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { int value = arr [ i ] ^ arr [ i + 1 ] ; temp . Add ( value ) ; arr [ i ] = temp [ i ] ; } } int ans = arr [ K ] ; return ans ; } public static void Main ( String [ ] args ) { int N = 5 ; int [ ] arr = { 1 , 4 , 5 , 6 , 7 } ; int M = 1 , K = 2 ; Console . Write ( xor_operations ( N , arr , M , K ) ) ; } }
Check if N can be divided into K consecutive elements with a sum equal to N | C # program for the above approach ; Function to find the K consecutive elements with a sum equal to N ; Iterate over [ 2 , INF ] ; Store the sum ; If the sum exceeds N then break the loop ; Common difference should be divisible by number of terms ; Print value of i & return ; Print " - 1" if not possible to break N ; Driver Code ; Given N ; Function call
using System ; class GFG { public static void canBreakN ( long n ) { for ( long i = 2 ; ; i ++ ) { long m = i * ( i + 1 ) / 2 ; if ( m > n ) break ; long k = n - m ; if ( k % i != 0 ) continue ; Console . Write ( i ) ; return ; } Console . Write ( " - 1" ) ; } public static void Main ( string [ ] args ) { long N = 12 ; canBreakN ( N ) ; } }
Coprime divisors of a number | C # program to find two coprime divisors of a given number such that both are greater than 1 ; Function which finds the required pair of divisors of N ; We iterate upto sqrt ( N ) as we can find all the divisors of N in this time ; If x is a divisor of N keep dividing as long as possible ; We have found a required pair ; No such pair of divisors of N was found , hence print - 1 ; Driver code ; Sample example 1 ; Sample example 2
using System ; class GFG { public static void findCoprimePair ( int N ) { for ( int x = 2 ; x <= Math . Sqrt ( N ) ; x ++ ) { if ( N % x == 0 ) { while ( N % x == 0 ) { N /= x ; } if ( N > 1 ) { Console . WriteLine ( x + " ▁ " + N ) ; return ; } } } Console . WriteLine ( - 1 ) ; } public static void Main ( String [ ] args ) { int N = 45 ; findCoprimePair ( N ) ; N = 25 ; findCoprimePair ( N ) ; } }
Wasteful Numbers | C # program for the above approach ; Array to store all prime less than and equal to MAX . ; Function for Sieve of Sundaram ; Boolean Array ; Mark all numbers which do not generate prime number by 2 * i + 1 ; Since 2 is a prime number ; Print remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; Function that returns true if n is a Wasteful number ; Count digits in original number ; Count all digits in prime factors of N pDigit is going to hold this value . ; Count powers of p in n ; If primes [ i ] is a prime factor , ; Count the power of prime factors ; Add its digits to pDigit ; Add digits of power of prime factors to pDigit . ; If n != 1 then one prime factor still to be summed up ; If digits in prime factors is more than digits in original number then return true . Else return false . ; Function to print Wasteful Number before N ; Iterate till N and check if i is wastefull or not ; Driver code ; Precompute prime numbers upto 10 ^ 6 ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int MAX = 10000 ; static List < int > primes = new List < int > ( ) ; static void sieveSundaram ( ) { bool [ ] marked = new bool [ MAX / 2 + 1 ] ; for ( int i = 1 ; i <= ( Math . Sqrt ( MAX ) - 1 ) / 2 ; i ++ ) { for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) { marked [ j ] = true ; } } primes . Add ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked [ i ] == false ) primes . Add ( 2 * i + 1 ) ; } static bool isWasteful ( int n ) { if ( n == 1 ) return false ; int original_no = n ; int sumDigits = 0 ; while ( original_no > 0 ) { sumDigits ++ ; original_no = original_no / 10 ; } int pDigit = 0 , count_exp = 0 , p = 0 ; for ( int i = 0 ; primes [ i ] <= n / 2 ; i ++ ) { while ( n % primes [ i ] == 0 ) { p = primes [ i ] ; n = n / p ; count_exp ++ ; } while ( p > 0 ) { pDigit ++ ; p = p / 10 ; } while ( count_exp > 1 ) { pDigit ++ ; count_exp = count_exp / 10 ; } } if ( n != 1 ) { while ( n > 0 ) { pDigit ++ ; n = n / 10 ; } } return ( pDigit > sumDigits ) ; } static void Solve ( int N ) { for ( int i = 1 ; i < N ; i ++ ) { if ( isWasteful ( i ) ) { Console . Write ( i + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { sieveSundaram ( ) ; int N = 10 ; Solve ( N ) ; } }
Hexanacci Numbers | C # simple recursive program to print Nth Hexanacci numbers . ; Function to print the Nth Hexanacci number ; Driver code
using System ; class GFG { static int printhexaRec ( int n ) { if ( n == 0 n == 1 n == 2 n == 3 n == 4 n == 5 ) return 0 ; else if ( n == 6 ) return 1 ; else return ( printhexaRec ( n - 1 ) + printhexaRec ( n - 2 ) + printhexaRec ( n - 3 ) + printhexaRec ( n - 4 ) + printhexaRec ( n - 5 ) + printhexaRec ( n - 6 ) ) ; } static void printhexa ( int n ) { Console . Write ( printhexaRec ( n ) + " STRNEWLINE " ) ; } public static void Main ( ) { int n = 11 ; printhexa ( n ) ; } }
Hexanacci Numbers | C # implementation to print Nth term of hexanacci numbers . ; Function to print the Nth term of the Hexanacci number ; Initialize first five numbers to base cases ; Declare a current variable ; Loop to add previous five numbers for each number starting from 5 and then assign first , second , third , fourth fifth to second , third , fourth , fifth and curr to sixth respectively ; Driver code
using System ; class GFG { static void printhexa ( int n ) { if ( n < 0 ) return ; int first = 0 ; int second = 0 ; int third = 0 ; int fourth = 0 ; int fifth = 0 ; int sixth = 1 ; int curr = 0 ; if ( n < 6 ) Console . WriteLine ( first ) ; else if ( n == 6 ) Console . WriteLine ( sixth ) ; else { for ( int i = 6 ; i < n ; i ++ ) { curr = first + second + third + fourth + fifth + sixth ; first = second ; second = third ; third = fourth ; fourth = fifth ; fifth = sixth ; sixth = curr ; } } Console . WriteLine ( curr ) ; } public static void Main ( String [ ] args ) { int n = 11 ; printhexa ( n ) ; } }
Find the smallest number whose sum of digits is N | C # program to find the smallest number whose sum of digits is also N ; Function to find the smallest number whose sum of digits is also N ; Driver code
using System ; class GFG { static void smallestNumber ( int N ) { Console . WriteLine ( ( N % 9 + 1 ) * Math . Pow ( 10 , ( N / 9 ) ) - 1 ) ; } public static void Main ( ) { int N = 10 ; smallestNumber ( N ) ; } }
Compositorial of a number | C # program to find compositorial of composite numbers ; Function to check if a number is composite . ; Corner cases ; This is checked so that we can skip the middle five numbers in the below loop ; This function stores all composite numbers less than N ; Function to calculate the compositorial of n ; Multiply first n composite number ; Driver code ; List to store all the composite less than N
using System ; using System . Collections . Generic ; class GFG { static List < int > compo = new List < int > ( ) ; static bool isComposite ( int n ) { if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; int i = 5 ; while ( i * i <= n ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; i = i + 6 ; } return false ; } static void Compositorial_list ( int n ) { int l = 0 ; for ( int i = 4 ; i < 1000000 ; i ++ ) { if ( l < n ) { if ( isComposite ( i ) ) { compo . Add ( i ) ; l += 1 ; } } } } static int calculateCompositorial ( int n ) { int result = 1 ; for ( int i = 0 ; i < n ; i ++ ) result = result * compo [ i ] ; return result ; } public static void Main ( String [ ] args ) { int n = 5 ; Compositorial_list ( n ) ; Console . Write ( ( calculateCompositorial ( n ) ) ) ; } }
Distinct powers of a number N such that the sum is equal to K | C # implementation to find out the powers of N that add upto K ; Initializing the PowerArray with all 0 's ; Function to find the powers of N that add up to K ; Initializing the counter ; Executing the while loop until K is greater than 0 ; If K % N == 1 , then the power array is incremented by 1 ; Checking if any power is occurred more than once ; For any other value , the sum of powers cannot be added up to K ; Printing the powers of N that sum up to K ; Driver code
using System ; class GFG { static int [ ] b = new int [ 50 ] ; static int PowerArray ( int n , int k ) { int count = 0 ; while ( k > 0 ) { if ( k % n == 0 ) { k /= n ; count ++ ; } else if ( k % n == 1 ) { k -= 1 ; b [ count ] ++ ; if ( b [ count ] > 1 ) { Console . Write ( - 1 ) ; return 0 ; } } else { Console . Write ( - 1 ) ; return 0 ; } } for ( int i = 0 ; i < 50 ; i ++ ) { if ( b [ i ] != 0 ) { Console . Write ( i + " , ▁ " ) ; } } return int . MinValue ; } public static void Main ( String [ ] args ) { int N = 3 ; int K = 40 ; PowerArray ( N , K ) ; } }
Program to find value of 1 ^ k + 2 ^ k + 3 ^ k + ... + n ^ k | C # Program to find the value 1 ^ K + 2 ^ K + 3 ^ K + . . + N ^ K ; Function to find value of 1 ^ K + 2 ^ K + 3 ^ K + . . + N ^ K ; Initialise sum to 0 ; Find the value of pow ( i , 4 ) and then add it to the sum ; Return the sum ; Drivers Code ; Function call to find the sum
using System ; public class GFG { static int findSum ( int N , int k ) { int sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { sum += ( int ) Math . Pow ( i , k ) ; } return sum ; } public static void Main ( string [ ] args ) { int N = 8 , k = 4 ; Console . WriteLine ( findSum ( N , k ) ) ; } }
Count of indices in an array that satisfy the given condition | C # implementation of the approach ; Function to return the count of indices that satisfy the given condition ; To store the result ; To store the current maximum Initialized to 0 since there are only positive elements in the array ; i is a valid index ; Update the maximum so far ; Increment the counter ; Driver code
using System ; class GFG { static int countIndices ( int [ ] arr , int n ) { int cnt = 0 ; int max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( max < arr [ i ] ) { max = arr [ i ] ; cnt ++ ; } } return cnt ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; Console . WriteLine ( countIndices ( arr , n ) ) ; } }
Maximum frequency of a remainder modulo 2 i | C # implementation of the approach ; Binary representation of the digits ; Function to return the maximum frequency of s modulo with a power of 2 ; Store the binary representation ; Convert the octal to binary ; Remove the LSB ; If there is 1 in the binary representation ; Find the number of zeroes in between two 1 's in the binary representation ; Driver code
using System ; class GFG { static String [ ] bin = { "000" , "001" , "010" , "011" , "100" , "101" , "110" , "111" } ; static int maxFreq ( String s ) { String binary = " " ; for ( int K = 0 ; K < s . Length ; K ++ ) { binary += bin [ s [ K ] - '0' ] ; } binary = binary . Substring ( 0 , binary . Length - 1 ) ; int count = 1 , prev = - 1 , i , j = 0 ; for ( i = binary . Length - 1 ; i >= 0 ; i -- , j ++ ) if ( binary [ i ] == '1' ) { count = Math . Max ( count , j - prev ) ; prev = j ; } return count ; } public static void Main ( String [ ] args ) { String octal = "13" ; Console . WriteLine ( maxFreq ( octal ) ) ; } }
Find all the prime numbers of given number of digits | C # implementation of the approach ; Function for Sieve of Eratosthenes ; Function to print all the prime numbers with d digits ; Range to check integers ; For every integer in the range ; If the current integer is prime ; Driver code ; Generate primes
using System ; class GFG { static int sz = 100000 ; static bool [ ] isPrime = new bool [ sz + 1 ] ; static void sieve ( ) { for ( int i = 0 ; i <= sz ; i ++ ) isPrime [ i ] = true ; isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i * i <= sz ; i ++ ) { if ( isPrime [ i ] ) { for ( int j = i * i ; j < sz ; j += i ) { isPrime [ j ] = false ; } } } } static void findPrimesD ( int d ) { int left = ( int ) Math . Pow ( 10 , d - 1 ) ; int right = ( int ) Math . Pow ( 10 , d ) - 1 ; for ( int i = left ; i <= right ; i ++ ) { if ( isPrime [ i ] ) { Console . Write ( i + " ▁ " ) ; } } } static public void Main ( ) { sieve ( ) ; int d = 1 ; findPrimesD ( d ) ; } }
Find the number of cells in the table contains X | C # program to find number of cells in the table contains X ; Function to find number of cells in the table contains X ; Driver code ; Function call
using System ; class GFG { public static int Cells ( int n , int x ) { if ( n <= 0 x <= 0 x > n * n ) return 0 ; int i = 0 , count = 0 ; while ( ++ i * i < x ) if ( x % i == 0 && x <= n * i ) count += 2 ; return i * i == x ? count + 1 : count ; } static public void Main ( ) { int n = 6 , x = 12 ; Console . WriteLine ( Cells ( n , x ) ) ; } }
Find the maximum possible value of the minimum value of modified array | C # program to find the maximum possible value of the minimum value of modified array ; Function to find the maximum possible value of the minimum value of the modified array ; To store minimum value of array ; To store sum of elements of array ; Solution is not possible ; zero is the possible value ; minimum possible value ; maximum possible value ; to store a required answer ; Binary Search ; If mid is possible then try to increase required answer ; If mid is not possible then decrease required answer ; Return required answer ; Driver Code
using System ; class GFG { static int maxOfMin ( int [ ] a , int n , int S ) { int mi = int . MaxValue ; int s1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { s1 += a [ i ] ; mi = Math . Min ( a [ i ] , mi ) ; } if ( s1 < S ) return - 1 ; if ( s1 == S ) return 0 ; int low = 0 ; int high = mi ; int ans = 0 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( s1 - ( mid * n ) >= S ) { ans = mid ; low = mid + 1 ; } else high = mid - 1 ; } return ans ; } public static void Main ( ) { int [ ] a = { 10 , 10 , 10 , 10 , 10 } ; int S = 10 ; int n = a . Length ; Console . WriteLine ( maxOfMin ( a , n , S ) ) ; } }
Program to print ' N ' alphabet using the number pattern from 1 to n | C # implementation of the approach ; Function to print the desired Alphabet N Pattern ; Declaring the values of Right , Left and Diagonal values ; Main Loop for the rows ; For the left Values ; Spaces for the diagonals ; Condition for the diagonals ; Spaces for the Right Values ; For the right values ; Driver Code ; Size of the Pattern ; Calling the function to print the desired Pattern
using System ; class GFG { public static void Alphabet_N_Pattern ( int N ) { int index , side_index ; int Right = 1 , Left = 1 , Diagonal = 2 ; for ( index = 0 ; index < N ; index ++ ) { Console . Write ( Left ++ ) ; for ( side_index = 0 ; side_index < 2 * ( index ) ; side_index ++ ) Console . Write ( " ▁ " ) ; if ( index != 0 && index != N - 1 ) Console . Write ( Diagonal ++ ) ; else Console . Write ( " ▁ " ) ; for ( side_index = 0 ; side_index < 2 * ( N - index - 1 ) ; side_index ++ ) Console . Write ( " ▁ " ) ; Console . Write ( Right ++ ) ; Console . Write ( " STRNEWLINE " ) ; } } static void Main ( ) { int Size = 6 ; Alphabet_N_Pattern ( Size ) ; } }
Check if the sum of digits of a number N divides it | C # program to check if sum of digits of a number divides it ; Function to check if sum of digits of a number divides it ; Calculate sum of all of digits of N ; Driver Code
using System ; class GFG { public int isSumDivides ( int N ) { int temp = N , sum = 0 ; while ( temp > 0 ) { sum += temp % 10 ; temp /= 10 ; } if ( N % sum == 0 ) return 1 ; else return 0 ; } public static void Main ( ) { GFG g = new GFG ( ) ; int N = 12 ; if ( g . isSumDivides ( N ) > 0 ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Sum of numbers from 1 to N which are divisible by 3 or 4 | C # program to find sum of numbers from 1 to N which are divisible by 3 or 4 ; Function to calculate the sum of numbers divisible by 3 or 4 ; Driver code
using System ; class GFG { static int sum ( int N ) { int S1 , S2 , S3 ; S1 = ( ( N / 3 ) ) * ( 2 * 3 + ( N / 3 - 1 ) * 3 ) / 2 ; S2 = ( ( N / 4 ) ) * ( 2 * 4 + ( N / 4 - 1 ) * 4 ) / 2 ; S3 = ( ( N / 12 ) ) * ( 2 * 12 + ( N / 12 - 1 ) * 12 ) / 2 ; return S1 + S2 - S3 ; } public static void Main ( ) { int N = 20 ; Console . WriteLine ( sum ( 12 ) ) ; } }
Next greater number than N with exactly one bit different in binary representation of N | C # program to find next greater number than N with exactly one bit different in binary representation of N ; Function to find next greater number than N with exactly one bit different in binary representation of N ; It is guaranteed that there is a bit zero in the number ; If the shifted bit is zero then break ; increase the bit shift ; increase the power of 2 ; set the lowest bit of the number ; Driver code ; display the next number
using System ; class GFG { static int nextGreater ( int N ) { int power_of_2 = 1 , shift_count = 0 ; while ( true ) { if ( ( ( N >> shift_count ) & 1 ) % 2 == 0 ) break ; shift_count ++ ; power_of_2 = power_of_2 * 2 ; } return ( N + power_of_2 ) ; } public static void Main ( ) { int N = 11 ; Console . WriteLine ( " The ▁ next ▁ number ▁ is ▁ = ▁ " + nextGreater ( N ) ) ; } }
Count number of ways to cover a distance | Set 2 | C # implementation of the approach ; Function to return the count of the total number of ways to cover the distance with 1 , 2 and 3 steps ; Base conditions ; To store the last three stages ; Find the numbers of steps required to reach the distance i ; Return the required answer ; Driver code
using System ; class GFG { static int countWays ( int n ) { if ( n == 0 ) return 1 ; if ( n <= 2 ) return n ; int f0 = 1 , f1 = 1 , f2 = 2 ; int ans = 0 ; for ( int i = 3 ; i <= n ; i ++ ) { ans = f0 + f1 + f2 ; f0 = f1 ; f1 = f2 ; f2 = ans ; } return ans ; } public static void Main ( String [ ] args ) { int n = 4 ; Console . WriteLine ( countWays ( n ) ) ; } }
Maximize sum by choosing elements from different section of a matrix | C # program for the above approach ; Function to find the maximum value ; Dp table ; Fill the dp in bottom up manner ; Maximum of the three sections ; Maximum of the first section ; Maximum of the second section ; Maximum of the third section ; If we choose element from section 1 , we cannot have selection from same section in adjacent rows ; Print the maximum sum ; Driver code
using System ; class GFG { static int n = 6 , m = 6 ; static void maxSum ( long [ , ] arr ) { long [ , ] dp = new long [ n + 1 , 3 ] ; for ( int i = 0 ; i < n ; i ++ ) { long m1 = 0 , m2 = 0 , m3 = 0 ; for ( int j = 0 ; j < m ; j ++ ) { if ( ( j / ( m / 3 ) ) == 0 ) { m1 = Math . Max ( m1 , arr [ i , j ] ) ; } else if ( ( j / ( m / 3 ) ) == 1 ) { m2 = Math . Max ( m2 , arr [ i , j ] ) ; } else if ( ( j / ( m / 3 ) ) == 2 ) { m3 = Math . Max ( m3 , arr [ i , j ] ) ; } } dp [ i + 1 , 0 ] = Math . Max ( dp [ i , 1 ] , dp [ i , 2 ] ) + m1 ; dp [ i + 1 , 1 ] = Math . Max ( dp [ i , 0 ] , dp [ i , 2 ] ) + m2 ; dp [ i + 1 , 2 ] = Math . Max ( dp [ i , 1 ] , dp [ i , 0 ] ) + m3 ; } Console . Write ( Math . Max ( Math . Max ( dp [ n , 0 ] , dp [ n , 1 ] ) , dp [ n , 2 ] ) + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { long [ , ] arr = { { 1 , 3 , 5 , 2 , 4 , 6 } , { 6 , 4 , 5 , 1 , 3 , 2 } , { 1 , 3 , 5 , 2 , 4 , 6 } , { 6 , 4 , 5 , 1 , 3 , 2 } , { 6 , 4 , 5 , 1 , 3 , 2 } , { 1 , 3 , 5 , 2 , 4 , 6 } } ; maxSum ( arr ) ; } }
Total number of odd length palindrome sub | C # implementation of above approach ; Function to find the total palindromic odd length sub - sequences ; dp array to store the number of palindromic subsequences for 0 to i - 1 and j + 1 to n - 1 ; We will start with the largest distance between i and j ; For each len , we fix our i ; For this i we will find our j ; Base cases ; If the characters are equal then look for out of bound index ; We have only 1 way that is to just pick these characters ; If the characters are not equal ; Subtract it as we have counted it twice ; We have just 1 palindrome sequence of length 1 ; Else total ways would be sum of dp [ i - 1 ] [ i + 1 ] , that is number of palindrome sub - sequences from 1 to i - 1 + number of palindrome sub - sequences from i + 1 to n - 1 ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void solve ( char [ ] s ) { int n = s . Length ; int [ , ] dp = new int [ n , n ] ; for ( int len = n - 1 ; len >= 0 ; -- len ) { for ( int i = 0 ; i + len < n ; ++ i ) { int j = i + len ; if ( i == 0 && j == n - 1 ) { if ( s [ i ] == s [ j ] ) dp [ i , j ] = 2 ; else if ( s [ i ] != s [ j ] ) dp [ i , j ] = 1 ; } else { if ( s [ i ] == s [ j ] ) { if ( i - 1 >= 0 ) { dp [ i , j ] += dp [ i - 1 , j ] ; } if ( j + 1 <= n - 1 ) { dp [ i , j ] += dp [ i , j + 1 ] ; } if ( i - 1 < 0 j + 1 >= n ) { dp [ i , j ] += 1 ; } } else if ( s [ i ] != s [ j ] ) { if ( i - 1 >= 0 ) { dp [ i , j ] += dp [ i - 1 , j ] ; } if ( j + 1 <= n - 1 ) { dp [ i , j ] += dp [ i , j + 1 ] ; } if ( i - 1 >= 0 && j + 1 <= n - 1 ) { dp [ i , j ] -= dp [ i - 1 , j + 1 ] ; } } } } } List < int > ways = new List < int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( i == 0 i == n - 1 ) { ways . Add ( 1 ) ; } else { int total = dp [ i - 1 , i + 1 ] ; ways . Add ( total ) ; } } for ( int i = 0 ; i < ways . Capacity ; ++ i ) { Console . Write ( ways [ i ] + " ▁ " ) ; } } public static void Main ( ) { char [ ] s = " xyxyx " . ToCharArray ( ) ; solve ( s ) ; } }
Find the number of Chicks in a Zoo at Nth day | C # implementation of the approach ; Function to return the number of chicks on the nth day ; Size of dp [ ] has to be at least 6 ( 1 - based indexing ) ; Every day current population will be three times of the previous day ; Manually calculated value ; From 8 th day onwards ; Chick population decreases by 2 / 3 everyday . For 8 th day on [ i - 6 ] i . e 2 nd day population was 3 and so 2 new born die on the 6 th day and so on for the upcoming days ; Driver code
using System ; class GFG { static long getChicks ( int n ) { int size = Math . Max ( n , 7 ) ; long [ ] dp = new long [ size ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i < 6 ; i ++ ) { dp [ i ] = dp [ i - 1 ] * 3 ; } dp [ 6 ] = 726 ; for ( int i = 8 ; i <= n ; i ++ ) { dp [ i ] = ( dp [ i - 1 ] - ( 2 * dp [ i - 6 ] / 3 ) ) * 3 ; } return dp [ n ] ; } static public void Main ( ) { int n = 3 ; Console . WriteLine ( getChicks ( n ) ) ; } }
Find the number of Chicks in a Zoo at Nth day | C # implementation of the approach ; Function to return the number of chicks on the nth day ; Driver code
using System ; class GFG { static int getChicks ( int n ) { int chicks = ( int ) Math . Pow ( 3 , n - 1 ) ; return chicks ; } public static void Main ( ) { int n = 3 ; Console . WriteLine ( getChicks ( n ) ) ; } }