"0 | A Dynamic Programming based solution for 0 - 1 Knapsack problem ; A utility function that returns maximum of two integers ; Returns the maximum value that can be put in a knapsack of capacity W ; Build table K [ ] [ ] in bottom up manner ; Driver code"
Solution in C#:

using System ; class GFG { static int max ( int a , int b ) { return ( a > b ) ? a : b ; } static int knapSack ( int W , int [ ] wt , int [ ] val , int n ) { int i , w ; int [ , ] K = new int [ n + 1 , W + 1 ] ; for ( i = 0 ; i <= n ; i ++ ) { for ( w = 0 ; w <= W ; w ++ ) { if ( i == 0 w == 0 ) K [ i , w ] = 0 ; else if ( wt [ i - 1 ] <= w ) K [ i , w ] = Math . Max ( val [ i - 1 ] + K [ i - 1 , w - wt [ i - 1 ] ] , K [ i - 1 , w ] ) ; else K [ i , w ] = K [ i - 1 , w ] ; } } return K [ n , W ] ; } static void Main ( ) { int [ ] val = new int [ ] { 60 , 100 , 120 } ; int [ ] wt = new int [ ] { 10 , 20 , 30 } ; int W = 50 ; int n = val . Length ; Console . WriteLine ( knapSack ( W , wt , val , n ) ) ; } }


"0 | A Naive recursive implementation of 0 - 1 Knapsack problem ; A utility function that returns maximum of two integers ; Returns the maximum value that can be put in a knapsack of capacity W ; Base Case ; If weight of the nth item is more than Knapsack capacity W , then this item cannot be included in the optimal solution ; Return the maximum of two cases : ( 1 ) nth item included ( 2 ) not included ; Driver code"
Solution in C#:

using System ; class GFG { static int max ( int a , int b ) { return ( a > b ) ? a : b ; } static int knapSack ( int W , int [ ] wt , int [ ] val , int n ) { if ( n == 0 W == 0 ) return 0 ; if ( wt [ n - 1 ] > W ) return knapSack ( W , wt , val , n - 1 ) ; else return max ( val [ n - 1 ] + knapSack ( W - wt [ n - 1 ] , wt , val , n - 1 ) , knapSack ( W , wt , val , n - 1 ) ) ; } public static void Main ( ) { int [ ] val = new int [ ] { 60 , 100 , 120 } ; int [ ] wt = new int [ ] { 10 , 20 , 30 } ; int W = 50 ; int n = val . Length ; Console . WriteLine ( knapSack ( W , wt , val , n ) ) ; } }


"0 | C # implementation of the approach ; To store states of DP ; To check if a state has been solved ; Function to compute the states ; Base case ; Check if a state has been solved ; Setting a state as solved ; Returning the solved state ; Function to pre - compute the states dp [ 0 ] [ 0 ] , dp [ 0 ] [ 1 ] , . . , dp [ 0 ] [ C_MAX ] ; Function to answer a query in O ( 1 ) ; Driver code ; Performing required pre - computation ; Perform queries"
Solution in C#:

using System ; class GFG { static int C_MAX = 30 ; static int max_arr_len = 10 ; static int [ , ] dp = new int [ max_arr_len , C_MAX + 1 ] ; static bool [ , ] v = new bool [ max_arr_len , C_MAX + 1 ] ; static int findMax ( int i , int r , int [ ] w , int n ) { if ( r < 0 ) return Int32 . MaxValue ; if ( i == n ) return 0 ; if ( v [ i , r ] ) return dp [ i , r ] ; v [ i , r ] = true ; dp [ i , r ] = Math . Max ( w [ i ] + findMax ( i + 1 , r - w [ i ] , w , n ) , findMax ( i + 1 , r , w , n ) ) ; return dp [ i , r ] ; } static void preCompute ( int [ ] w , int n ) { for ( int i = C_MAX ; i >= 0 ; i -- ) findMax ( 0 , i , w , n ) ; } static int ansQuery ( int w ) { return dp [ 0 , w ] ; } public static void Main ( ) { int [ ] w = { 3 , 8 , 9 } ; int n = w . Length ; preCompute ( w , n ) ; int [ ] queries = { 11 , 10 , 4 } ; int q = queries . Length ; for ( int i = 0 ; i < q ; i ++ ) Console . WriteLine ( ansQuery ( queries [ i ] ) ) ; } }


"1 ' s ▁ and ▁ 2' s complement of a Binary Number | C # program to print 1 ' s ▁ and ▁ 2' s complement of a binary number ; Returns '0' for '1' and '1' for '0' ; Print 1 ' s ▁ and ▁ 2' s complement of binary number represented by " bin " ; for ones complement flip every bit ; for two 's complement go from right to left in  ones complement and if we get 1 make, we make  them 0 and keep going left when we get first  0, make that 1 and go out of loop ; If No break : all are 1 as in 111 or 11111 ; in such case , add extra 1 at beginning ; Driver code"
Solution in C#:

using System ; class GFG { static char flip ( char c ) { return ( c == '0' ) ? '1' : '0' ; } static void printOneAndTwosComplement ( String bin ) { int n = bin . Length ; int i ; String ones = " " , twos = " " ; ones = twos = " " ; for ( i = 0 ; i < n ; i ++ ) { ones += flip ( bin [ i ] ) ; } twos = ones ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( ones [ i ] == '1' ) { twos = twos . Substring ( 0 , i ) + '0' + twos . Substring ( i + 1 , twos . Length - ( i + 1 ) ) ; } else { twos = twos . Substring ( 0 , i ) + '1' + twos . Substring ( i + 1 , twos . Length - ( i + 1 ) ) ; break ; } } if ( i == - 1 ) { twos = '1' + twos ; } Console . WriteLine ( "1 ' s ▁ complement : ▁ " + ones ) ; ; Console . WriteLine ( "2 ' s ▁ complement : ▁ " + twos ) ; } public static void Main ( String [ ] args ) { String bin = "1100" ; printOneAndTwosComplement ( bin ) ; } }


"1 to n bit numbers with no consecutive 1 s in binary representation | C # Code to Print all numbers upto n bits with no consecutive set bits . ; Let us first compute 2 raised to power n . ; loop 1 to n to check all the numbers ; A number i doesn ' t ▁ contain ▁ ▁ consecutive ▁ set ▁ bits ▁ if ▁ ▁ bitwise ▁ and ▁ of ▁ i ▁ and ▁ left ▁ ▁ shifted ▁ i ▁ do ' t contain a commons set bit . ; Driver code"
Solution in C#:

using System ; class GFG { static void printNonConsecutive ( int n ) { int p = ( 1 << n ) ; for ( int i = 1 ; i < p ; i ++ ) if ( ( i & ( i << 1 ) ) == 0 ) Console . Write ( i + " ▁ " ) ; } public static void Main ( ) { int n = 3 ; printNonConsecutive ( n ) ; } }


"10 's Complement of a decimal number | C # program to find 10 's complement ; Function to find 10 's complement ; Calculating total digits in num ; restore num ; calculate 10 's complement ; Driver code"
Solution in C#:

using System ; class GFG { static int complement ( int num ) { int len = 0 , temp , comp ; temp = num ; while ( true ) { len ++ ; num = num / 10 ; if ( Math . Abs ( num ) == 0 ) break ; } num = temp ; comp = ( int ) Math . Pow ( 10 , len ) - num ; return comp ; } public static void Main ( ) { Console . WriteLine ( complement ( 25 ) ) ; Console . WriteLine ( complement ( 456 ) ) ; } }


"120 | C # program for above approach ; Function to find the nth 120 - gon Number ; Driver code"
Solution in C#:

using System ; class GFG { static int gonNum120 ( int n ) { return ( 118 * n * n - 116 * n ) / 2 ; } public static void Main ( String [ ] args ) { int n = 3 ; Console . Write ( gonNum120 ( n ) ) ; } }


"2 's complement for a givin string using XOR | C # program to find 2 's complement using XOR. ; A flag used to find if a 1 bit is seen or not . ; xor operator is used to flip the ; bits after converting in to ASCII values ; If there is no 1 in the string so just add 1 in starting of string and return ; Driver code"
Solution in C#:

using System ; class GFG { static void TwoscomplementbyXOR ( string str ) { int n = str . Length ; bool check_bit = false ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == '0' && check_bit == false ) { continue ; } else { if ( check_bit == true ) { if ( str [ i ] == '0' ) str = str . Substring ( 0 , i ) + '1' + str . Substring ( i + 1 ) ; else str = str . Substring ( 0 , i ) + '0' + str . Substring ( i + 1 ) ; } check_bit = true ; } } if ( check_bit == false ) { Console . WriteLine ( "1" + str ) ; } else Console . WriteLine ( str ) ; } static void Main ( ) { string str = "101" ; TwoscomplementbyXOR ( str ) ; } }


"2 Keys Keyboard Problem | C # program for the above approach ; Function to find the minimum number of steps required to form N number of A 's ; Stores the count of steps needed ; Traverse over the range [ 2 , N ] ; Iterate while N is divisible by d ; Increment the value of ans by d ; Divide N by d ; If N is not 1 ; Return the ans ; Driver Code"
Solution in C#:

using System ; class GFG { static int minSteps ( int N ) { int ans = 0 ; for ( int d = 2 ; d * d <= N ; d ++ ) { while ( N % d == 0 ) { ans += d ; N /= d ; } } if ( N != 1 ) { ans += N ; } return ans ; } static public void Main ( ) { int N = 3 ; Console . Write ( minSteps ( N ) ) ; } }


"21 Matchsticks Problem | C # implementation of the approach ; Function to return the optimal strategy ; Removing matchsticks in blocks of five ; Driver code"
Solution in C#:

 using System ; class GFG { static void TwentyoneMatchstick ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i += 1 ) { Console . Write ( 5 - arr [ i ] + " ▁ " ) ; } Console . Write ( "
 " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 4 , 2 , 2 } ; int N = arr . Length ; TwentyoneMatchstick ( arr , N ) ; } }


"257 | C # program for above approach ; Function to find the nth 257 - gon Number ; Driver code"
Solution in C#:

using System ; class GFG { static int gonNum257 ( int n ) { return ( 255 * n * n - 253 * n ) / 2 ; } public static void Main ( String [ ] args ) { int n = 3 ; Console . Write ( gonNum257 ( n ) ) ; } }


"3 | C # implementation of the approach ; Function that returns true if n is an Osiris number ; 3 rd digit ; 2 nd digit ; 1 st digit ; Check the required condition ; Driver code"
Solution in C#:

using System ; class GFG { static bool isOsiris ( int n ) { int a = n % 10 ; int b = ( n / 10 ) % 10 ; int c = n / 100 ; int digit_sum = a + b + c ; if ( n == ( 2 * ( digit_sum ) * 11 ) ) { return true ; } return false ; } static void Main ( ) { int n = 132 ; if ( isOsiris ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"3 | C # program for 3 - way quick sort ; A function which is used to swap values ; A utility function to print an array ; It uses Dutch National Flag Algorithm ; To handle 2 elements ; update i and j ; j = mid ; or high + 1 ; 3 - way partition based quick sort ; if ( low >= high ) 1 or 0 elements ; Note that i and j are passed as reference ; Recur two halves ; Driver code ; int [ ] a = { 4 , 39 , 54 , 14 , 31 , 89 , 44 , 34 , 59 , 64 , 64 , 11 , 41 } ; int [ ] a = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int [ ] a = { 91 , 82 , 73 , 64 , 55 , 46 , 37 , 28 , 19 , 10 } ; int [ ] a = { 4 , 9 , 4 , 4 , 9 , 1 , 1 , 1 } ; ; Function Call"
Solution in C#:

 using System ; class GFG { static void swap < T > ( ref T lhs , ref T rhs ) { T temp ; temp = lhs ; lhs = rhs ; rhs = temp ; } public static void printarr ( int [ ] a , int n ) { for ( int i = 0 ; i < n ; ++ i ) Console . Write ( a [ i ] + " ▁ " ) ; Console . Write ( "
 " ) ; } public static void partition ( int [ ] a , int low , int high , ref int i , ref int j ) { if ( high - low <= 1 ) { if ( a [ high ] < a [ low ] ) swap ( ref a [ high ] , ref a [ low ] ) ; i = low ; j = high ; return ; } int mid = low ; int pivot = a [ high ] ; while ( mid <= high ) { if ( a [ mid ] < pivot ) swap ( ref a [ low ++ ] , ref a [ mid ++ ] ) ; else if ( a [ mid ] == pivot ) mid ++ ; else if ( a [ mid ] > pivot ) swap ( ref a [ mid ] , ref a [ high -- ] ) ; } i = low - 1 ; } public static void quicksort ( int [ ] a , int low , int high ) { return ; int i = 0 , j = 0 ; partition ( a , low , high , ref i , ref j ) ; quicksort ( a , low , i ) ; quicksort ( a , j , high ) ; } static void Main ( ) { int [ ] a = { 4 , 9 , 4 , 4 , 1 , 9 , 4 , 4 , 9 , 4 , 4 , 1 , 4 } ; int size = a . Length ; printarr ( a , size ) ; quicksort ( a , 0 , size - 1 ) ; printarr ( a , size ) ; } }


"3 | C # program for 3 - way quick sort ; This function partitions a [ ] in three parts a ) a [ l . . i ] contains all elements smaller than pivot b ) a [ i + 1. . j - 1 ] contains all occurrences of pivot c ) a [ j . . r ] contains all elements greater than pivot ; From left , find the first element greater than or equal to v . This loop will definitely terminate as v is last element ; From right , find the first element smaller than or equal to v ; If i and j cross , then we are done ; Swap , so that smaller goes on left greater goes on right ; Move all same left occurrence of pivot to beginning of array and keep count using p ; Move all same right occurrence of pivot to end of array and keep count using q ; Move pivot element to its correct index ; Move all left same occurrences from beginning to adjacent to arr [ i ] ; Move all right same occurrences from end to adjacent to arr [ i ] ; 3 - way partition based quick sort ; Note that i and j are passed as reference ; Recur ; A utility function to print an array ; Driver code ; Function Call"
Solution in C#:

 using System ; class GFG { public static void partition ( int [ ] a , int l , int r , ref int i , ref int j ) { i = l - 1 ; j = r ; int p = l - 1 , q = r ; int v = a [ r ] ; while ( true ) { while ( a [ ++ i ] < v ) ; while ( v < a [ -- j ] ) if ( j == l ) break ; if ( i >= j ) break ; swap ( ref a [ i ] , ref a [ j ] ) ; if ( a [ i ] == v ) { p ++ ; swap ( ref a [ p ] , ref a [ i ] ) ; } if ( a [ j ] == v ) { q -- ; swap ( ref a [ j ] , ref a [ q ] ) ; } } swap ( ref a [ i ] , ref a [ r ] ) ; j = i - 1 ; for ( int k = l ; k < p ; k ++ , j -- ) swap ( ref a [ k ] , ref a [ j ] ) ; i = i + 1 ; for ( int k = r - 1 ; k > q ; k -- , i ++ ) swap ( ref a [ i ] , ref a [ k ] ) ; } public static void quicksort ( int [ ] a , int l , int r ) { if ( r <= l ) return ; int i = 0 , j = 0 ; partition ( a , l , r , ref i , ref j ) ; quicksort ( a , l , j ) ; quicksort ( a , i , r ) ; } public static void printarr ( int [ ] a , int n ) { for ( int i = 0 ; i < n ; ++ i ) Console . Write ( a [ i ] + " ▁ " ) ; Console . Write ( "
 " ) ; } static void Main ( ) { int [ ] a = { 4 , 9 , 4 , 4 , 1 , 9 , 4 , 4 , 9 , 4 , 4 , 1 , 4 } ; int size = a . Length ; static void swap < T > ( ref T lhs , ref T rhs ) { T temp ; temp = lhs ; lhs = rhs ; rhs = temp ; } printarr ( a , size ) ; quicksort ( a , 0 , size - 1 ) ; printarr ( a , size ) ; } }


"360 | C # program for above approach ; Function to find the nth 360 - gon Number ; Driver code"
Solution in C#:

using System ; class GFG { static int gonNum360 ( int n ) { return ( 358 * n * n - 356 * n ) / 2 ; } public static void Main ( String [ ] args ) { int n = 3 ; Console . Write ( gonNum360 ( n ) ) ; } }


"65537 | C # program for above approach ; Function to find the nth 65537 - gon Number ; Driver code"
Solution in C#:

using System ; class GFG { static int gonNum65537 ( int n ) { return ( 65535 * n * n - 65533 * n ) / 2 ; } public static void Main ( String [ ] args ) { int n = 3 ; Console . Write ( gonNum65537 ( n ) ) ; } }


"9 's complement of a decimal number | C # program to find 9 's complement of a number. ; Driver code"
Solution in C#:

using System ; class GFG { static void complement ( string number1 ) { char [ ] number = number1 . ToCharArray ( ) ; for ( int i = 0 ; i < number . Length ; i ++ ) if ( number [ i ] != ' . ' ) number [ i ] = ( char ) ( ( int ) ( '9' ) - ( int ) ( number [ i ] ) + ( int ) ( '0' ) ) ; System . Console . WriteLine ( "9 ' s ▁ complement ▁ is ▁ : ▁ " + new string ( number ) ) ; } public static void Main ( ) { String number = "345.45" ; complement ( number ) ; } }


"A Binary String Game | C # program for the above approach ; Function to return the result of the game ; length of the string ; List to maintain the lengths of consecutive '1' s in the string ; Variable that keeps a track of the current length of the block of consecutive '1' s ; Adds non - zero lengths ; This takes care of the case when the last character is '1' ; Sorts the lengths in descending order ; Scores of the 2 players ; For player 1 ; For player 2 ; In case of a tie ; Print the result ; Driver Code ; Given string S ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static String gameMax ( String S ) { int N = S . Length ; List < int > list = new List < int > ( ) ; int one = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '1' ) { one ++ ; } else { if ( one != 0 ) { list . Add ( one ) ; } one = 0 ; } } if ( one != 0 ) { list . Add ( one ) ; } list . Sort ( ) ; list . Reverse ( ) ; int score_1 = 0 , score_2 = 0 ; for ( int i = 0 ; i < list . Count ; i ++ ) { if ( list [ i ] % 2 == 1 ) { score_1 += list [ i ] ; } else { score_2 += list [ i ] ; } } if ( score_1 == score_2 ) return " - 1" ; return ( score_1 > score_2 ) ? " Player ▁ 1" : " Player ▁ 2" ; } public static void Main ( String [ ] args ) { String S = "11111101" ; Console . WriteLine ( gameMax ( S ) ) ; } }


"A Boolean Matrix Question | C # Code For A Boolean Matrix Question ; Initialize all values of row [ ] as 0 ; Initialize all values of col [ ] as 0 ; Store the rows and columns to be marked as 1 in row [ ] and col [ ] arrays respectively ; Modify the input matrix mat [ ] using the above constructed row [ ] and col [ ] arrays ; A utility function to print a 2D matrix ; Driver code"
Solution in C#:

using System ; class GFG { public static void modifyMatrix ( int [ , ] mat , int R , int C ) { int [ ] row = new int [ R ] ; int [ ] col = new int [ C ] ; int i , j ; for ( i = 0 ; i < R ; i ++ ) { row [ i ] = 0 ; } for ( i = 0 ; i < C ; i ++ ) { col [ i ] = 0 ; } for ( i = 0 ; i < R ; i ++ ) { for ( j = 0 ; j < C ; j ++ ) { if ( mat [ i , j ] == 1 ) { row [ i ] = 1 ; col [ j ] = 1 ; } } } for ( i = 0 ; i < R ; i ++ ) { for ( j = 0 ; j < C ; j ++ ) { if ( row [ i ] == 1 col [ j ] == 1 ) { mat [ i , j ] = 1 ; } } } } public static void printMatrix ( int [ , ] mat , int R , int C ) { int i , j ; for ( i = 0 ; i < R ; i ++ ) { for ( j = 0 ; j < C ; j ++ ) { Console . Write ( mat [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } static public void Main ( ) { int [ , ] mat = { { 1 , 0 , 0 , 1 } , { 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 } } ; Console . WriteLine ( " Matrix ▁ Intially " ) ; printMatrix ( mat , 3 , 4 ) ; modifyMatrix ( mat , 3 , 4 ) ; Console . WriteLine ( " Matrix ▁ after ▁ " + " modification ▁ n " ) ; printMatrix ( mat , 3 , 4 ) ; } }


"A Boolean Matrix Question | C # Code For A Boolean Matrix Question ; variables to check if there are any 1 in first row and column ; updating the first row and col if 1 is encountered ; Modify the input matrix mat [ ] using the first row and first column of Matrix mat ; modify first row if there was any 1 ; modify first col if there was any 1 ; A utility function to print a 2D matrix ; Driver Code"
Solution in C#:

 using System ; class GFG { public static void modifyMatrix ( int [ , ] mat ) { bool row_flag = false ; bool col_flag = false ; for ( int i = 0 ; i < mat . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < mat . GetLength ( 1 ) ; j ++ ) { if ( i == 0 && mat [ i , j ] == 1 ) row_flag = true ; if ( j == 0 && mat [ i , j ] == 1 ) col_flag = true ; if ( mat [ i , j ] == 1 ) { mat [ 0 , j ] = 1 ; mat [ i , 0 ] = 1 ; } } } for ( int i = 1 ; i < mat . GetLength ( 0 ) ; i ++ ) { for ( int j = 1 ; j < mat . GetLength ( 1 ) ; j ++ ) { if ( mat [ 0 , j ] == 1 mat [ i , 0 ] == 1 ) { mat [ i , j ] = 1 ; } } } if ( row_flag == true ) { for ( int i = 0 ; i < mat . GetLength ( 1 ) ; i ++ ) { mat [ 0 , i ] = 1 ; } } if ( col_flag == true ) { for ( int i = 0 ; i < mat . GetLength ( 0 ) ; i ++ ) { mat [ i , 0 ] = 1 ; } } } public static void printMatrix ( int [ , ] mat ) { for ( int i = 0 ; i < mat . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < mat . GetLength ( 1 ) ; j ++ ) { Console . Write ( mat [ i , j ] + " ▁ " ) ; } Console . Write ( "
 " ) ; } } public static void Main ( ) { int [ , ] mat = { { 1 , 0 , 0 , 1 } , { 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 } } ; Console . Write ( " Input ▁ Matrix ▁ :
 " ) ; printMatrix ( mat ) ; modifyMatrix ( mat ) ; Console . Write ( " Matrix ▁ After ▁ " + " Modification ▁ :
 " ) ; printMatrix ( mat ) ; } }


"A Product Array Puzzle |  ; Function to print product array for a given array arr [ ] of size n ; Base case ; Allocate memory for the product array ; Initialize the product array as 1 ; In this loop , temp variable contains product of elements on left side excluding arr [ i ] ; Initialize temp to 1 for product on right side ; In this loop , temp variable contains product of elements on right side excluding arr [ i ] ; print the constructed prod array ; Driver program to test above functions"
Solution in C#:

using System ; class GFG { static void productArray ( int [ ] arr , int n ) { if ( n == 1 ) { Console . Write ( 0 ) ; return ; } int i , temp = 1 ; int [ ] prod = new int [ n ] ; for ( int j = 0 ; j < n ; j ++ ) prod [ j ] = 1 ; for ( i = 0 ; i < n ; i ++ ) { prod [ i ] = temp ; temp *= arr [ i ] ; } temp = 1 ; for ( i = n - 1 ; i >= 0 ; i -- ) { prod [ i ] *= temp ; temp *= arr [ i ] ; } for ( i = 0 ; i < n ; i ++ ) Console . Write ( prod [ i ] + " ▁ " ) ; return ; } public static void Main ( ) { int [ ] arr = { 10 , 3 , 5 , 6 , 2 } ; int n = arr . Length ; Console . WriteLine ( " The ▁ product ▁ array ▁ is ▁ : ▁ " ) ; productArray ( arr , n ) ; } }


"A Space Optimized DP solution for 0 | C # program of a space optimized DP solution for 0 - 1 knapsack problem . ; val [ ] is for storing maximum profit for each weight wt [ ] is for storing weights n number of item W maximum capacity of bag dp [ W + 1 ] to store final result ; array to store final result dp [ i ] stores the profit with KnapSack capacity " i " ; initially profit with 0 to W KnapSack capacity is 0 ; iterate through all items ; traverse dp array from right to left ; above line finds out maximum of dp [ j ] ( excluding ith element value ) and val [ i ] + dp [ j - wt [ i ] ] ( including ith element value and the profit with " KnapSack ▁ capacity ▁ - ▁ ith ▁ element ▁ weight " ) ; Driver code"
Solution in C#:

using System ; class GFG { static int KnapSack ( int [ ] val , int [ ] wt , int n , int W ) { int [ ] dp = new int [ W + 1 ] ; for ( int i = 0 ; i < W + 1 ; i ++ ) dp [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = W ; j >= wt [ i ] ; j -- ) dp [ j ] = Math . Max ( dp [ j ] , val [ i ] + dp [ j - wt [ i ] ] ) ; return dp [ W ] ; } public static void Main ( String [ ] args ) { int [ ] val = { 7 , 8 , 4 } ; int [ ] wt = { 3 , 8 , 6 } ; int W = 10 , n = 3 ; Console . WriteLine ( KnapSack ( val , wt , n , W ) ) ; } }


"A Space Optimized DP solution for 0 | C # program of a space optimized DP solution for 0 - 1 knapsack problem . ; val [ ] is for storing maximum profit for each weight wt [ ] is for storing weights n number of item W maximum capacity of bag mat [ 2 , W + 1 ] to store final result ; matrix to store final result ; iterate through all items ; one by one traverse each element ; traverse all weights j <= W ; if i is odd that mean till now we have odd number of elements so we store result in 1 th indexed row ; check for each value ; include element ; exclude element ; if i is even that means till now we have even number of elements so we store result in 0 th indexed row ; Return mat [ 0 , W ] if n is odd , else mat [ 1 , W ] ; Driver Code"
Solution in C#:

using System ; class GFG { static int KnapSack ( int [ ] val , int [ ] wt , int n , int W ) { int [ , ] mat = new int [ 2 , W + 1 ] ; int i = 0 ; while ( i < n ) { int j = 0 ; if ( i % 2 != 0 ) { while ( ++ j <= W ) { if ( wt [ i ] <= j ) { mat [ 1 , j ] = Math . Max ( val [ i ] + mat [ 0 , j - wt [ i ] ] , mat [ 0 , j ] ) ; } else { mat [ 1 , j ] = mat [ 0 , j ] ; } } } else { while ( ++ j <= W ) { if ( wt [ i ] <= j ) { mat [ 0 , j ] = Math . Max ( val [ i ] + mat [ 1 , j - wt [ i ] ] , mat [ 1 , j ] ) ; } else { mat [ 0 , j ] = mat [ 1 , j ] ; } } } i ++ ; } return ( n % 2 != 0 ) ? mat [ 0 , W ] : mat [ 1 , W ] ; } public static void Main ( String [ ] args ) { int [ ] val = { 7 , 8 , 4 } ; int [ ] wt = { 3 , 8 , 6 } ; int W = 10 , n = 3 ; Console . WriteLine ( KnapSack ( val , wt , n , W ) ) ; } }


"A Space Optimized Solution of LCS | C # Code for A Space Optimized Solution of LCS ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Find lengths of two strings ; Binary index , used to index current row and previous row . ; Compute current binary index ; Last filled entry contains length of LCS for X [ 0. . n - 1 ] and Y [ 0. . m - 1 ] ; Driver Code"
Solution in C#:

using System ; class GFG { public static int lcs ( string X , string Y ) { int m = X . Length , n = Y . Length ; int [ , ] L = new int [ 2 , n + 1 ] ; int bi = 0 ; for ( int i = 0 ; i <= m ; i ++ ) { bi = i & 1 ; for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ bi , j ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) L [ bi , j ] = L [ 1 - bi , j - 1 ] + 1 ; else L [ bi , j ] = Math . Max ( L [ 1 - bi , j ] , L [ bi , j - 1 ] ) ; } } return L [ bi , n ] ; } public static void Main ( ) { string X = " AGGTAB " ; string Y = " GXTXAYB " ; Console . Write ( " Length ▁ of ▁ LCS ▁ is ▁ " + lcs ( X , Y ) ) ; } }


"A Sum Array Puzzle | C # implementation of above approach ; Allocate memory for temporary arrays leftSum [ ] , rightSum [ ] and Sum [ ] ; Left most element of left array is always 0 ; Right most element of right array is always 0 ; Construct the left array ; Construct the right array ; Construct the sum array using left [ ] and right [ ] ; print the sum array ; Driver function to test above function"
Solution in C#:

using System ; class Geeks { public static void sumArray ( int [ ] arr , int n ) { int [ ] leftSum = new int [ n ] ; int [ ] rightSum = new int [ n ] ; int [ ] Sum = new int [ n ] ; int i = 0 , j = 0 ; leftSum [ 0 ] = 0 ; rightSum [ n - 1 ] = 0 ; for ( i = 1 ; i < n ; i ++ ) leftSum [ i ] = arr [ i - 1 ] + leftSum [ i - 1 ] ; for ( j = n - 2 ; j >= 0 ; j -- ) rightSum [ j ] = arr [ j + 1 ] + rightSum [ j + 1 ] ; for ( i = 0 ; i < n ; i ++ ) Sum [ i ] = leftSum [ i ] + rightSum [ i ] ; for ( i = 0 ; i < n ; i ++ ) Console . Write ( Sum [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 3 , 6 , 4 , 8 , 9 } ; int n = arr . Length ; sumArray ( arr , n ) ; } }


"A backtracking approach to generate n bit Gray Codes | C # program to find the gray sequence of n bits . ; we have 2 choices for each of the n bits either we can include i . e invert the bit or we can exclude the bit i . e we can leave the number as it is . ; base case when we run out bits to process we simply include it in gray code sequence . ; ignore the bit . ; invert the bit . ; returns the vector containing the gray code sequence of n bits . ; num is passed by reference to keep track of current code . ; Driver function ."
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static int num ; static void grayCodeUtil ( List < int > res , int n ) { if ( n == 0 ) { res . Add ( num ) ; return ; } grayCodeUtil ( res , n - 1 ) ; num = num ^ ( 1 << ( n - 1 ) ) ; grayCodeUtil ( res , n - 1 ) ; } static List < int > grayCodes ( int n ) { List < int > res = new List < int > ( ) ; num = 0 ; grayCodeUtil ( res , n ) ; return res ; } public static void Main ( String [ ] args ) { int n = 3 ; List < int > code = grayCodes ( n ) ; for ( int i = 0 ; i < code . Count ; i ++ ) Console . Write ( code [ i ] + "
 " ) ; } }


"A product array puzzle | Set 2 ( O ( 1 ) Space ) | C # program for product array puzzle with O ( n ) time and O ( 1 ) space . ; epsilon value to maintain precision ; to hold sum of all values ; output product for each index anti log to find original product value ; Driver code"
Solution in C#:

using System ; class GFG { static double EPS = 1e-9 ; static void productPuzzle ( int [ ] a , int n ) { double sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += Math . Log10 ( a [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( ( int ) ( EPS + Math . Pow ( 10.00 , sum - Math . Log10 ( a [ i ] ) ) ) + " ▁ " ) ; } public static void Main ( ) { int [ ] a = { 10 , 3 , 5 , 6 , 2 } ; int n = a . Length ; Console . WriteLine ( " The ▁ product ▁ array ▁ is : ▁ " ) ; productPuzzle ( a , n ) ; } }


"A program to check if a binary tree is BST or not | C # program to check if a given tree is BST . ; A binary tree node has data , pointer to left child && a pointer to right child / ; Returns true if given tree is BST . ; Base condition ; if left node exist then check it has correct data or not i . e . left node ' s ▁ data ▁ ▁ should ▁ be ▁ less ▁ than ▁ root ' s data ; if right node exist then check it has correct data or not i . e . right node ' s ▁ data ▁ ▁ should ▁ be ▁ greater ▁ than ▁ root ' s data ; check recursively for every node . ; Helper function that allocates a new node with the given data && null left && right pointers . / ; Driver code"
Solution in C#:

using System ; class GFG { public class Node { public int data ; public Node left , right ; } ; static Boolean isBST ( Node root , Node l , Node r ) { if ( root == null ) return true ; if ( l != null && root . data <= l . data ) return false ; if ( r != null && root . data >= r . data ) return false ; return isBST ( root . left , l , root ) && isBST ( root . right , root , r ) ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 3 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 4 ) ; if ( isBST ( root , null , null ) ) Console . Write ( " Is ▁ BST " ) ; else Console . Write ( " Not ▁ a ▁ BST " ) ; } }


"A program to check if a binary tree is BST or not | C # program to check if a given tree is BST . ; A binary tree node has data , pointer to left child and a pointer to right child ; traverse the tree in inorder fashion and keep track of prev node ; Allows only distinct valued nodes ; Driver Code"
Solution in C#:

using System ; public class GFG { public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } ; static Node prev ; static Boolean isBSTUtil ( Node root ) { if ( root != null ) { if ( ! isBSTUtil ( root . left ) ) return false ; if ( prev != null && root . data <= prev . data ) return false ; prev = root ; return isBSTUtil ( root . right ) ; } return true ; } static Boolean isBST ( Node root ) { return isBSTUtil ( root ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( 3 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 5 ) ; root . left . left = new Node ( 1 ) ; root . left . right = new Node ( 4 ) ; if ( isBST ( root ) ) Console . WriteLine ( " Is ▁ BST " ) ; else Console . WriteLine ( " Not ▁ a ▁ BST " ) ; } }


"A sorting algorithm that slightly improves on selection sort | C # program to implement min max selection sort . ; shifting the min . ; Shifting the max . The equal condition happens if we shifted the max to arr [ min_i ] in the previous swap . ; Driver code"
Solution in C#:

 using System ; class GFG { static void minMaxSelectionSort ( int [ ] arr , int n ) { for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { int min = arr [ i ] , max = arr [ i ] ; int min_i = i , max_i = i ; for ( int k = i ; k <= j ; k ++ ) { if ( arr [ k ] > max ) { max = arr [ k ] ; max_i = k ; } else if ( arr [ k ] < min ) { min = arr [ k ] ; min_i = k ; } } swap ( arr , i , min_i ) ; if ( arr [ min_i ] == max ) swap ( arr , j , min_i ) ; else swap ( arr , j , max_i ) ; } } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void Main ( String [ ] args ) { int [ ] arr = { 23 , 78 , 45 , 8 , 32 , 56 , 1 } ; int n = arr . Length ; minMaxSelectionSort ( arr , n ) ; Console . Write ( " Sorted ▁ array :
 " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( " " ) ; } }


"A variation of Rat in a Maze : multiple steps or jumps allowed | C # implementation of the approach ; Function to check whether the path exists ; Declaring and initializing CRF ( can reach from ) matrix ; Using the DP to fill CRF matrix in correct order ; If it is possible to get to a valid location from cell maze [ k , j ] ; If it is possible to get to a valid location from cell maze [ j , k ] ; If CRF [ 0 , 0 ] is false it means we cannot reach the end of the maze at all ; Filling the solution matrix using CRF ; Get to a valid location from the current cell ; Utility function to print the contents of a 2 - D array ; Driver code ; If path exists ; Print the path"
Solution in C#:

using System ; class GFG { static int MAX = 50 ; static Boolean hasPath ( int [ , ] maze , int [ , ] sol , int N ) { int i , j , k ; for ( i = 0 ; i < N ; i ++ ) for ( j = 0 ; j < N ; j ++ ) sol [ i , j ] = 0 ; Boolean [ , ] CRF = new Boolean [ N , N ] ; CRF [ N - 1 , N - 1 ] = true ; for ( k = N - 1 ; k >= 0 ; k -- ) { for ( j = k ; j >= 0 ; j -- ) { if ( ! ( k == N - 1 && j == N - 1 ) ) { for ( int a = 0 ; a <= maze [ k , j ] ; a ++ ) { if ( ( j + a < N && CRF [ k , j + a ] == true ) || ( k + a < N && CRF [ k + a , j ] == true ) ) { CRF [ k , j ] = true ; break ; } } for ( int a = 0 ; a <= maze [ j , k ] ; a ++ ) { if ( ( k + a < N && CRF [ j , k + a ] == true ) || ( j + a < N && CRF [ j + a , k ] == true ) ) { CRF [ j , k ] = true ; break ; } } } } } if ( CRF [ 0 , 0 ] == false ) return false ; i = 0 ; j = 0 ; while ( ! ( i == N - 1 && j == N - 1 ) ) { sol [ i , j ] = 1 ; if ( maze [ i , j ] > 0 ) for ( int a = 1 ; a <= maze [ i , j ] ; a ++ ) { if ( ( j + a < N && CRF [ i , j + a ] == true ) ) { j = j + a ; break ; } else if ( ( i + a < N && CRF [ i + a , j ] == true ) ) { i = i + a ; break ; } } } sol [ N - 1 , N - 1 ] = 1 ; return true ; } static void printMatrix ( int [ , ] sol , int N ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) Console . Write ( sol [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int [ , ] maze = { { 2 , 2 , 1 , 1 , 0 } , { 0 , 0 , 3 , 0 , 0 } , { 1 , 0 , 0 , 0 , 0 } , { 0 , 0 , 2 , 0 , 1 } , { 0 , 0 , 3 , 0 , 0 } } ; int N = maze . GetLength ( 0 ) ; int [ , ] sol = new int [ N , MAX ] ; if ( hasPath ( maze , sol , N ) ) printMatrix ( sol , N ) ; else Console . WriteLine ( " No ▁ path ▁ exists " ) ; } }


"AKS Primality Test | C # code to check if number is prime . This program demonstrates concept behind AKS algorithm and doesn 't implement the actual algorithm (This works only till n = 64) ; array used to store coefficients . ; function to calculate the coefficients of ( x - 1 ) ^ n - ( x ^ n - 1 ) with the help of Pascal 's triangle . ; function to check whether the number is prime or not ; Calculating all the coefficients by the function coef and storing all the coefficients in c array . ; subtracting c [ n ] and adding c [ 0 ] by 1 as ( x - 1 ) ^ n - ( x ^ n - 1 ) , here we are subtracting c [ n ] by 1 and adding 1 in expression . ; checking all the coefficients whether they are divisible by n or not . if n is not prime , then loop breaks and ( i > 0 ) . ; Return true if all coefficients are divisible by n . ; Driver code"
Solution in C#:

using System ; class GFG { static long [ ] c = new long [ 100 ] ; static void coef ( int n ) { c [ 0 ] = 1 ; for ( int i = 0 ; i < n ; c [ 0 ] = - c [ 0 ] , i ++ ) { c [ 1 + i ] = 1 ; for ( int j = i ; j > 0 ; j -- ) c [ j ] = c [ j - 1 ] - c [ j ] ; } } static bool isPrime ( int n ) { coef ( n ) ; c [ 0 ] ++ ; c [ n ] -- ; int i = n ; while ( ( i -- ) > 0 && c [ i ] % n == 0 ) ; return i < 0 ; } public static void Main ( ) { int n = 37 ; if ( isPrime ( n ) ) Console . WriteLine ( " Prime " ) ; else Console . WriteLine ( " Not ▁ Prime " ) ; } }


"AVL with duplicate keys | C # program of AVL tree that handles duplicates ; An AVL tree node ; A utility function to get height of the tree ; A utility function to get maximum of two integers ; Helper function that allocates a new node with the given key and null left and right pointers . ; new node is initially added at leaf ; A utility function to right rotate subtree rooted with y See the diagram given above . ; Perform rotation ; Update heights ; Return new root ; A utility function to left rotate subtree rooted with x See the diagram given above . ; Perform rotation ; Update heights ; Return new root ; Get Balance factor of node N ; 1. Perform the normal BST rotation ; If key already exists in BST , icnrement count and return ; Otherwise , recur down the tree ; 2. Update height of this ancestor node ; 3. Get the balance factor of this ancestor node to check whether this node became unbalanced ; If this node becomes unbalanced , then there are 4 cases Left Left Case ; Right Right Case ; Left Right Case ; Right Left Case ; return the ( unchanged ) node pointer ; Given a non - empty binary search tree , return the node with minimum key value found in that tree . Note that the entire tree does not need to be searched . ; loop down to find the leftmost leaf ; STEP 1 : PERFORM STANDARD BST DELETE ; If the key to be deleted is smaller than the root 's key,  then it lies in left subtree ; If the key to be deleted is greater than the root 's key,  then it lies in right subtree ; if key is same as root 's key,  then this is the node to be deleted ; If key is present more than once , simply decrement count and return ; ElSE , delete the node node with only one child or no child ; No child case ; One child case ; Copy the contents of the non - empty child ; node with two children : Get the inorder successor ( smallest in the right subtree ) ; Copy the inorder successor 's  data to this node and update the count ; Delete the inorder successor ; If the tree had only one node then return ; STEP 2 : UPDATE HEIGHT OF THE..."
Solution in C#:

using System ; class GFG { class node { public int key ; public node left ; public node right ; public int height ; public int count ; } static int height ( node N ) { if ( N == null ) return 0 ; return N . height ; } static int max ( int a , int b ) { return ( a > b ) ? a : b ; } static node newNode ( int key ) { node node = new node ( ) ; node . key = key ; node . left = null ; node . right = null ; node . height = 1 ; node . count = 1 ; return ( node ) ; } static node rightRotate ( node y ) { node x = y . left ; node T2 = x . right ; x . right = y ; y . left = T2 ; y . height = max ( height ( y . left ) , height ( y . right ) ) + 1 ; x . height = max ( height ( x . left ) , height ( x . right ) ) + 1 ; return x ; } static node leftRotate ( node x ) { node y = x . right ; node T2 = y . left ; y . left = x ; x . right = T2 ; x . height = max ( height ( x . left ) , height ( x . right ) ) + 1 ; y . height = max ( height ( y . left ) , height ( y . right ) ) + 1 ; return y ; } static int getBalance ( node N ) { if ( N == null ) return 0 ; return height ( N . left ) - height ( N . right ) ; } static node insert ( node node , int key ) { if ( node == null ) return ( newNode ( key ) ) ; if ( key == node . key ) { ( node . count ) ++ ; return node ; } if ( key < node . key ) node . left = insert ( node . left , key ) ; else node . right = insert ( node . right , key ) ; node . height = max ( height ( node . left ) , height ( node . right ) ) + 1 ; int balance = getBalance ( node ) ; if ( balance > 1 && key < node . left . key ) return rightRotate ( node ) ; if ( balance < - 1 && key > node . right . key ) return leftRotate ( node ) ; if ( balance > 1 && key > node . left . key ) { node . left = leftRotate ( node . left ) ; return rightRotate ( node ) ; } if ( balance < - 1 && key < node . right . key ) { node . right = rightRotate ( node . right ) ; return leftRotate ( node ) ; } return node ; } static node minValueNode ( node node ) { node current = node ; while ( current . left != null ) current = current . left...


"Absolute Difference between the Sum of Non | C # program to find the Absolute Difference between the Sum of Non - Prime numbers and Prime numbers of an Array ; Function to find the difference between the sum of non - primes and the sum of primes of an array . ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store the sum of primes in S1 and the sum of non primes in S2 ; the number is prime ; the number is non - prime ; Return the absolute difference ; Driver Code ; Get the array ; Find the absolute difference"
Solution in C#:

using System ; class GFG { static int CalculateDifference ( int [ ] arr , int n ) { int max_val = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > max_val ) max_val = arr [ i ] ; } bool [ ] prime = new bool [ max_val + 1 ] ; for ( int i = 0 ; i <= max_val ; i ++ ) prime [ i ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } int S1 = 0 , S2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { S1 += arr [ i ] ; } else if ( arr [ i ] != 1 ) { S2 += arr [ i ] ; } } return Math . Abs ( S2 - S1 ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 3 , 5 , 10 , 15 , 7 } ; int n = arr . Length ; Console . WriteLine ( CalculateDifference ( arr , n ) ) ; } }


"Absolute Difference of all pairwise consecutive elements in an array | C # program to print the absolute difference of the consecutive elements ; Function to print pairwise absolute difference of consecutive elements ; absolute difference between consecutive numbers ; Driver Code"
Solution in C#:

using System ; class GFG { static void pairwiseDifference ( int [ ] arr , int n ) { int diff ; for ( int i = 0 ; i < n - 1 ; i ++ ) { diff = Math . Abs ( arr [ i ] - arr [ i + 1 ] ) ; Console . WriteLine ( diff + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 10 , 15 , 5 , 6 } ; int n = arr . Length ; pairwiseDifference ( arr , n ) ; } }


"Absolute difference between floor of Array sum divided by X and floor sum of every Array element when divided by X | C # program for the above approach ; Function to find absolute difference between the two sum values ; Variable to store total sum ; Variable to store sum of A [ i ] / X ; Traverse the array ; Update totalSum ; Update perElementSum ; Floor of total sum divided by X ; Return the absolute difference ; Driver code ; Input ; Size of Array ; Function call to find absolute difference between the two sum values"
Solution in C#:

using System ; class GFG { static int floorDifference ( int [ ] A , int N , int X ) { int totalSum = 0 ; int perElementSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { totalSum += A [ i ] ; perElementSum += A [ i ] / X ; } int totalFloorSum = totalSum / X ; return Math . Abs ( totalFloorSum - perElementSum ) ; } static void Main ( ) { int [ ] A = { 1 , 2 , 3 , 4 , 5 , 6 } ; int X = 4 ; int N = A . Length ; Console . Write ( floorDifference ( A , N , X ) ) ; } }


"Absolute difference between sum and product of roots of a quartic equation | C # implementation of above approach ; Function taking coefficient of each term of equation as input ; Finding sum of roots ; Finding product of roots ; Absolute difference ; Driver Code"
Solution in C#:

using System ; class GFG { static double sumProductDifference ( int a , int b , int c , int d , int e ) { double rootSum = ( double ) ( - 1 * b ) / a ; double rootProduct = ( double ) e / a ; return Math . Abs ( rootSum - rootProduct ) ; } public static void Main ( ) { Console . Write ( sumProductDifference ( 8 , 4 , 6 , 4 , 1 ) ) ; } }


"Absolute difference between the Product of Non | C # program to find the Absolute Difference between the Product of Non - Prime numbers and Prime numbers of an Array ; Function to find the difference between the product of non - primes and the product of primes of an array . ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store the product of primes in P1 and the product of non primes in P2 ; the number is prime ; the number is non - prime ; Return the absolute difference ; Driver Code ; Find the absolute difference"
Solution in C#:

using System ; using System . Linq ; class GFG { static int calculateDifference ( int [ ] arr , int n ) { int max_val = arr . Max ( ) ; var prime = Enumerable . Repeat ( true , max_val + 1 ) . ToArray ( ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } int P1 = 1 , P2 = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { P1 *= arr [ i ] ; } else if ( arr [ i ] != 1 ) { P2 *= arr [ i ] ; } } return Math . Abs ( P2 - P1 ) ; } public static void Main ( ) { int [ ] arr = new int [ ] { 1 , 3 , 5 , 10 , 15 , 7 } ; int n = arr . Length ; Console . WriteLine ( calculateDifference ( arr , n ) ) ; } }


"Absolute difference between the XOR of Non | C # implementation of the approach ; Function to return max_element from an array ; Function to find the absolute difference between the XOR of non - primes and the XOR of primes in the given array ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store the XOR of primes in X1 and the XOR of non primes in X2 ; The number is prime ; The number is non - prime ; Return the absolute difference ; Driver Code ; Find the absolute difference"
Solution in C#:

using System ; class GFG { static int max_element ( int [ ] arr ) { int max = arr [ 0 ] ; foreach ( int ele in arr ) if ( max < ele ) max = ele ; return max ; } static int calculateDifference ( int [ ] arr , int n ) { int max_val = max_element ( arr ) ; bool [ ] prime = new bool [ max_val + 1 ] ; for ( int index = 0 ; index < max_val + 1 ; index ++ ) prime [ index ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } int x1 = 1 , x2 = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) x1 ^= arr [ i ] ; else if ( arr [ i ] != 1 ) x2 ^= arr [ i ] ; } return Math . Abs ( x1 - x2 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 5 , 10 , 15 , 7 } ; int n = arr . Length ; Console . WriteLine ( calculateDifference ( arr , n ) ) ; } }


"Absolute difference between the first X and last X Digits of N | C # implementation of the approach ; Function to find the number of digits in the integer ; Function to find the absolute difference ; Store the last x digits in last ; Count the no . of digits in N ; Remove the digits except the first x ; Store the first x digits in first ; Return the absolute difference between the first and last ; Driver code"
Solution in C#:

using System ; class GFG { static int digitsCount ( int n ) { int len = 0 ; while ( n > 0 ) { len ++ ; n /= 10 ; } return len ; } static int absoluteFirstLast ( int n , int x ) { int i = 0 , mod = 1 ; while ( i < x ) { mod *= 10 ; i ++ ; } int last = n % mod ; int len = digitsCount ( n ) ; while ( len != x ) { n /= 10 ; len -- ; } int first = n ; return Math . Abs ( first - last ) ; } public static void Main ( String [ ] args ) { int n = 21546 , x = 2 ; Console . Write ( absoluteFirstLast ( n , x ) ) ; } }


"Absolute distinct count in a Linked List | C # program to calculate the count of distinct absolute values in a linked list ; Node of the singly linked list ; Function to insert a node at the beginning of the singly Linked List ; Allocate node ; Insert the data ; Point the current Node to the current head ; Make the current node as the new head ; Function to return the count of distinct absolute values present in the Linked list ; Driver Code ; Create the head ; Insert nodes to the Linked List"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node next ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } static int distinctCount ( Node head_ref1 ) { HashSet < int > s = new HashSet < int > ( ) ; Node ptr1 = head_ref1 ; while ( ptr1 != null ) { s . Add ( Math . Abs ( ptr1 . data ) ) ; ptr1 = ptr1 . next ; } return s . Count ; } public static void Main ( String [ ] args ) { Node head1 = null ; head1 = push ( head1 , - 1 ) ; head1 = push ( head1 , - 2 ) ; head1 = push ( head1 , 0 ) ; head1 = push ( head1 , 4 ) ; head1 = push ( head1 , 5 ) ; head1 = push ( head1 , 8 ) ; int ans = distinctCount ( head1 ) ; Console . WriteLine ( ans ) ; } }


"Absolute distinct count in a sorted array | C # code to find absolute distinct count of an array in O ( n ) time . ; The function returns number of distinct absolute values among the elements of the array ; Note that set keeps only one copy even if we try to insert multiple values ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int distinctCount ( int [ ] arr , int n ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) s . Add ( Math . Abs ( arr [ i ] ) ) ; return s . Count ; } public static void Main ( ) { int [ ] arr = { - 2 , - 1 , 0 , 1 , 1 } ; int n = arr . Length ; Console . Write ( " Count ▁ of ▁ absolute ▁ distinct ▁ values ▁ : ▁ " + distinctCount ( arr , n ) ) ; } }


"Absolute distinct count in a sorted array | C # program to find absolute distinct count of an array using O ( 1 ) space . ; The function returns return number of distinct absolute values among the elements of the array ; initialize count as number of elements ; Remove duplicate elements from the left of the current window ( i , j ) and also decrease the count ; Remove duplicate elements from the right of the current window ( i , j ) and also decrease the count ; break if only one element is left ; Now look for the zero sum pair in current window ( i , j ) ; decrease the count if ( positive , negative ) pair is encountered ; Driver code"
Solution in C#:

using System ; class GFG { static int distinctCount ( int [ ] arr , int n ) { int count = n ; int i = 0 , j = n - 1 , sum = 0 ; while ( i < j ) { while ( i != j && arr [ i ] == arr [ i + 1 ] ) { count -- ; i ++ ; } while ( i != j && arr [ j ] == arr [ j - 1 ] ) { count -- ; j -- ; } if ( i == j ) break ; sum = arr [ i ] + arr [ j ] ; if ( sum == 0 ) { count -- ; i ++ ; j -- ; } else if ( sum < 0 ) i ++ ; else j -- ; } return count ; } public static void Main ( ) { int [ ] arr = { - 2 , - 1 , 0 , 1 , 1 } ; int n = arr . Length ; Console . WriteLine ( " Count ▁ of ▁ absolute ▁ distinct ▁ values ▁ : ▁ " + distinctCount ( arr , n ) ) ; } }


"Abstraction of Binary Search | C # program for the above example ; Function to find X such that it is less than the target value and function is f ( x ) = x ^ 2 ; Initialise start and end ; Loop till start <= end ; Find the mid ; Check for the left half ; Store the result ; Reinitialize the start point ; Check for the right half ; Print the maximum value of x such that x ^ 2 is less than the targetValue ; Driver Code ; Given targetValue ; ; Function Call"
Solution in C#:

 using System ; class GFG { static void findX ( int targetValue ) { int start = 0 , end = targetValue ; int mid = 0 , result = 0 ; while ( start <= end ) { mid = start + ( end - start ) / 2 ; if ( mid * mid <= targetValue ) { result = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } Console . Write ( result + "
 " ) ; } public static void Main ( String [ ] args ) { int targetValue = 81 ; findX ( targetValue ) ; } }


"Abundant Number | An Optimized Solution to check Abundant Number in C # Function to calculate sum of divisors ; Function to calculate sum of divisors ; Note that this loop runs till square root of n ; If divisors are equal , take only one of them ; else Otherwise take both ; calculate sum of all proper divisors only ; Function to check Abundant Number ; Return true if sum of divisors is greater than n . ; Driver program to test above function"
Solution in C#:

using System ; class GFG { static int getSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= ( Math . Sqrt ( n ) ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) sum = sum + i ; { sum = sum + i ; sum = sum + ( n / i ) ; } } } sum = sum - n ; return sum ; } static bool checkAbundant ( int n ) { return ( getSum ( n ) > n ) ; } public static void Main ( ) { if ( checkAbundant ( 12 ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; if ( checkAbundant ( 15 ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }


"Acronym words | C # implementation of the approach ; Function to return the number of strings that can be an acronym for other strings ; Frequency array to store the frequency of the first character of every string in the array ; To store the count of required strings ; Current word ; Frequency array to store the frequency of each of the character of the current string ; Check if the frequency of every character in the current string is <= its value in freq [ ] ; First character of the current string ; Driver code"
Solution in C#:

using System ; class GFG { static int count_acronym ( int n , string [ ] arr ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] [ 0 ] - ' a ' ] ++ ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { string st = arr [ i ] ; int [ ] num = new int [ 26 ] ; for ( int j = 0 ; j < st . Length ; j ++ ) num [ st [ j ] - ' a ' ] ++ ; bool flag = true ; for ( int j = 1 ; j < 26 ; j ++ ) { if ( num [ j ] > freq [ j ] ) { flag = false ; break ; } } int x = st [ 0 ] - ' a ' ; if ( freq [ x ] - 1 < num [ x ] ) flag = false ; if ( flag ) cnt ++ ; } return cnt ; } public static void Main ( ) { string [ ] arr = { " abc " , " bcad " , " cabd " , " cba " , " dzzz " } ; int n = arr . Length ; Console . WriteLine ( count_acronym ( n , arr ) ) ; } }


"Activity Selection Problem | Greedy Algo | The following implementation assumes that the activities are already sorted according to their finish time ; Prints a maximum set of activities that can be done by a single person , one at a time . n -- > Total number of activities s [ ] -- > An array that contains start time of all activities f [ ] -- > An array that contains finish time of all activities ; The first activity always gets selected ; Consider rest of the activities ; If this activity has start time greater than or equal to the finish time of previously selected activity , then select it ; Driver Code"
Solution in C#:

using System ; class GFG { public static void printMaxActivities ( int [ ] s , int [ ] f , int n ) { int i , j ; Console . Write ( " Following ▁ activities ▁ are ▁ selected ▁ : ▁ " ) ; i = 0 ; Console . Write ( i + " ▁ " ) ; for ( j = 1 ; j < n ; j ++ ) { if ( s [ j ] >= f [ i ] ) { Console . Write ( j + " ▁ " ) ; i = j ; } } } public static void Main ( ) { int [ ] s = { 1 , 3 , 0 , 5 , 8 , 5 } ; int [ ] f = { 2 , 4 , 6 , 7 , 9 , 9 } ; int n = s . Length ; printMaxActivities ( s , f , n ) ; } }


"Adam Number | C # Program to Check Adam Number ; To reverse Digits of numbers ; To square number ; To check Adam Number ; Square first number and square reverse digits of second number ; If reverse of b equals a then given number is Adam number ; Driver Code"
Solution in C#:

using System ; public class main { static int reverseDigits ( int num ) { int rev = 0 ; while ( num > 0 ) { rev = rev * 10 + num % 10 ; num /= 10 ; } return rev ; } static int square ( int num ) { return ( num * num ) ; } static bool checkAdamNumber ( int num ) { int a = square ( num ) ; int b = square ( reverseDigits ( num ) ) ; if ( a == reverseDigits ( b ) ) return true ; return false ; } public static void Main ( String [ ] args ) { int num = 12 ; if ( checkAdamNumber ( num ) ) Console . WriteLine ( " Adam ▁ Number " ) ; else Console . WriteLine ( " Not ▁ a ▁ Adam ▁ Number " ) ; } }


"Add 1 to a given number | C # code to Add 1 to a given number ; Driver program"
Solution in C#:

using System ; class GFG { static int addOne ( int x ) { return ( - ( ~ x ) ) ; } public static void Main ( ) { Console . WriteLine ( addOne ( 13 ) ) ; } }


"Add 1 to a given number | C # code to add one to a given number ; Flip all the set bits until we find a 0 ; flip the rightmost 0 bit ; Driver code"
Solution in C#:

using System ; class GFG { static int addOne ( int x ) { int m = 1 ; while ( ( int ) ( x & m ) == 1 ) { x = x ^ m ; m <<= 1 ; } x = x ^ m ; return x ; } public static void Main ( ) { Console . WriteLine ( addOne ( 13 ) ) ; } }


"Add 1 to a number represented as linked list | Recursive C # program to add 1 to a linked list ; Linked list node ; Function to create a new node with given data ; Recursively add 1 from end to beginning and returns carry after all nodes are processed . ; If linked list is empty , then return carry ; Add carry returned be next node call ; Update data and return new carry ; This function mainly uses addWithCarry ( ) . ; Add 1 to linked list from end to beginning ; If there is carry after processing all nodes , then we need to add a new node to linked list ; New node becomes head now ; A utility function to print a linked list ; Driver code"
Solution in C#:

using System ; class GfG { public class Node { public int data ; public Node next ; } public static Node newNode ( int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } public static int addWithCarry ( Node head ) { if ( head == null ) return 1 ; int res = head . data + addWithCarry ( head . next ) ; head . data = ( res ) % 10 ; return ( res ) / 10 ; } public static Node addOne ( Node head ) { int carry = addWithCarry ( head ) ; Node newNodes = null ; if ( carry > 0 ) { newNodes = newNode ( carry ) ; newNodes . next = head ; return newNodes ; } return head ; } public static void printList ( Node node ) { while ( node != null ) { Console . Write ( node . data ) ; node = node . next ; } Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { Node head = newNode ( 1 ) ; head . next = newNode ( 9 ) ; head . next . next = newNode ( 9 ) ; head . next . next . next = newNode ( 9 ) ; Console . Write ( " List ▁ is ▁ " ) ; printList ( head ) ; head = addOne ( head ) ; Console . WriteLine ( ) ; Console . Write ( " Resultant ▁ list ▁ is ▁ " ) ; printList ( head ) ; } }


"Add N digits to A such that it is divisible by B after each addition | C # implementation of the approach ; Try all digits from ( 0 to 9 ) ; Fails in the first move itself ; Add ( n - 1 ) 0 's ; Driver Code"
Solution in C#:

using System ; class GFG { static int addNDigits ( int a , int b , int n ) { int num = a ; for ( int i = 0 ; i <= 9 ; i ++ ) { int tmp = a * 10 + i ; if ( tmp % b == 0 ) { a = tmp ; break ; } } if ( num == a ) return - 1 ; for ( int j = 0 ; j < n - 1 ; j ++ ) a *= 10 ; return a ; } public static void Main ( ) { int a = 5 , b = 3 , n = 3 ; Console . WriteLine ( addNDigits ( a , b , n ) ) ; } }


"Add minimum number to an array so that the sum becomes even | C # program to add minimum number so that the sum of array becomes even ; Function to find out minimum number ; Count odd number of terms in array ; Driver Code"
Solution in C#:

using System ; class GFG { static int minNum ( int [ ] arr , int n ) { int odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 != 0 ) odd += 1 ; return ( ( odd % 2 ) != 0 ) ? 1 : 2 ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . Length ; Console . Write ( minNum ( arr , n ) ) ; } }


using System ; class GFG { static int minNum ( int [ ] arr , int n ) { bool odd = false ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 != 0 ) odd = ! odd ; if ( odd ) return 1 ; return 2 ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . Length ; Console . Write ( minNum ( arr , n ) ) ; } }


"Add n binary strings | C # program to add n binary strings ; This function adds two binary strings and return result as a third string ; Initialize result ; Initialize digit sum ; Traverse both strings starting from last characters ; Compute sum of last digits and carry ; If current digit sum is 1 or 3 , add 1 to result ; Compute carry ; Move to next digits ; function to add n binary strings ; Driver code"
Solution in C#:

using System ; class GFG { static String addBinaryUtil ( String a , String b ) { String result = " " ; int s = 0 ; int i = a . Length - 1 , j = b . Length - 1 ; while ( i >= 0 j >= 0 s == 1 ) { s += ( ( i >= 0 ) ? a [ i ] - '0' : 0 ) ; s += ( ( j >= 0 ) ? b [ j ] - '0' : 0 ) ; result = s % 2 + result ; s /= 2 ; i -- ; j -- ; } return result ; } static String addBinary ( String [ ] arr , int n ) { String result = " " ; for ( int i = 0 ; i < n ; i ++ ) { result = addBinaryUtil ( result , arr [ i ] ) ; } return result ; } public static void Main ( String [ ] args ) { String [ ] arr = { "1" , "10" , "11" } ; int n = arr . Length ; Console . WriteLine ( addBinary ( arr , n ) ) ; } }


"Add two integers of different base and represent sum in smaller base of the two | C # Program to find the sum of two integers of different bases . ; Initialize power of base ; Initialize result ; Decimal equivalent is str [ len - 1 ] * 1 + str [ len - 2 ] * base + str [ len - 3 ] * ( base ^ 2 ) + ... ; Maximum base - 36 String base_num = " " ; ; print ans in reverse order ; Driver Code ; Integer in base 10 ; Sum of integers ; Minimum Base ; Sum of integers in Min Base"
Solution in C#:

using System . Collections . Generic ; using System ; using System . Linq ; using System . Text ; class GFG { static int val ( char c ) { if ( c >= '0' && c <= '9' ) return ( int ) c - '0' ; else return ( int ) c - ' A ' + 10 ; } static int convert ( string num , int bases ) { int len = ( num . Length ) ; int power = 1 ; int res = 0 ; int i ; for ( i = len - 1 ; i >= 0 ; i -- ) { res += val ( num [ i ] ) * power ; power = power * bases ; } return res ; } static void dec_to_base ( int num , int bases ) { var base_num = new StringBuilder ( ) ; while ( num > 0 ) { int dig = ( num % bases ) ; if ( dig < 10 ) base_num . Append ( dig . ToString ( ) ) ; else base_num . Append ( ( ' A ' + dig - 10 ) . ToString ( ) ) ; num /= bases ; } for ( int i = base_num . Length - 1 ; i >= 0 ; i -- ) Console . Write ( base_num [ i ] ) ; } public static void Main ( String [ ] args ) { String a = "123" ; String b = "234" ; int base_a = 6 ; int base_b = 8 ; int a10 = convert ( a , base_a ) ; int b10 = convert ( b , base_b ) ; int summ = a10 + b10 ; int min_base = Math . Min ( base_a , base_b ) ; dec_to_base ( summ , min_base ) ; } }


"Add two numbers represented by Stacks | C # program to implement the above approach ; Function to return the stack that contains the sum of two numbers ; Calculate the sum of the top elements of both the stacks ; Push the sum into the stack ; Store the carry ; Pop the top elements ; If N1 is not empty ; If N2 is not empty ; If carry remains ; Reverse the stack . so that most significant digit is at the bottom of the stack ; Function to display the resultamt stack ; Driver Code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static Stack < int > PushStack ( Stack < int > N1 , Stack < int > N2 ) { Stack < int > res = new Stack < int > ( ) ; int sum = 0 , rem = 0 ; while ( N1 . Count != 0 && N2 . Count != 0 ) { sum = ( rem + N1 . Peek ( ) + N2 . Peek ( ) ) ; res . Push ( ( int ) sum % 10 ) ; rem = sum / 10 ; N1 . Pop ( ) ; N2 . Pop ( ) ; } while ( N1 . Count != 0 ) { sum = ( rem + N1 . Peek ( ) ) ; res . Push ( sum % 10 ) ; rem = sum / 10 ; N1 . Pop ( ) ; } while ( N2 . Count != 0 ) { sum = ( rem + N2 . Peek ( ) ) ; res . Push ( sum % 10 ) ; rem = sum / 10 ; N2 . Pop ( ) ; } while ( rem > 0 ) { res . Push ( rem ) ; rem /= 10 ; } while ( res . Count != 0 ) { N1 . Push ( res . Peek ( ) ) ; res . Pop ( ) ; } res = N1 ; return res ; } static void display ( Stack < int > res ) { int N = res . Count ; String s = " " ; while ( res . Count != 0 ) { s = String . Join ( " " , res . Peek ( ) ) + s ; res . Pop ( ) ; } Console . Write ( s + "
 " ) ; } public static void Main ( String [ ] args ) { Stack < int > N1 = new Stack < int > ( ) ; N1 . Push ( 5 ) ; N1 . Push ( 8 ) ; N1 . Push ( 7 ) ; N1 . Push ( 4 ) ; Stack < int > N2 = new Stack < int > ( ) ; N2 . Push ( 2 ) ; N2 . Push ( 1 ) ; N2 . Push ( 3 ) ; Stack < int > res = PushStack ( N1 , N2 ) ; display ( res ) ; } }


"Add two numbers represented by two arrays | C # program to sum two numbers represented two arrays . ; Return sum of two number represented by the arrays . Size of a [ ] is greater than b [ ] . It is made sure be the wrapper function ; array to store sum . ; Until we reach beginning of array . we are comparing only for second array because we have already compare the size of array in wrapper function . ; find sum of corresponding element of both array . ; Finding carry for next sum . ; If second array size is less the first array size . ; Add carry to first array elements . ; If there is carry on adding 0 index elements append 1 to total sum . ; Converting array into number . ; Wrapper Function ; Making first array which have greater number of element ; Driver program"
Solution in C#:

using System ; class GFG { static int calSumUtil ( int [ ] a , int [ ] b , int n , int m ) { int [ ] sum = new int [ n ] ; int i = n - 1 , j = m - 1 , k = n - 1 ; int carry = 0 , s = 0 ; while ( j >= 0 ) { s = a [ i ] + b [ j ] + carry ; sum [ k ] = ( s % 10 ) ; carry = s / 10 ; k -- ; i -- ; j -- ; } while ( i >= 0 ) { s = a [ i ] + carry ; sum [ k ] = ( s % 10 ) ; carry = s / 10 ; i -- ; k -- ; } int ans = 0 ; if ( carry == 1 ) ans = 10 ; for ( i = 0 ; i <= n - 1 ; i ++ ) { ans += sum [ i ] ; ans *= 10 ; } return ans / 10 ; } static int calSum ( int [ ] a , int [ ] b , int n , int m ) { if ( n >= m ) return calSumUtil ( a , b , n , m ) ; else return calSumUtil ( b , a , m , n ) ; } public static void Main ( ) { int [ ] a = { 9 , 3 , 9 } ; int [ ] b = { 6 , 1 } ; int n = a . Length ; int m = b . Length ; Console . WriteLine ( calSum ( a , b , n , m ) ) ; } }


"Add two numbers using ++ and / or | C # program to add two numbers using ++ ; Returns value of x + y without using + ; If y is positive , y times add 1 to x ; If y is negative , y times subtract 1 from x ; Driver code"
Solution in C#:

using System ; public class GFG { static int add ( int x , int y ) { while ( y > 0 && y != 0 ) { x ++ ; y -- ; } while ( y < 0 && y != 0 ) { x -- ; y ++ ; } return x ; } public static void Main ( ) { Console . WriteLine ( add ( 43 , 23 ) ) ; Console . WriteLine ( add ( 43 , - 23 ) ) ; } }


"Addition of two numbers without propagating Carry | C # implementation of the above approach ; Function to print sum of 2 numbers without propagating carry ; Reverse a ; Reverse b ; Generate sum Since length of both a and b are same , take any one of them . ; Extract digits from a and b and add ; If sum is single digit ; If sum is not single digit reverse sum ; Extract digits from sum and append to result ; Driver code"
Solution in C#:

using System ; class GFG { static int printSum ( int a , int b ) { int res = 0 ; int temp1 = 0 , temp2 = 0 ; while ( a != 0 ) { temp1 = temp1 * 10 + ( a % 10 ) ; a /= 10 ; } a = temp1 ; while ( b != 0 ) { temp2 = temp2 * 10 + ( b % 10 ) ; b /= 10 ; } b = temp2 ; while ( a != 0 ) { int sum = ( a % 10 + b % 10 ) ; if ( sum / 10 == 0 ) res = res * 10 + sum ; else { temp1 = 0 ; while ( sum != 0 ) { temp1 = temp1 * 10 + ( sum % 10 ) ; sum /= 10 ; } sum = temp1 ; while ( sum != 0 ) { res = res * 10 + ( sum % 10 ) ; sum /= 10 ; } } a /= 10 ; b /= 10 ; } return res ; } public static void Main ( ) { int a = 7752 , b = 8834 ; Console . Write ( printSum ( a , b ) ) ; } }


"Additive Congruence method for generating Pseudo Random Numbers | C # implementation of the above approach ; Function to generate random numbers ; Initialize the seed state ; Traverse to generate required numbers of random numbers ; Follow the additive congruential method ; Driver Code ; Seed value ; Modulus parameter ; Increment term ; Number of Random numbers to be generated ; To store random numbers ; Function call ; Print the generated random numbers"
Solution in C#:

using System ; class GFG { static void additiveCongruentialMethod ( int Xo , int m , int c , int [ ] randomNums , int noOfRandomNums ) { randomNums [ 0 ] = Xo ; for ( int i = 1 ; i < noOfRandomNums ; i ++ ) { randomNums [ i ] = ( randomNums [ i - 1 ] + c ) % m ; } } public static void Main ( String [ ] args ) { int Xo = 3 ; int m = 15 ; int c = 2 ; int noOfRandomNums = 20 ; int [ ] randomNums = new int [ noOfRandomNums ] ; additiveCongruentialMethod ( Xo , m , c , randomNums , noOfRandomNums ) ; for ( int i = 0 ; i < noOfRandomNums ; i ++ ) { Console . Write ( randomNums [ i ] + " ▁ " ) ; } } }


"Additive Prime Number | C # program for the above approach ; Check if N is prime or not ; Corner Cases ; This is checked to skip middle five numbers ; Function to get sum of digits ; Return the sum of digits ; Function to check whether the given number is Additive Prime number or not ; If number is not prime ; Check if sum of digits is prime or not ; Driver code ; Given Number N ; Function Call"
Solution in C#:

using System ; class GFG { static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static int getSum ( int n ) { int sum = 0 ; while ( n != 0 ) { sum = sum + n % 10 ; n = n / 10 ; } return sum ; } static bool isAdditivePrime ( int n ) { if ( ! isPrime ( n ) ) return false ; return isPrime ( getSum ( n ) ) ; } public static void Main ( ) { int n = 23 ; if ( isAdditivePrime ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Admirable Numbers | C # implementation to check if N is a admirable number ; Function to calculate the sum of all divisors of a given number ; Sum of divisors ; Find all divisors which divides ' num ' ; If ' i ' is divisor of ' n ' ; If both divisors are same then add it once else add ; Add 1 and n to result as above loop considers proper divisors greater ; Function to check if there exists a proper divisor D ' ▁ of ▁ N ▁ such ▁ that ▁ sigma ( n ) -2D ' = 2 N ; Find all divisors which divides ' num ' ; If ' i ' is divisor of ' num ' ; If both divisors are same then add it only once else add both ; Check 1 since 1 is also a divisor ; Function to check if N is an admirable number ; Driver code"
Solution in C#:

using System ; class GFG { static int divSum ( int n ) { int result = 0 ; for ( int i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == ( n / i ) ) result += i ; else result += ( i + n / i ) ; } } return ( result + n + 1 ) ; } static bool check ( int num ) { int sigmaN = divSum ( num ) ; for ( int i = 2 ; i <= Math . Sqrt ( num ) ; i ++ ) { if ( num % i == 0 ) { if ( i == ( num / i ) ) { if ( sigmaN - 2 * i == 2 * num ) return true ; } else { if ( sigmaN - 2 * i == 2 * num ) return true ; if ( sigmaN - 2 * ( num / i ) == 2 * num ) return true ; } } } if ( sigmaN - 2 * 1 == 2 * num ) return true ; return false ; } static bool isAdmirableNum ( int N ) { return check ( N ) ; } public static void Main ( ) { int n = 12 ; if ( isAdmirableNum ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Alcuin 's Sequence | C # program for Alcuin 's Sequence ; find the nth term of Alcuin 's sequence ; return the ans ; print first n terms of Alcuin number ; display the number ; Driver code"
Solution in C#:

using System ; class GFG { static int Alcuin ( int n ) { double _n = n , ans ; ans = Math . Round ( ( _n * _n ) / 12 ) - Math . Floor ( _n / 4 ) * Math . Floor ( ( _n + 2 ) / 4 ) ; return ( int ) ans ; } static void solve ( int n ) { int i = 0 ; for ( i = 1 ; i <= n ; i ++ ) { Console . Write ( Alcuin ( i ) + " , ▁ " ) ; } } public static void Main ( String [ ] args ) { int n = 15 ; solve ( n ) ; } }


"Aliquot Sequence | C # implementation of Optimized approach to generate Aliquot Sequence ; Function to calculate sum of all proper divisors ; Note that this loop runs till square root of n ; If divisors are equal , take only one of them ; else Otherwise take both ; calculate sum of all proper divisors only ; Function to print Aliquot Sequence for an input n . ; Print the first term ; Calculate next term from previous term ; Print next term ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int getSum ( int n ) { for ( int i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { sum = sum + i ; } { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum - n ; } static void printAliquot ( int n ) { Console . Write ( n + " ▁ " ) ; HashSet < int > s = new HashSet < int > ( ) ; s . Add ( n ) ; while ( n > 0 ) { n = getSum ( n ) ; if ( s . Contains ( n ) ) { Console . Write ( " Repeats with " break ; } Console . Write ( n + " ▁ " ) ; s . Add ( n ) ; } } public static void Main ( String [ ] args ) { printAliquot ( 12 ) ; } }


"Aliquot sum | C # program for aliquot sum ; Function to calculate sum of all proper divisors ; Driver Code"
Solution in C#:

using System ; class GFG { static int aliquotSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i < n ; i ++ ) if ( n % i == 0 ) sum += i ; return sum ; } public static void Main ( ) { int n = 12 ; Console . WriteLine ( aliquotSum ( n ) ) ; } }


"All pairs whose xor gives unique prime | C # implementation of the approach ; Function that returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to return the count of valid pairs ; If xor ( a [ i ] , a [ j ] ) is prime and unique ; Driver code"
Solution in C#:

using System ; using System . Collections ; class solution { static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static int countPairs ( int [ ] a , int n ) { int count = 0 ; Hashtable m = new Hashtable ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( isPrime ( a [ i ] ^ a [ j ] ) && m [ a [ i ] ^ a [ j ] ] == null ) { m . Add ( ( a [ i ] ^ a [ j ] ) , 1 ) ; count ++ ; } } } return count ; } public static void Main ( ) { int [ ] a = { 10 , 12 , 23 , 45 , 5 , 6 } ; int n = a . Length ; Console . WriteLine ( countPairs ( a , n ) ) ; } }


"All possible co | C # implementation of the approach ; Function to count possible pairs ; total count of numbers in range ; printing count of pairs ; Driver code"
Solution in C#:

 using System ; class GFG { static void CountPair ( int L , int R ) { int x = ( R - L + 1 ) ; Console . WriteLine ( x / 2 + "
 " ) ; } public static void Main ( ) { int L , R ; L = 1 ; R = 8 ; CountPair ( L , R ) ; } }


"All possible numbers of N digits and base B without leading zeros | C # implementation of the approach ; function to count all permutations ; count of all permutations ; count of permutations with leading zeros ; Return the permutations without leading zeros ; Driver code"
Solution in C#:

using System ; class GFG { static void countPermutations ( int N , int B ) { int x = ( int ) Math . Pow ( B , N ) ; int y = ( int ) Math . Pow ( B , N - 1 ) ; Console . WriteLine ( x - y ) ; } public static void Main ( ) { int N = 6 ; int B = 4 ; countPermutations ( N , B ) ; } }


"All possible values of floor ( N / K ) for all values of K | C # program for the above approach ; Function to print all possible values of Math . floor ( N / K ) ; Loop from 1 to N + 1 ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void allQuotients ( int N ) { SortedSet < int > s = new SortedSet < int > ( ) ; for ( int k = 1 ; k <= N + 1 ; k ++ ) { s . Add ( N / k ) ; } foreach ( int it in s ) { Console . Write ( it + " ▁ " ) ; } } static void Main ( ) { int N = 5 ; allQuotients ( N ) ; } }


"All unique combinations whose sum equals to K | C # implementation of the approach ; Function to find all unique combination of given elements such that their sum is K ; If a unique combination is found ; For all other combinations ; Check if the sum exceeds K ; Check if it is repeated or not ; Take the element into the combination ; Recursive call ; Remove element from the combination ; Function to find all combination of the given elements ; Sort the given elements ; To store combination ; Driver code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void unique_combination ( int l , int sum , int K , List < int > local , List < int > A ) { if ( sum == K ) { Console . Write ( " { " ) ; for ( int i = 0 ; i < local . Count ; i ++ ) { if ( i != 0 ) Console . Write ( " ▁ " ) ; Console . Write ( local [ i ] ) ; if ( i != local . Count - 1 ) Console . Write ( " , ▁ " ) ; } Console . WriteLine ( " } " ) ; return ; } for ( int i = l ; i < A . Count ; i ++ ) { if ( sum + A [ i ] > K ) continue ; if ( i > l && A [ i ] == A [ i - 1 ] ) continue ; local . Add ( A [ i ] ) ; unique_combination ( i + 1 , sum + A [ i ] , K , local , A ) ; local . RemoveAt ( local . Count - 1 ) ; } } static void Combination ( List < int > A , int K ) { A . Sort ( ) ; List < int > local = new List < int > ( ) ; unique_combination ( 0 , 0 , K , local , A ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 1 , 2 , 7 , 6 , 1 , 5 } ; List < int > A = new List < int > ( arr ) ; int K = 8 ; Combination ( A , K ) ; } }


"All unique triplets that sum up to a given value | C # program to find all unique triplets without using any extra space . ; Function to all find unique triplets without using extra space ; Sort the input array ; For handling the cases when no such triplets exits . ; Iterate over the array from start to n - 2. ; Index of the first element in remaining range . ; Index of the last element ; Setting our new target ; Checking if current element is same as previous ; Checking if current element is same as previous ; If we found the triplets then print it and set the flag ; If target is greater then increment the start index ; If target is smaller than decrement the end index ; If no such triplets found ; Driver code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findTriplets ( int [ ] a , int n , int sum ) { int i ; Array . Sort ( a ) ; bool flag = false ; for ( i = 0 ; i < n - 2 ; i ++ ) { if ( i == 0 a [ i ] > a [ i - 1 ] ) { int start = i + 1 ; int end = n - 1 ; int target = sum - a [ i ] ; while ( start < end ) { if ( start > i + 1 && a [ start ] == a [ start - 1 ] ) { start ++ ; continue ; } if ( end < n - 1 && a [ end ] == a [ end + 1 ] ) { end -- ; continue ; } if ( target == a [ start ] + a [ end ] ) { Console . Write ( " [ " + a [ i ] + " , " + a [ start ] + " , " + a [ end ] + " ] ▁ " ) ; flag = true ; start ++ ; end -- ; } else if ( target > ( a [ start ] + a [ end ] ) ) { start ++ ; } else { end -- ; } } } } if ( flag == false ) { Console . WriteLine ( " No ▁ Such ▁ Triplets ▁ Exist " ) ; } } static void Main ( ) { int [ ] a = { 12 , 3 , 6 , 1 , 6 , 9 } ; int n = a . Length ; int sum = 24 ; findTriplets ( a , n , sum ) ; } }


"Allocate minimum number of pages | C # program for optimal allocation of pages ; Utility function to check if current minimum value is feasible or not . ; iterate over all books ; check if current number of pages are greater than curr_min that means we will get the result after mid no . of pages ; count how many students are required to distribute curr_min pages ; increment student count ; update curr_sum ; if students required becomes greater than given no . of students , return false ; else update curr_sum ; function to find minimum pages ; return - 1 if no . of books is less than no . of students ; Count total number of pages ; initialize start as 0 pages and end as total pages ; traverse until start <= end ; check if it is possible to distribute books by using mid as current minimum ; update result to current distribution as it 's the best we have found till now. ; as we are finding minimum and books are sorted so reduce end = mid - 1 that means ; if not possible means pages should be increased so update start = mid + 1 ; at - last return minimum no . of pages ; Drivers code ; Number of pages in books ; int m = 2 ; No . of students"
Solution in C#:

using System ; class GFG { static bool isPossible ( int [ ] arr , int n , int m , int curr_min ) { int studentsRequired = 1 ; int curr_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > curr_min ) return false ; if ( curr_sum + arr [ i ] > curr_min ) { studentsRequired ++ ; curr_sum = arr [ i ] ; if ( studentsRequired > m ) return false ; } else curr_sum += arr [ i ] ; } return true ; } static int findPages ( int [ ] arr , int n , int m ) { long sum = 0 ; if ( n < m ) return - 1 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; int start = 0 , end = ( int ) sum ; int result = int . MaxValue ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( isPossible ( arr , n , m , mid ) ) { result = mid ; end = mid - 1 ; } else start = mid + 1 ; } return result ; } static public void Main ( ) { int [ ] arr = { 12 , 34 , 67 , 90 } ; int n = arr . Length ; Console . WriteLine ( " Minimum ▁ number ▁ of ▁ pages ▁ = ▁ " + findPages ( arr , n , m ) ) ; } }


"Almost Prime Numbers | C # Program to print first n numbers that are k - primes ; A function to count all prime factors of a given number ; Count the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , count i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; A function to print the first n numbers that are k - almost primes . ; Print this number if it is k - prime ; Increment count of k - primes printed so far ; Driver code"
Solution in C#:

using System ; class GFG { static int countPrimeFactors ( int n ) { int count = 0 ; while ( n % 2 == 0 ) { n = n / 2 ; count ++ ; } for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { n = n / i ; count ++ ; } } if ( n > 2 ) count ++ ; return ( count ) ; } static void printKAlmostPrimes ( int k , int n ) { for ( int i = 1 , num = 2 ; i <= n ; num ++ ) { if ( countPrimeFactors ( num ) == k ) { Console . Write ( num + " ▁ " ) ; i ++ ; } } return ; } public static void Main ( ) { int n = 10 , k = 2 ; Console . WriteLine ( " First ▁ " + n + " ▁ " + k + " - almost ▁ prime ▁ numbers ▁ : ▁ " ) ; printKAlmostPrimes ( k , n ) ; } }


"Alternate Fibonacci Numbers | Alternate Fibonacci Series using Dynamic Programming ; 0 th and 1 st number of the series are 0 and 1 ; Driver Code"
Solution in C#:

using System ; class GFG { static void alternateFib ( int n ) { if ( n < 0 ) return ; int f1 = 0 ; int f2 = 1 ; Console . Write ( f1 + " ▁ " ) ; for ( int i = 2 ; i <= n ; i ++ ) { int f3 = f2 + f1 ; if ( i % 2 == 0 ) Console . Write ( f3 + " ▁ " ) ; f1 = f2 ; f2 = f3 ; } } public static void Main ( ) { int N = 15 ; alternateFib ( N ) ; } }


"Alternate Lower Upper String Sort | C # program for unusual string sorting ; Function for alternate sorting of string ; Count occurrences of individual lower case and upper case characters ; Traverse through count arrays and one by one pick characters . Below loop takes O ( n ) time considering the MAX is constant . ; Driver function"
Solution in C#:

using System ; public class GFG { private static int MAX = 100 ; static String alternateSort ( String s1 ) { int n = s1 . Length ; int l = 0 , j = 0 , k = 0 ; char [ ] s = s1 . ToCharArray ( ) ; int [ ] lCount = new int [ MAX ] ; int [ ] uCount = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( char . IsUpper ( s [ i ] ) ) uCount [ s [ i ] - ' A ' ] ++ ; else lCount [ s [ i ] - ' a ' ] ++ ; } while ( k < n ) { while ( l < MAX && uCount [ l ] == 0 ) l ++ ; if ( l < MAX ) { s [ k ++ ] = ( char ) ( ' A ' + l ) ; uCount [ l ] -- ; } while ( j < MAX && lCount [ j ] == 0 ) j ++ ; if ( j < MAX ) { s [ k ++ ] = ( char ) ( ' a ' + j ) ; lCount [ j ] -- ; } } return ( new String ( s ) ) ; } public static void Main ( ) { String str = " bAwutndekWEdkd " ; Console . Write ( alternateSort ( str ) ) ; } }


"Alternate Primes till N | C # program to print all primes smaller than or equal to n using Naive approach . ; Function for checking number is prime or not ; if flag = 0 then number is prime and return 1 otherwise return 0 ; Function for printing alternate prime number ; counter is initialize with 0 ; looping through 2 to n - 1 ; function calling along with if condition ; if counter is multiple of 2 then only print prime number ; Driver code ; Function calling"
Solution in C#:

 using System ; class GFG { static int prime ( int num ) { int i , flag = 0 ; for ( i = 2 ; i <= num / 2 ; i ++ ) { if ( num % i == 0 ) { flag = 1 ; break ; } } if ( flag == 0 ) return 1 ; else return 0 ; } static void print_alternate_prime ( int n ) { int counter = 0 ; for ( int num = 2 ; num < n ; num ++ ) { if ( prime ( num ) == 1 ) { if ( counter % 2 == 0 ) Console . Write ( num + " ▁ " ) ; counter ++ ; } } } public static void Main ( ) { int n = 15 ; Console . Write ( " Following ▁ are ▁ the ▁ alternate ▁ " + " prime ▁ number ▁ smaller ▁ than ▁ " + " or ▁ equal ▁ to ▁ " + n + "
 " ) ; print_alternate_prime ( n ) ; } }


"Alternate Primes till N | C # program to print all primes smaller than or equal to n using Sieve of Eratosthenes ; Create a bool array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Print all prime numbers ; for next prime to get printed ; Driver Code"
Solution in C#:

 using System ; class GFG { static void SieveOfEratosthenes ( int n ) { bool [ ] prime = new bool [ n + 1 ] ; for ( int i = 0 ; i < prime . Length ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } bool flag = true ; for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) { if ( flag ) { Console . Write ( p + " ▁ " ) ; flag = false ; } else { flag = true ; } } } } public static void Main ( ) { int n = 15 ; Console . Write ( " Following ▁ are ▁ the ▁ alternate " + " ▁ prime ▁ numbers ▁ smaller ▁ " + " than ▁ or ▁ equal ▁ to ▁ " + n + "
 " ) ; SieveOfEratosthenes ( n ) ; } }


"Alternate XOR operations on sorted array | C # implementation of the approach ; Function to find the maximum and the minimum elements from the array after performing the given operation k times ; To store the current sequence of elements ; To store the next sequence of elements after xoring with current elements ; Store the frequency of elements of arr [ ] in arr1 [ ] ; Storing all precomputed XOR values so that we don 't have to do it again and again  as XOR is a costly operation ; Perform the operations k times ; The value of count decides on how many elements we have to apply XOR operation ; If current element is present in the array to be modified ; Suppose i = m and arr1 [ i ] = num , it means ' m ' appears ' num ' times If the count is even we have to perform XOR operation on alternate ' m ' starting from the 0 th index because count is even and we have to perform XOR operations starting with initial ' m ' Hence there will be ceil ( num / 2 ) operations on ' m ' that will change ' m ' to xor_val [ m ] i . e . m ^ x ; Decrease the frequency of ' m ' from arr1 [ ] ; Increase the frequency of ' m ^ x ' in arr2 [ ] ; If the count is odd we have to perform XOR operation on alternate ' m ' starting from the 1 st index because count is odd and we have to leave the 0 th ' m ' Hence there will be ( num / 2 ) XOR operations on ' m ' that will change ' m ' to xor_val [ m ] i . e . m ^ x ; Updating the count by frequency of the current elements as we have processed that many elements ; Updating arr1 [ ] which will now store the next sequence of elements At this time , arr1 [ ] stores the remaining ' m ' on which XOR was not performed and arr2 [ ] stores the frequency of ' m ^ x ' i . e . those ' m ' on which operation was performed Updating arr1 [ ] with frequency of remaining ' m ' & frequency of ' m ^ x ' from arr2 [ ] With help of arr2 [ ] , we prevent sorting of the array again and again ; Resetting arr2 [ ] for next iteration ; Finding the maximum and the minimum element from the modified array after the..."
Solution in C#:

using System ; class GFG { static int MAX = 100000 ; public static void xorOnSortedArray ( int [ ] arr , int n , int k , int x ) { int [ ] arr1 = new int [ MAX + 1 ] ; int [ ] arr2 = new int [ MAX + 1 ] ; int [ ] xor_val = new int [ MAX + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) arr1 [ arr [ i ] ] ++ ; for ( int i = 0 ; i <= MAX ; i ++ ) xor_val [ i ] = i ^ x ; while ( k -- > 0 ) { int count = 0 ; for ( int i = 0 ; i <= MAX ; i ++ ) { int store = arr1 [ i ] ; if ( arr1 [ i ] > 0 ) { if ( count % 2 == 0 ) { int div = ( int ) Math . Ceiling ( ( double ) ( arr1 [ i ] / 2 ) ) ; arr1 [ i ] = arr1 [ i ] - div ; arr2 [ xor_val [ i ] ] += div ; } else if ( count % 2 != 0 ) { int div = arr1 [ i ] / 2 ; arr1 [ i ] = arr1 [ i ] - div ; arr2 [ xor_val [ i ] ] += div ; } } count = count + store ; } for ( int i = 0 ; i <= MAX ; i ++ ) { arr1 [ i ] = arr1 [ i ] + arr2 [ i ] ; arr2 [ i ] = 0 ; } } int min = int . MaxValue ; int max = int . MinValue ; for ( int i = 0 ; i <= MAX ; i ++ ) { if ( arr1 [ i ] > 0 ) { if ( min > i ) min = i ; if ( max < i ) max = i ; } } Console . WriteLine ( min + " ▁ " + max ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 605 , 986 } ; int n = arr . Length ; int k = 548 , x = 569 ; xorOnSortedArray ( arr , n , k , x ) ; } }


"Alternate bits of two numbers to create a new number | C # Program to generate a number using alternate bits of two numbers . ; set even bit of number n ; res for store 101010. . number ; generate number form of 101010. . ... till temp size ; if bit is even then generate number and or with res ; return set even bit number ; set odd bit of number m ; res for store 101010. . number ; generate number form of 101010. . . . till temp size ; if bit is even then generate number and or with res ; return set odd bit number ; set even bit of number n ; set odd bit of number m ; take OR with these number ; Driver code ; n = 1 0 1 0 ^ ^ m = 1 0 1 1 ^ ^ result = 1 0 1 1"
Solution in C#:

using System ; class GFG { static int setevenbits ( int n ) { int temp = n ; int count = 0 ; int res = 0 ; for ( temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 1 ) res |= ( 1 << count ) ; count ++ ; } return ( n & res ) ; } static int setoddbits ( int m ) { int count = 0 ; int res = 0 ; for ( int temp = m ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 0 ) res |= ( 1 << count ) ; count ++ ; } return ( m & res ) ; } static int getAlternateBits ( int n , int m ) { int tempn = setevenbits ( n ) ; int tempm = setoddbits ( m ) ; return ( tempn tempm ) ; } public static void Main ( ) { int n = 10 ; int m = 11 ; Console . WriteLine ( getAlternateBits ( n , m ) ) ; } }


"Alternate vowel and consonant string | C # implementation of alternate vowel and consonant string ; ' ch ' is vowel or not ; create alternate vowel and consonant string str1 [ 0. . . l1 - 1 ] and str2 [ start ... l2 - 1 ] ; first adding character of vowel / consonant then adding character of consonant / vowel ; function to find the required alternate vowel and consonant string ; count vowels and update vowel string ; count consonants and update consonant string ; no such string can be formed ; remove first character of vowel string then create alternate string with cstr [ 0. . . nc - 1 ] and vstr [ 1. . . nv - 1 ] ; remove first character of consonant string then create alternate string with vstr [ 0. . . nv - 1 ] and cstr [ 1. . . nc - 1 ] ; if both vowel and consonant strings are of equal length start creating string with consonant ; start creating string with vowel ; Driver code"
Solution in C#:

using System ; class GFG { static Boolean isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) return true ; return false ; } static String createAltStr ( String str1 , String str2 , int start , int l ) { String finalStr = " " ; for ( int i = 0 , j = start ; j < l ; i ++ , j ++ ) finalStr = ( finalStr + str1 [ i ] ) + str2 [ j ] ; return finalStr ; } static String findAltStr ( String str ) { int nv = 0 , nc = 0 ; String vstr = " " , cstr = " " ; int l = str . Length ; for ( int i = 0 ; i < l ; i ++ ) { char ch = str [ i ] ; if ( isVowel ( ch ) ) { nv ++ ; vstr = vstr + ch ; } else { nc ++ ; cstr = cstr + ch ; } } if ( Math . Abs ( nv - nc ) >= 2 ) return " no ▁ such ▁ string " ; if ( nv > nc ) return ( vstr [ 0 ] + createAltStr ( cstr , vstr , 1 , nv ) ) ; if ( nc > nv ) return ( cstr [ 0 ] + createAltStr ( vstr , cstr , 1 , nc ) ) ; if ( cstr [ 0 ] < vstr [ 0 ] ) return createAltStr ( cstr , vstr , 0 , nv ) ; return createAltStr ( vstr , cstr , 0 , nc ) ; } public static void Main ( String [ ] args ) { String str = " geeks " ; Console . WriteLine ( findAltStr ( str ) ) ; } }


"Alternating Numbers | C # program for the above approach ; Function to check if a string is of the form even odd even odd ... ; Function to check if a string is of the form odd even odd even ... ; Function to check if n is an alternating number ; Driver Code ; Given number N ; Function call"
Solution in C#:

using System ; class GFG { static bool isEvenOddForm ( String s ) { int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 && s [ i ] % 2 != 0 ) return false ; if ( i % 2 == 1 && s [ i ] % 2 != 1 ) return false ; } return true ; } static bool isOddEvenForm ( String s ) { int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 && s [ i ] % 2 != 1 ) return false ; if ( i % 2 == 1 && s [ i ] % 2 != 0 ) return false ; } return true ; } static bool isAlternating ( int n ) { String str = n . ToString ( ) ; return ( isEvenOddForm ( str ) || isOddEvenForm ( str ) ) ; } public static void Main ( String [ ] args ) { int N = 129 ; if ( isAlternating ( N ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Alternating split of a given Singly Linked List | Set 1 |  ; points to the last node in ' a ' ; points to the last node in ' b ' ; add at ' a ' tail ; advance the ' a ' tail"
Solution in C#:

static void AlternatingSplit ( Node source , Node aRef , Node bRef ) { Node aDummy = new Node ( ) ; Node aTail = aDummy ; Node bDummy = new Node ( ) ; Node bTail = bDummy ; Node current = source ; aDummy . next = null ; bDummy . next = null ; while ( current != null ) { MoveNode ( ( aTail . next ) , current ) ; aTail = aTail . next ; if ( current != null ) { MoveNode ( ( bTail . next ) , current ) ; bTail = bTail . next ; } } aRef = aDummy . next ; bRef = bDummy . next ; }


"Alternatively Merge two Strings in Java | C # code to alternatively merge two strings ; Function for alternatively merging two strings ; To store the final string ; For every index in the strings ; First choose the ith character of the first string if it exists ; Then choose the ith character of the second string if it exists ; Driver code"
Solution in C#:

using System ; class GFG { static string merge ( string s1 , string s2 ) { string result = " " ; for ( int i = 0 ; i < s1 . Length < s2 . Length ; i ++ ) { if ( i < s1 . Length ) result += s1 [ i ] ; if ( i < s2 . Length ) result += s2 [ i ] ; } return result ; } static void Main ( ) { string s1 = " geeks " ; string s2 = " forgeeks " ; Console . WriteLine ( merge ( s1 , s2 ) ) ; } }


"Altitude of largest Triangle that can be inscribed in a Rectangle | C # program for the above approach ; Function to find the greatest altitude of the largest triangle triangle that can be inscribed in the rectangle ; If L is greater than B ; Stores the maximum altitude value ; Return res ; Driver Code ; Given Input ; Function call"
Solution in C#:

using System ; class GFG { static int largestAltitude ( int L , int B ) { if ( L > B ) { int t = L ; L = B ; B = t ; } int res = Math . Min ( B / 2 , L ) ; return res ; } public static void Main ( String [ ] args ) { int L = 3 ; int B = 4 ; Console . Write ( largestAltitude ( L , B ) ) ; } }


"Altitude of largest Triangle that can be inscribed in a Rectangle | C # program for the above approach ; Function to find the greatest altitude of the largest triangle triangle that can be inscribed in the rectangle ; If L is greater than B ; Variables to perform binary search ; Stores the maximum altitude possible ; Iterate until low is less than high ; Stores the mid value ; If mide is less than or equal to the B / 2 ; Update res ; Update low ; Update high ; Print the result ; Driver Code ; Given Input ; Function call"
Solution in C#:

using System ; class GFG { static int largestAltitude ( int L , int B ) { if ( L > B ) { int t = L ; L = B ; B = t ; } int low = 0 , high = L ; int res = 0 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( mid <= ( B / 2 ) ) { res = mid ; low = mid + 1 ; } else high = mid - 1 ; } return res ; } public static void Main ( string [ ] args ) { int L = 3 ; int B = 4 ; Console . Write ( largestAltitude ( L , B ) ) ; } }


"An application on Bertrand 's ballot theorem | C # implementation of the approach ; Function to calculate factorial of a number mod 1000000007 ; Factorial of i = factorial of ( i - 1 ) * i ; ; Taking mod along with calculation . ; Function for modular exponentiation ; If p is odd ; If p is even ; Function to return the count of required permutations ; Calculating multiplicative modular inverse for x ! and multiplying with ans ; Calculating multiplicative modular inverse for y ! and multiplying with ans ; Driver code ; Pre - compute factorials"
Solution in C#:

class GFG { static long mod = 1000000007 ; static long [ ] arr = new long [ 1000001 ] ; static void cal_factorial ( ) { arr [ 0 ] = 1 ; for ( long i = 1 ; i <= 1000000 ; i ++ ) { arr [ i ] = ( arr [ i - 1 ] * i ) % mod ; } } static long mod_exponent ( long num , long p ) { if ( p == 0 ) return 1 ; if ( ( p & 1 ) != 0 ) { return ( ( num % mod ) * ( mod_exponent ( ( num * num ) % mod , p / 2 ) ) % mod ) % mod ; } else return ( mod_exponent ( ( num * num ) % mod , p / 2 ) ) % mod ; } static long getCount ( long x , long y ) { long ans = arr [ x + y - 1 ] ; ans *= mod_exponent ( arr [ x ] , mod - 2 ) ; ans %= mod ; ans *= mod_exponent ( arr [ y ] , mod - 2 ) ; ans %= mod ; ans *= ( x - y ) ; ans %= mod ; return ans ; } static void Main ( ) { cal_factorial ( ) ; long x = 3 , y = 1 ; System . Console . WriteLine ( getCount ( x , y ) ) ; } }