codeparrot/xlcost-text-to-code huggingface dataset:
Total number of possible Binary Search Trees and Binary Trees with n keys | See https : www . geeksforgeeks . org / program - nth - catalan - number / for reference of below code . ; A function to find factorial of a given number ; Calculate value of [ 1 * ( 2 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; A function to count number of BST with n nodes using catalan ; find nth catalan number ; return nth catalan number ; A function to count number of binary trees with n nodes ; find count of BST with n numbers ; return count * n ! ; Driver Code ; find count of BST and binary trees with n nodes ; print count of BST and binary trees with n nodes
using System ; class GFG { static int factorial ( int n ) { int res = 1 ; for ( int i = 1 ; i <= n ; ++ i ) { res *= i ; } return res ; } static int binomialCoeff ( int n , int k ) { int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static int catalan ( int n ) { int c = binomialCoeff ( 2 * n , n ) ; return c / ( n + 1 ) ; } static int countBST ( int n ) { int count = catalan ( n ) ; return count ; } static int countBT ( int n ) { int count = catalan ( n ) ; return count * factorial ( n ) ; } static public void Main ( ) { int count1 , count2 , n = 5 ; count1 = countBST ( n ) ; count2 = countBT ( n ) ; Console . WriteLine ( " Count ▁ of ▁ BST ▁ with ▁ " + n + " ▁ nodes ▁ is ▁ " + count1 ) ; Console . WriteLine ( " Count ▁ of ▁ binary ▁ " + " trees ▁ with ▁ " + n + " ▁ nodes ▁ is ▁ " + count2 ) ; } }
Find all numbers in range [ 1 , N ] that are not present in given Array | C # program for above approach ; Function to find the missing numbers ; traverse the array arr [ ] ; Update ; Traverse the array arr [ ] ; If Num is not present ; Driver Code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void getMissingNumbers ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ Math . Abs ( arr [ i ] ) - 1 ] = - ( Math . Abs ( arr [ Math . Abs ( arr [ i ] ) - 1 ] ) ) ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) Console . Write ( i + 1 + " ▁ " ) ; } } public static void Main ( ) { int N = 5 ; int [ ] arr = { 5 , 5 , 4 , 4 , 2 } ; getMissingNumbers ( arr , N ) ; } }
Check if elements of a Binary Matrix can be made alternating | C # program for the above approach ; Function to check for the grid in one of the alternating ways ; Iterate over the range ; Iterate over the range ; ( i + j ) % 2 == 1 cells should be with the character check and the rest should be with the other character ; Function to fill the grid in a possible way ; Function to find if the grid can be made alternating ; If grid contains atleast one 1 or 0 ; Fill the grid in any possible way ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static bool PosCheck ( char [ , ] a , int n , int m , char check ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( a [ i , j ] == ' * ' ) { continue ; } else { if ( ( ( ( i + j ) & 1 ) == 1 ) && a [ i , j ] != check ) { return false ; } if ( ! ( ( ( i + j ) & 1 ) == 1 ) && a [ i , j ] == check ) { return false ; } } } } return true ; } public static void fill ( char [ , ] a , int n , int m , char odd , char even ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( ( ( i + j ) & 1 ) == 1 ) { a [ i , j ] = odd ; } else { a [ i , j ] = even ; } } } } public static void findPossibleGrid ( int n , int m , char [ , ] a ) { bool flag = true ; int k = - 1 , o = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( a [ i , j ] != ' * ' ) { k = i ; o = j ; break ; } } if ( k != - 1 ) { break ; } } if ( k != - 1 ) { flag = PosCheck ( a , n , m , '1' ) ; if ( flag ) { fill ( a , n , m , '1' , '0' ) ; } else { flag = PosCheck ( a , n , m , '0' ) ; if ( flag ) { fill ( a , n , m , '0' , '1' ) ; } } } else { char h = '1' ; for ( int i = 0 ; i < m ; i ++ ) { a [ 0 , i ] = h ; if ( h == '1' ) { h = '0' ; } else { h = '1' ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( i - 1 < 0 ) { continue ; } if ( a [ i - 1 , j ] == '1' ) { a [ i , j ] = '0' ; } else { a [ i , j ] = '1' ; } } } flag = true ; } if ( ! flag ) { Console . WriteLine ( " No " ) ; } else { Console . WriteLine ( " Yes " ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { Console . Write ( a [ i , j ] ) ; } Console . WriteLine ( ) ; } } } public static void Main ( ) { int n = 4 , m = 4 ; char [ , ] grid = { { ' * ' , ' * ' , '1' , '0' } , { ' * ' , ' * ' , ' * ' , ' * ' } , { ' * ' , ' * ' , ' * ' , ' * ' } , { ' * ' , ' * ' , '0' , '1' } } ; findPossibleGrid ( n , m , grid ) ; } }
Find the Kth smallest odd length palindrome number | C # program for the above approach ; Function to find the Kth smallest palindrome of odd length ; Store the original number K ; Removing the last digit of K ; Generate the palindrome by appending the reverse of K except last digit to itself ; Find the remainder ; Add the digit to palin ; Divide K by 10 ; Return the resultant palindromic number formed ; Driver Code
using System ; class GFG { static int oddLengthPalindrome ( int k ) { int palin = k ; k = k / 10 ; while ( k > 0 ) { int rev = k % 10 ; palin = ( palin * 10 ) + rev ; k = k / 10 ; } return palin ; } static void Main ( string [ ] args ) { int k = 504 ; Console . WriteLine ( oddLengthPalindrome ( k ) ) ; } }
Maximize length of subsequence consisting of single distinct character possible by K increments in a string | C # program for the above approach ; Function to find the maximum length of a subsequence of same characters after at most K increment operations ; Store the size of s ; Sort the given string sort ( S . begin ( ) , S . end ( ) ) ; convert input string to char array ; sort tempArray ; Stores the maximum length and the sum of the sliding window ; Traverse the string S ; Add the current character to the window ; Decrease the window size ; Update the value of sum ; Increment the value of start ; Update the maximum window size ; Print the resultant maximum length of the subsequence ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void maxSubsequenceLen ( string s , int K ) { int N = s . Length ; int start = 0 , end = 0 ; char [ ] S = s . ToCharArray ( ) ; Array . Sort ( S ) ; int ans = Int32 . MinValue , sum = 0 ; for ( end = 0 ; end < N ; end ++ ) { sum = sum + ( S [ end ] - ' a ' ) ; while ( sum + K < ( S [ end ] - ' a ' ) * ( end - start + 1 ) ) { sum = sum - ( S [ start ] - ' a ' ) ; start ++ ; } ans = Math . Max ( ans , end - start + 1 ) ; } Console . WriteLine ( ans ) ; } public static void Main ( ) { string S = " acscbcca " ; int K = 1 ; maxSubsequenceLen ( S , K ) ; } }
Check if a given integer is the product of K consecutive integers | C # program for the above approach ; Function to check if N can be expressed as the product of K consecutive integers ; Stores the K - th root of N ; Stores the product of K consecutive integers ; Traverse over the range [ 1 , K ] ; Update the product ; If product is N , then return " Yes " ; Otherwise , traverse over the range [ 2 , Kthroot ] ; Update the value of product ; If product is equal to N ; Otherwise , return " No " ; Driver Code
using System ; class GFG { static string checkPro ( int n , int k ) { double exp = 1.0 / k ; int KthRoot = ( int ) Math . Pow ( n , exp ) ; int product = 1 ; for ( int i = 1 ; i < k + 1 ; i ++ ) { product = product * i ; } if ( product == n ) return " Yes " ; else { for ( int j = 2 ; j < KthRoot + 1 ; j ++ ) { product = product * ( j + k - 1 ) ; product = product / ( j - 1 ) ; if ( product == n ) return " Yes " ; } } return " No " ; } static public void Main ( ) { int N = 210 ; int K = 3 ; Console . WriteLine ( checkPro ( N , K ) ) ; } }
Convert a Binary String to another by flipping prefixes minimum number of times | C # program to implement the above approach ; Function to count minimum number of operations required to convert string a to another string b ; Store the lengths of each prefixes selected ; Traverse the string ; If first character is same as b [ i ] ; Insert 1 to ops [ ] ; And , flip the bit ; Reverse the prefix string of length i + 1 ; Flip the characters in this prefix length ; Push ( i + 1 ) to array ops [ ] ; Print the number of operations ; Print the length of each prefixes stored ; Function to reverse a [ ] from start to end ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void minOperations ( string s1 , string s2 , int n ) { char [ ] a = s1 . ToCharArray ( ) ; char [ ] b = s2 . ToCharArray ( ) ; List < int > ops = new List < int > ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( a [ i ] != b [ i ] ) { if ( a [ 0 ] == b [ i ] ) { ops . Add ( 1 ) ; a [ 0 ] = ( a [ 0 ] == '0' ? '1' : '0' ) ; } reverse ( a , 0 , i ) ; for ( int j = 0 ; j <= i ; j ++ ) { a [ j ] = ( a [ j ] == '0' ? '1' : '0' ) ; } ops . Add ( i + 1 ) ; } } Console . WriteLine ( ops . Count ) ; foreach ( int x in ops ) { Console . Write ( x + " ▁ " ) ; } } static void reverse ( char [ ] a , int start , int end ) { while ( start < end ) { char temp = a [ start ] ; a [ start ] = a [ end ] ; a [ end ] = temp ; start ++ ; end -- ; } } public static void Main ( ) { string a = "10" , b = "01" ; int N = a . Length ; minOperations ( a , b , N ) ; } }
Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups | C # program for the above approach ; Function to fill the matrix with the given conditions ; Count of parts with size sizeOfPart ; Assigning the cell with no of groups ; Update row ; Update col ; Increment count ; For new group increment start ; Function to return the reference of the matrix to be filled ; Create matrix of size N * M ; Starting index of the matrix ; Size of one group ; Element to assigned to matrix ; Fill the matrix that have rem no of parts with size size + 1 ; Fill the remaining number of parts with each part size is ' size ' ; Function to print the matrix ; Traverse the rows ; Traverse the columns ; Driver Code ; Given N , M , K ; Function Call ; Function Call to print matrix
using System ; class GFG { static int N , M ; static int [ , ] mat ; static int row , col , start , flag ; static void fillMatrix ( int sizeOfpart , int noOfPart , int m ) { for ( int i = 0 ; i < noOfPart ; ++ i ) { int count = 0 ; while ( count < sizeOfpart ) { mat [ row , col ] = start ; if ( col == m - 1 && flag == 1 ) { row ++ ; col = m ; flag = 0 ; } else if ( col == 0 && flag == 0 ) { row ++ ; col = - 1 ; flag = 1 ; } if ( flag == 1 ) { col ++ ; } else { col -- ; } count ++ ; } start ++ ; } } static void findMatrix ( int k ) { mat = new int [ M , N ] ; row = 0 ; col = 0 ; int size = ( N * M ) / k ; int rem = ( N * M ) % k ; start = 1 ; flag = 1 ; fillMatrix ( size + 1 , rem , M ) ; fillMatrix ( size , k - rem , M ) ; } static void printMatrix ( ) { for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < M ; ++ j ) { Console . Write ( mat [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { N = 5 ; M = 5 ; int K = 6 ; findMatrix ( K ) ; printMatrix ( ) ; } }
Replace ' ? ' in a string such that no two adjacent characters are same | C # program for the above approach ; Function that replace all ' ? ' with lowercase alphabets such that each adjacent character is different ; Store the given String ; If the first character is ' ? ' ; Traverse the String [ 1 , N - 1 ] ; If the current character is ' ? ' ; Change the character ; Check equality with the previous character ; Check equality with the next character ; Check equality with the previous character ; If the last character is ' ? ' ; Change character ; Check with previous character ; Return the resultant String ; Driver Code ; Given String S ; Function Call
using System ; class GFG { static string changeString ( string S ) { char [ ] s = S . ToCharArray ( ) ; int N = S . Length ; if ( s [ 0 ] == ' ? ' ) { s [ 0 ] = ' a ' ; if ( s [ 0 ] == s [ 1 ] ) { s [ 0 ] ++ ; } } for ( int i = 1 ; i < N - 1 ; i ++ ) { if ( s [ i ] == ' ? ' ) { s [ i ] = ' a ' ; if ( s [ i ] == s [ i - 1 ] ) { s [ i ] ++ ; } if ( s [ i ] == s [ i + 1 ] ) { s [ i ] ++ ; } if ( s [ i ] == s [ i - 1 ] ) { s [ i ] ++ ; } } } if ( s [ N - 1 ] == ' ? ' ) { s [ N - 1 ] = ' a ' ; if ( s [ N - 1 ] == s [ N - 2 ] ) { s [ N - 1 ] ++ ; } } string ans = " " ; for ( int i = 0 ; i < s . Length ; i ++ ) { ans += s [ i ] ; } return ans ; } public static void Main ( ) { string S = " ? a ? a " ; Console . WriteLine ( changeString ( S ) ) ; } }
Check if a given string is a Reverse Bitonic String or not | C # program to implement the above approach ; Function to check if the given string is reverse bitonic ; Check for decreasing sequence ; If end of string has been reached ; Check for increasing sequence ; If the end of string hasn 't been reached ; If the string is reverse bitonic ; Driver Code
using System ; class GFG { static int checkReverseBitonic ( String s ) { int i , j ; for ( i = 1 ; i < s . Length ; i ++ ) { if ( s [ i ] < s [ i - 1 ] ) continue ; if ( s [ i ] >= s [ i - 1 ] ) break ; } if ( i == s . Length - 1 ) return 1 ; for ( j = i + 1 ; j < s . Length ; j ++ ) { if ( s [ j ] > s [ j - 1 ] ) continue ; if ( s [ j ] <= s [ j - 1 ] ) break ; } i = j ; if ( i != s . Length ) return 0 ; return 1 ; } public static void Main ( String [ ] args ) { String s = " abcdwef " ; if ( checkReverseBitonic ( s ) == 1 ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Smallest number whose square has N digits | C # program for above approach ; Function to return smallest number whose square has N digits ; Calculate N - th term of the series ; Driver code
using System ; class GFG { static int smallestNum ( int N ) { float x = ( float ) ( Math . Pow ( 10 , ( N - 1 ) / 2.0 ) ) ; return ( int ) ( Math . Ceiling ( x ) ) ; } public static void Main ( ) { int N = 4 ; Console . Write ( smallestNum ( N ) ) ; } }
Count of K | C # code for the above program . ; Function to to count the number of K - countdowns for multiple queries ; Flag which stores the current value of value in the countdown ; Count of K - countdowns ; Loop to iterate over the elements of the array ; Condition check if the elements of the array is equal to K ; Condition check if the elements of the array is in continuous order ; Condition check if the elements of the array are not in continuous order ; Condition check to increment the counter if the there is a K - countdown present in the array ; Returning the count of K - countdowns ; Driver code ; Function Call
using System ; class GFG { public static int countKCountdown ( int [ ] arr , int N , int K ) { int flag = - 1 ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == K ) flag = K ; if ( arr [ i ] == flag ) flag -- ; else flag = - 1 ; if ( flag == 0 ) count ++ ; } return count ; } public static void Main ( ) { int N = 8 ; int K = 3 ; int [ ] arr = { 4 , 3 , 2 , 1 , 5 , 3 , 2 , 1 } ; Console . Write ( countKCountdown ( arr , N , K ) ) ; } }
Convert the given RGB color code to Hex color code | C # code to convert the given RGB color code to Hex color code ; Function to convert decimal to hexadecimal ; char array to store hexadecimal number ; Counter for hexadecimal number array ; Temporary variable to store remainder ; Storing remainder in temp variable . ; Check if temp < 10 ; Return the equivalent hexadecimal color code ; Function to convert the RGB code to Hex color code ; The hex color code doesn 't exist ; Driver code
using System ; class GFG { static string decToHexa ( int n ) { char [ ] hexaDeciNum = new char [ 2 ] ; int i = 0 ; while ( n != 0 ) { int temp = 0 ; temp = n % 16 ; if ( temp < 10 ) { hexaDeciNum [ i ] = ( char ) ( temp + 48 ) ; i ++ ; } else { hexaDeciNum [ i ] = ( char ) ( temp + 55 ) ; i ++ ; } n = n / 16 ; } string hexCode = " " ; if ( i == 2 ) { hexCode += hexaDeciNum [ 0 ] ; hexCode += hexaDeciNum [ 1 ] ; } else if ( i == 1 ) { hexCode = "0" ; hexCode += hexaDeciNum [ 0 ] ; } else if ( i == 0 ) hexCode = "00" ; return hexCode ; } static string convertRGBtoHex ( int R , int G , int B ) { if ( ( R >= 0 && R <= 255 ) && ( G >= 0 && G <= 255 ) && ( B >= 0 && B <= 255 ) ) { string hexCode = " # " ; hexCode += decToHexa ( R ) ; hexCode += decToHexa ( G ) ; hexCode += decToHexa ( B ) ; return hexCode ; } else return " - 1" ; } public static void Main ( string [ ] args ) { int R = 0 , G = 0 , B = 0 ; Console . Write ( convertRGBtoHex ( R , G , B ) + " STRNEWLINE " ) ; R = 255 ; G = 255 ; B = 255 ; Console . Write ( convertRGBtoHex ( R , G , B ) + " STRNEWLINE " ) ; R = 25 ; G = 56 ; B = 123 ; Console . Write ( convertRGBtoHex ( R , G , B ) + " STRNEWLINE " ) ; R = 2 ; G = 3 ; B = 4 ; Console . Write ( convertRGBtoHex ( R , G , B ) + " STRNEWLINE " ) ; R = 255 ; G = 255 ; B = 256 ; Console . Write ( convertRGBtoHex ( R , G , B ) + " STRNEWLINE " ) ; } }
Strings formed from given characters without any consecutive repeating characters | C # implementation of the above approach ; Function to print the strings which satisfy the mentioned conditions ; Iterate through all the strings in the array . ; check function to check the conditions for every string ; Function to check whether the string contains any consecutive repetitive characters and any characters other than those in str ; Valid characters check ; Nonrepetitive check ; Driver code
using System ; class GFG { public static void getStrings ( String str , String [ ] arr ) { for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( check ( arr [ i ] , str ) ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } } public static bool check ( String s , String str ) { char [ ] chars = s . ToCharArray ( ) ; foreach ( char c in chars ) { if ( ! str . Contains ( String . Join ( " " , c ) ) ) { return false ; } } for ( int i = 0 ; i < chars . Length - 1 ; i ++ ) { if ( chars [ i ] == chars [ i + 1 ] ) { return false ; } } return true ; } public static void Main ( String [ ] args ) { String str = " ABCD " ; String [ ] arr = { " AABCDA " , " ABCDZADC " , " ABCDBCA " , " ABCDABDCA " } ; getStrings ( str , arr ) ; } }
Program to build a DFA to accept strings that start and end with same character | C # Program for DFA that accepts string if it starts and ends with same character ; Function for the state Q1 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q2 ; Function for the state Q2 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q2 ; Function for the state Q3 ; Condition to check end of string ; State transitions ' a ' takes to q4 , and ' b ' takes to q3 ; Function for the state Q4 ; Condition to check end of string ; State transitions ' a ' takes to q4 , and ' b ' takes to q3 ; Function for the state Q0 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q3 ; Driver Code ; Since q0 is the starting state Send the string to q0
using System ; class GFG { static void q1 ( string s , int i ) { if ( i == s . Length ) { Console . WriteLine ( " Yes " ) ; return ; } if ( s [ i ] == ' a ' ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } static void q2 ( string s , int i ) { if ( i == s . Length ) { Console . WriteLine ( " No " ) ; return ; } if ( s [ i ] == ' a ' ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } static void q3 ( string s , int i ) { if ( i == s . Length ) { Console . WriteLine ( " Yes " ) ; return ; } if ( s [ i ] == ' a ' ) q4 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } static void q4 ( string s , int i ) { if ( i == s . Length ) { Console . WriteLine ( " No " ) ; return ; } if ( s [ i ] == ' a ' ) q4 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } static void q0 ( string s , int i ) { if ( i == s . Length ) { Console . WriteLine ( " No " ) ; return ; } if ( s [ i ] == ' a ' ) q1 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } public static void Main ( ) { string s = " abbaabb " ; q0 ( s , 0 ) ; } }
Longest prefix in a string with highest frequency | C # implementation of the above approach ; Function to find Longest prefix string with the highest frequency ; storing all indices where first element is found ; if the first letter in the string does not occur again then answer will be the whole string ; loop till second appearance of the first element ; check one letter after every stored index ; If there is no mismatch we move forward ; otherwise we stop ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void prefix ( char [ ] str ) { int k = 1 , j ; int n = str . Length ; List < int > g = new List < int > ( ) ; int flag = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( str [ i ] == str [ 0 ] ) { g . Add ( i ) ; flag = 1 ; } } if ( flag == 0 ) { Console . WriteLine ( String . Join ( " " , str ) ) ; } else { int len = g . Count ; while ( k < g [ 0 ] ) { int cnt = 0 ; for ( j = 0 ; j < len ; j ++ ) { if ( ( g [ j ] + k ) < n && str [ g [ j ] + k ] == str [ k ] ) { cnt ++ ; } } if ( cnt == len ) { k ++ ; } else { break ; } } for ( int i = 0 ; i < k ; i ++ ) { Console . Write ( str [ i ] ) ; } Console . WriteLine ( ) ; } } public static void Main ( ) { String str = " abcab " ; prefix ( str . ToCharArray ( ) ) ; } }
Generate permutation of 1 to N such that absolute difference of consecutive numbers give K distinct integers | C # implementation of the above approach ; Function to generate a permutation of integers from 1 to N such that the absolute difference of all the two consecutive integers give K distinct integers ; To store the permutation ; For sequence 1 2 3. . . ; For sequence N , N - 1 , N - 2. . . ; Flag is used to alternate between the above if else statements ; If last element added was r + 1 ; If last element added was l - 1 ; Print the permutation ; Driver code
using System ; using System . Collections ; class GFG { static void printPermutation ( int N , int K ) { ArrayList res = new ArrayList ( ) ; int l = 1 , r = N , flag = 0 ; for ( int i = 0 ; i < K ; i ++ ) { if ( flag == 0 ) { res . Add ( l ) ; l ++ ; } else { res . Add ( r ) ; r -- ; } flag ^= 1 ; } if ( flag != 1 ) { for ( int i = r ; i >= l ; i -- ) { res . Add ( i ) ; } } else { for ( int i = l ; i <= r ; i ++ ) { res . Add ( i ) ; } } foreach ( int i in res ) { Console . Write ( i + " ▁ " ) ; } } public static void Main ( ) { int N = 10 , K = 4 ; printPermutation ( N , K ) ; } }
Find Nth term of the series 1 , 8 , 54 , 384. . . | C # program to find N - th term of the series : 1 , 8 , 54 , 384. . . ; Calculate factorial of N ; By using above formula ; Driver Code
using System ; class GFG { public static int fact ( int N ) { int i , product = 1 ; for ( i = 1 ; i <= N ; i ++ ) product = product * i ; return product ; } public static int nthTerm ( int N ) { return ( N * N ) * fact ( N ) ; } public static void Main ( String [ ] args ) { Console . WriteLine ( nthTerm ( N ) ) ; } }