"Divide 1 to n into two groups with minimum sum difference | C # program to divide n integers in two groups such that absolute difference of their sum is minimum ; To print vector along size ; Print vector size ; Print vector elements ; To divide n in two groups such that absolute difference of their sum is minimum ; Find sum of all elements upto n ; Sum of elements of group1 ; If sum is greater then or equal to 0 include i in group1 otherwise include in group2 ; Decrease sum of group1 ; Print both the groups ; Driver code"
Solution in C#:

using System ; using System . Collections ; class GFG { static void printVector ( ArrayList v ) { Console . WriteLine ( v . Count ) ; for ( int i = 0 ; i < v . Count ; i ++ ) Console . Write ( v [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } static void findTwoGroup ( int n ) { int sum = n * ( n + 1 ) / 2 ; int group1Sum = sum / 2 ; ArrayList group1 = new ArrayList ( ) ; ArrayList group2 = new ArrayList ( ) ; for ( int i = n ; i > 0 ; i -- ) { if ( group1Sum - i >= 0 ) { group1 . Add ( i ) ; group1Sum -= i ; } else { group2 . Add ( i ) ; } } printVector ( group1 ) ; printVector ( group2 ) ; } public static void Main ( ) { int n = 5 ; findTwoGroup ( n ) ; } }


"Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups | C # program for the above approach ; Function to fill the matrix with the given conditions ; Count of parts with size sizeOfPart ; Assigning the cell with no of groups ; Update row ; Update col ; Increment count ; For new group increment start ; Function to return the reference of the matrix to be filled ; Create matrix of size N * M ; Starting index of the matrix ; Size of one group ; Element to assigned to matrix ; Fill the matrix that have rem no of parts with size size + 1 ; Fill the remaining number of parts with each part size is ' size ' ; Function to print the matrix ; Traverse the rows ; Traverse the columns ; Driver Code ; Given N , M , K ; Function Call ; Function Call to print matrix"
Solution in C#:

using System ; class GFG { static int N , M ; static int [ , ] mat ; static int row , col , start , flag ; static void fillMatrix ( int sizeOfpart , int noOfPart , int m ) { for ( int i = 0 ; i < noOfPart ; ++ i ) { int count = 0 ; while ( count < sizeOfpart ) { mat [ row , col ] = start ; if ( col == m - 1 && flag == 1 ) { row ++ ; col = m ; flag = 0 ; } else if ( col == 0 && flag == 0 ) { row ++ ; col = - 1 ; flag = 1 ; } if ( flag == 1 ) { col ++ ; } else { col -- ; } count ++ ; } start ++ ; } } static void findMatrix ( int k ) { mat = new int [ M , N ] ; row = 0 ; col = 0 ; int size = ( N * M ) / k ; int rem = ( N * M ) % k ; start = 1 ; flag = 1 ; fillMatrix ( size + 1 , rem , M ) ; fillMatrix ( size , k - rem , M ) ; } static void printMatrix ( ) { for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < M ; ++ j ) { Console . Write ( mat [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { N = 5 ; M = 5 ; int K = 6 ; findMatrix ( K ) ; printMatrix ( ) ; } }


"Divide N into K parts in the form ( X , 2 X , ... , KX ) for some value of X | Java program for the above approach ; Function to find the division ; Calculating value of x1 ; Print - 1 if division is not possible ; Get the first number ie x1 then successively multiply it by x1 k times by index number to get the required answer ; Driver Code ; Given N and K ; Function Call"
Solution in C#:

import java . . * ; class GFG { static void solve ( int n , int k ) { int x1 , d ; d = k * ( k + 1 ) ; if ( ( 2 * n ) % d != 0 ) { System . out . print ( " - 1" ) ; return ; } x1 = 2 * n / d ; for ( int i = 1 ; i <= k ; i ++ ) { System . out . print ( x1 * i + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int n = 10 , k = 4 ; solve ( n , k ) ; } }


"Divide N into K unique parts such that gcd of those parts is maximum | C # implementation of the approach ; Function to calculate maximum GCD ; Minimum possible sum for K unique positive integers ; It is not possible to divide N into K unique parts ; All the factors greater than sqrt ( N ) are complementary of the factors less than sqrt ( N ) ; If i is a factor of N ; Driver code"
Solution in C#:

using System ; class GFG { static int maxGCD ( int N , int K ) { int minSum = ( K * ( K + 1 ) ) / 2 ; if ( N < minSum ) return - 1 ; int i = ( int ) Math . Sqrt ( N ) ; int res = 1 ; while ( i >= 1 ) { if ( N % i == 0 ) { if ( i >= minSum ) res = Math . Max ( res , N / i ) ; if ( N / i >= minSum ) res = Math . Max ( res , i ) ; } i -- ; } return res ; } public static void Main ( ) { int N = 18 , K = 3 ; Console . WriteLine ( maxGCD ( N , K ) ) ; } }


"Divide a number into two parts | C # implementation of the approach ; Function to print the two parts ; Find the position of 4 ; If current character is not '4' but appears after the first occurrence of '4' ; Print both the parts ; Driver code"
Solution in C#:

using System ; class GfG { static void twoParts ( string str ) { int flag = 0 ; string a = " " ; char [ ] gfg = str . ToCharArray ( ) ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( gfg [ i ] == '4' ) { gfg [ i ] = '3' ; a += '1' ; flag = 1 ; } else if ( flag != 0 ) a += '0' ; } str = new String ( gfg ) ; Console . WriteLine ( str + " ▁ " + a ) ; } static void Main ( ) { string str = "9441" ; twoParts ( str ) ; } }


"Divide a sorted array in K parts with sum of difference of max and min minimized in each part | C # program to find the minimum sum of differences possible for the given array when the array is divided into K subarrays ; Function to find the minimum sum of differences possible for the given array when the array is divided into K subarrays ; Array to store the differences between two adjacent elements ; Iterating through the array ; Storing differences to p ; Sorting p in descending order ; Sum of the first k - 1 values of p ; Computing the result ; Driver code"
Solution in C#:

using System ; class GFG { static int calculate_minimum_split ( int n , int [ ] a , int k ) { int [ ] p = new int [ n - 1 ] ; for ( int i = 1 ; i < n ; i ++ ) p [ i - 1 ] = a [ i ] - a [ i - 1 ] ; Array . Sort ( p ) ; Array . Reverse ( p ) ; int min_sum = 0 ; for ( int i = 0 ; i < k - 1 ; i ++ ) min_sum += p [ i ] ; int res = a [ n - 1 ] - a [ 0 ] - min_sum ; return res ; } static void Main ( ) { int [ ] arr = { 4 , 8 , 15 , 16 , 23 , 42 } ; int k = 3 ; int n = arr . Length ; Console . Write ( calculate_minimum_split ( n , arr , k ) ) ; } }


"Divide a string in N equal parts | C # program to divide a string in n equal parts ; Method to print n equal parts of str ; Check if string can be divided in n equal parts ; Calculate the size of parts to find the division points ; Driver Code ; length od string is 28 ; Print 4 equal parts of the string"
Solution in C#:

using System ; class GFG { static void divideString ( String str , int n ) { int str_size = str . Length ; int part_size ; if ( str_size % n != 0 ) { Console . Write ( " Invalid ▁ Input : ▁ String ▁ size " + " is ▁ not ▁ divisible ▁ by ▁ n " ) ; return ; } part_size = str_size / n ; for ( int i = 0 ; i < str_size ; i ++ ) { if ( i % part_size == 0 ) Console . WriteLine ( ) ; Console . Write ( str [ i ] ) ; } } static void Main ( ) { String str = " a _ simple _ divide _ string _ quest " ; divideString ( str , 4 ) ; } }


"Divide an array into K subarray with the given condition | C # implementation of above approach ; Function to divide an array into k parts such that the sum of difference of every element with the maximum element of that part is minimum ; Dp to store the values ; Fill up the dp table ; Intitilize maximum value ; Max element and the sum ; Run a loop from i to n ; Find the maximum number from i to l and the sum from i to l ; Find the sum of difference of every element with the maximum element ; If the array can be divided ; Returns the minimum sum in K parts ; Driver code"
Solution in C#:

using System ; class GFG { static int divideArray ( int [ ] arr , int n , int k ) { int [ , ] dp = new int [ 500 , 500 ] ; int i , j ; for ( i = 0 ; i < 500 ; i ++ ) for ( j = 0 ; j < 500 ; j ++ ) dp [ i , j ] = 0 ; k -= 1 ; for ( i = n - 1 ; i >= 0 ; i -- ) { for ( j = 0 ; j <= k ; j ++ ) { dp [ i , j ] = int . MaxValue ; int max_ = - 1 , sum = 0 ; for ( int l = i ; l < n ; l ++ ) { max_ = Math . Max ( max_ , arr [ l ] ) ; sum += arr [ l ] ; int diff = ( l - i + 1 ) * max_ - sum ; if ( j > 0 ) dp [ i , j ] = Math . Min ( dp [ i , j ] , diff + dp [ l + 1 , j - 1 ] ) ; else dp [ i , j ] = diff ; } } } return dp [ 0 , k ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 9 , 5 , 4 , 8 , 3 , 6 } ; int n = arr . Length ; int k = 2 ; Console . WriteLine ( divideArray ( arr , n , k ) ) ; } }


"Divide an array into k segments to maximize maximum of segment minimums | C # Program to find maximum value of maximum of minimums of k segments . ; function to calculate the max of all the minimum segments ; if we have to divide it into 1 segment then the min will be the answer ; If k >= 3 , return maximum of all elements . ; Driver function"
Solution in C#:

using System ; using System . Linq ; public class GFG { static int maxOfSegmentMins ( int [ ] a , int n , int k ) { if ( k == 1 ) return a . Min ( ) ; if ( k == 2 ) return Math . Max ( a [ 0 ] , a [ n - 1 ] ) ; return a . Max ( ) ; } static public void Main ( ) { int [ ] a = { - 10 , - 9 , - 8 , 2 , 7 , - 6 , - 5 } ; int n = a . Length ; int k = 2 ; Console . WriteLine ( maxOfSegmentMins ( a , n , k ) ) ; } }


"Divide an isosceles triangle in two parts with ratio of areas as n : m | C # program , to find height h which divide isosceles triangle into ratio n : m ; Function to return the height ; type cast the n , m into float ; calculate the height for cut ; Driver code"
Solution in C#:

using System ; class GFG { static float heightCalculate ( int H , int n , int m ) { float N = ( float ) ( n * 1.0 ) ; float M = ( float ) ( m * 1.0 ) ; float h = H * ( float ) Math . Sqrt ( N / ( N + M ) ) ; return h ; } public static void Main ( ) { int H = 10 , n = 3 , m = 4 ; Console . WriteLine ( heightCalculate ( H , n , m ) ) ; } }


"Divide array into increasing and decreasing subsequence without changing the order | C # implementation of the approach ; Function to print strictly increasing and strictly decreasing sequence if possible ; Arrays to store strictly increasing and decreasing sequence ; Initializing last element of both sequence ; Iterating through the array ; If current element can be appended to both the sequences ; If next element is greater than the current element Then append it to the strictly increasing array ; Otherwise append it to the strictly decreasing array ; If current element can be appended to the increasing sequence only ; If current element can be appended to the decreasing sequence only ; Else we can not make such sequences from the given array ; Print the required sequences ; Driver Code"
Solution in C#:

 using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static void Find_Sequence ( int [ ] arr , int n ) { ArrayList inc_arr = new ArrayList ( ) ; ArrayList dec_arr = new ArrayList ( ) ; int flag = 0 ; long inc = - 1 , dec = ( long ) 1e7 ; for ( int i = 0 ; i < n ; i ++ ) { if ( inc < arr [ i ] && arr [ i ] < dec ) { if ( arr [ i ] < arr [ i + 1 ] ) { inc = arr [ i ] ; inc_arr . Add ( arr [ i ] ) ; } else { dec = arr [ i ] ; dec_arr . Add ( arr [ i ] ) ; } } else if ( inc < arr [ i ] ) { inc = arr [ i ] ; inc_arr . Add ( arr [ i ] ) ; } else if ( dec > arr [ i ] ) { dec = arr [ i ] ; dec_arr . Add ( arr [ i ] ) ; } else { Console . Write ( - 1 ) ; flag = 1 ; break ; } } if ( flag == 0 ) { foreach ( int i in inc_arr ) Console . Write ( i + " ▁ " ) ; Console . Write ( '
 ' ) ; foreach ( int i in dec_arr ) Console . Write ( i + " ▁ " ) ; Console . Write ( '
 ' ) ; } } public static void Main ( string [ ] args ) { int [ ] arr = { 5 , 1 , 3 , 6 , 8 , 2 , 9 , 0 , 10 } ; int n = arr . Length ; Find_Sequence ( arr , n ) ; } }


"Divide array into two arrays which does not contain any pair with sum K | C # program for the above approach ; Function to split the given array into two separate arrays satisfying given condition ; Stores resultant arrays ; Traverse the array ; If a [ i ] is smaller than or equal to k / 2 ; Print first array ; Print second array ; Driver Code ; Given K ; Given array ; Given size"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void splitArray ( int [ ] a , int n , int k ) { List < int > first = new List < int > ( ) ; List < int > second = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] <= k / 2 ) first . Add ( a [ i ] ) ; else second . Add ( a [ i ] ) ; } for ( int i = 0 ; i < first . Count ; i ++ ) { Console . Write ( first [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; for ( int i = 0 ; i < second . Count ; i ++ ) { Console . Write ( second [ i ] + " ▁ " ) ; } } public static void Main ( ) { int k = 5 ; int [ ] a = { 0 , 1 , 3 , 2 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int n = a . Length ; splitArray ( a , n , k ) ; } }


"Divide array into two parts with equal sum according to the given constraints | C # implementation of the approach ; Function that checks if the given conditions are satisfied ; To store the prefix sum of the array elements ; Sort the array ; Compute the prefix sum array ; Maximum element in the array ; Variable to check if there exists any number ; Stores the index of the largest number present in the array smaller than i ; Stores the index of the smallest number present in the array greater than i ; Find index of smallest number greater than i ; Find index of smallest number greater than i ; If there exists a number ; If no such number exists print no ; Driver code"
Solution in C#:

using System ; class GFG { static void IfExists ( int [ ] arr , int n ) { int [ ] sum = new int [ n ] ; Array . Sort ( arr ) ; sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i ] ; int max = arr [ n - 1 ] ; bool flag = false ; for ( int i = 1 ; i <= max ; i ++ ) { int findex = 0 ; int lindex = 0 ; int l = 0 ; int r = n - 1 ; while ( l <= r ) { int m = ( l + r ) / 2 ; if ( arr [ m ] < i ) { findex = m ; l = m + 1 ; } else r = m - 1 ; } l = 1 ; r = n ; flag = false ; while ( l <= r ) { int m = ( r + l ) / 2 ; if ( arr [ m ] > i ) { lindex = m ; r = m - 1 ; } else l = m + 1 ; } if ( sum [ findex ] == sum [ n - 1 ] - sum [ lindex - 1 ] ) { flag = true ; break ; } } if ( flag ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 2 , 5 } ; int n = arr . Length ; IfExists ( arr , n ) ; } }


"Divide every element of one array by other array elements | C # program to find quotient of array elements ; Function to calculate the quotient of every element of the array ; Calculate the product of all elements ; To calculate the quotient of every array element ; Driver code"
Solution in C#:

using System ; class GFG { static void calculate ( int [ ] a , int [ ] b , int n , int m ) { int mul = 1 ; for ( int i = 0 ; i < m ; i ++ ) if ( b [ i ] != 0 ) mul = mul * b [ i ] ; for ( int i = 0 ; i < n ; i ++ ) { int x = ( int ) Math . Floor ( ( double ) ( a [ i ] / mul ) ) ; Console . Write ( x + " ▁ " ) ; } } public static void Main ( ) { int [ ] a = { 5 , 100 , 8 } ; int [ ] b = { 2 , 3 } ; int n = a . Length ; int m = b . Length ; calculate ( a , b , n , m ) ; } }


"Divide first N natural numbers into 3 equal sum subsets | C # implementation of the approach ; Function that returns true if the subsets are possible ; If n <= 3 then it is not possible to divide the elements in three subsets satisfying the given conditions ; Sum of all the elements in the range [ 1 , n ] ; If the sum is divisible by 3 then it is possible ; Driver code"
Solution in C#:

using System ; class GFG { public static bool possible ( int n ) { if ( n > 3 ) { int sum = ( n * ( n + 1 ) ) / 2 ; if ( sum % 3 == 0 ) { return true ; } } return false ; } static public void Main ( ) { int n = 5 ; if ( possible ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Divide given numeric string into at most two increasing subsequences which form an increasing string upon concatenation | C # program for the approach ; Function to check for valid subsequences ; Stores which element belongs to which subsequence ; Check for each pos if a possible subsequence exist or not ; Last member of 1 subsequence ; Last Member of 2 nd subsequence ; Check if current element can go to 2 nd subsequence ; Check if the current elements belongs to first subsequence ; If the current element does not belong to any subsequence ; Check if last digit of first subsequence is greater than pos ; If a subsequence is found , find the subsequences ; Stores the resulting subsequences ; Print the subsequence ; If no subsequence found , print - 1 ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findSubsequence ( string str ) { int n = str . Length ; char [ ] res = new char [ n ] ; for ( int i = 0 ; i < n ; i ++ ) res [ i ] = '0' ; for ( int pos = 0 ; pos <= 9 ; pos ++ ) { char lst1 = '0' ; bool flag = true ; char lst2 = ( char ) ( pos + '0' ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( lst2 <= str [ i ] ) { res [ i ] = '2' ; lst2 = str [ i ] ; } else if ( lst1 <= str [ i ] ) { res [ i ] = '1' ; lst1 = str [ i ] ; } else flag = false ; } if ( lst1 > pos + '0' ) flag = false ; if ( flag ) { string S1 = " " ; string S2 = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( res [ i ] == '1' ) { S1 += str [ i ] ; } else { S2 += str [ i ] ; } } Console . WriteLine ( S1 + ' ▁ ' + S2 ) ; return ; } } Console . Write ( " - 1" ) ; } public static void Main ( ) { string S = "040425524644" ; findSubsequence ( S ) ; S = "123456789" ; findSubsequence ( S ) ; } }


"Divide number into two parts divisible by given numbers | C # program to break the number string into two divisible parts by given numbers ; method prints divisible parts if possible , otherwise prints ' Not ▁ possible ' ; creating arrays to store reminder ; looping over all suffix and storing reminder with f ; getting suffix reminder from previous suffix reminder ; looping over all prefix and storing reminder with s ; getting prefix reminder from next prefix reminder ; updating base1 value ; now looping over all reminders to check partition condition ; if both reminders are 0 and digit itself is not 0 , then print result and return ; if we reach here , then string can ' be partitioned under constraints ; Driver Code"
Solution in C#:

using System ; class GFG { static void printTwoDivisibleParts ( String num , int f , int s ) { int N = num . Length ; int [ ] prefixReminder = new int [ N + 1 ] ; int [ ] suffixReminder = new int [ N + 1 ] ; suffixReminder [ 0 ] = 0 ; for ( int i = 1 ; i < N ; i ++ ) suffixReminder [ i ] = ( suffixReminder [ i - 1 ] * 10 + ( num [ i - 1 ] - '0' ) ) % f ; prefixReminder [ N ] = 0 ; int base1 = 1 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { prefixReminder [ i ] = ( prefixReminder [ i + 1 ] + ( num [ i ] - '0' ) * base1 ) % s ; base1 = ( base1 * 10 ) % s ; } for ( int i = 0 ; i < N ; i ++ ) { if ( prefixReminder [ i ] == 0 && suffixReminder [ i ] == 0 && num [ i ] != '0' ) { Console . WriteLine ( num . Substring ( 0 , i ) + " ▁ " + num . Substring ( i ) ) ; return ; } } Console . WriteLine ( " Not ▁ Possible " ) ; } public static void Main ( ) { String num = "246904096" ; int f = 12345 ; int s = 1024 ; printTwoDivisibleParts ( num , f , s ) ; } }


"Divide the array in K segments such that the sum of minimums is maximized | C # program to find the sum of the minimum of all the segments ; Function to maximize the sum of the minimums ; If k segments have been divided ; If we are at the end ; If we donot reach the end then return a negative number that cannot be the sum ; If at the end but k segments are not formed ; If the state has not been visited yet ; If the state has not been visited ; Get the minimum element in the segment ; Iterate and try to break at every index and create a segment ; Find the minimum element in the segment ; Find the sum of all the segments trying all the possible combinations ; Return the answer by memoizing it ; Driver Code ; Initialize dp array with - 1"
Solution in C#:

using System ; class GFG { static int MAX = 10 ; public static int maximizeSum ( int [ ] a , int n , int ind , int k , int [ ] dp ) { if ( k == 0 ) { if ( ind == n ) return 0 ; else return - 1000000000 ; } else if ( ind == n ) return - 1000000000 ; else if ( dp [ ind , k ] != - 1 ) return dp [ ind , k ] ; else { int ans = 0 ; int mini = a [ ind ] ; for ( int i = ind ; i < n ; i ++ ) { mini = Math . Min ( mini , a [ i ] ) ; ans = Math . Max ( ans , maximizeSum ( a , n , i + 1 , k - 1 , dp ) + mini ) ; } return dp [ ind , k ] = ans ; } } public static void Main ( String [ ] args ) { int [ ] a = { 5 , 7 , 4 , 2 , 8 , 1 , 6 } ; int k = 3 ; int n = a . Length ; int [ , ] dp = new int [ MAX , MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) dp [ i , j ] = - 1 ; } Console . WriteLine ( maximizeSum ( a , n , 0 , k , dp ) ) ; } }


"Divide the two given numbers by their common divisors | C # implementation of above approach ; Function to calculate gcd of two numbers ; Function to calculate all common divisors of two given numbers a , b -- > input integer numbers ; find gcd of a , b ; Driver code"
Solution in C#:

using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void commDiv ( int a , int b ) { int n = gcd ( a , b ) ; a = a / n ; b = b / n ; Console . WriteLine ( " A ▁ = ▁ " + a + " , ▁ B ▁ = ▁ " + b ) ; } public static void Main ( ) { int a = 10 , b = 15 ; commDiv ( a , b ) ; } }


"Divide the two given numbers by their common divisors | C # implementation of above approach ; print the numbers after dividing them by their common factors ; iterate from 1 to minimum of a and b ; if i is the common factor of both the numbers ; Driver code ; divide A and B by their common factors"
Solution in C#:

using System ; class GFG { static void divide ( int a , int b ) { for ( int i = 2 ; i <= Math . Min ( a , b ) ; i ++ ) { while ( a % i == 0 && b % i == 0 ) { a = a / i ; b = b / i ; } } Console . WriteLine ( " A ▁ = ▁ " + a + " , ▁ B ▁ = ▁ " + b ) ; } static public void Main ( ) { int A = 10 , B = 15 ; divide ( A , B ) ; } }


"Divide two integers without using multiplication , division and mod operator | C # implementation to Divide two integers without using multiplication , division and mod operator ; Function to divide a by b and return floor value it ; Calculate sign of divisor i . e . , sign will be negative only iff either one of them is negative otherwise it will be positive ; remove sign of operands ; Initialize the quotient ; test down from the highest bit and accumulate the tentative value for valid bit ; if the sign value computed earlier is - 1 then negate the value of quotient ; Driver code"
Solution in C#:

using System ; class GFG { public static long divide ( long dividend , long divisor ) { long sign = ( ( dividend < 0 ) ^ ( divisor < 0 ) ) ? - 1 : 1 ; dividend = Math . Abs ( dividend ) ; divisor = Math . Abs ( divisor ) ; long quotient = 0 , temp = 0 ; for ( int i = 31 ; i >= 0 ; -- i ) { if ( temp + ( divisor << i ) <= dividend ) { temp += divisor << i ; quotient |= 1L L << i ; } } if ( sign == - 1 ) quotient = - quotient ; return quotient ; } public static void Main ( ) { int a = 10 , b = 3 ; Console . WriteLine ( divide ( a , b ) ) ; int a1 = 43 , b1 = - 8 ; Console . WriteLine ( divide ( a1 , b1 ) ) ; } }


"Divide two integers without using multiplication , division and mod operator | Set2 | C # program for above approach ; Returns the quotient of dividend / divisor . ; Calculate sign of divisor i . e . , sign will be negative only if either one of them is negative otherwise it will be positive ; Remove signs of dividend and divisor ; Zero division Exception . ; Using Formula derived above . ; Driver code"
Solution in C#:

using System ; class GFG { static void Divide ( int a , int b ) { long dividend = ( long ) a ; long divisor = ( long ) b ; long sign = ( dividend < 0 ) ^ ( divisor < 0 ) ? - 1 : 1 ; dividend = Math . Abs ( dividend ) ; divisor = Math . Abs ( divisor ) ; if ( divisor == 0 ) { Console . WriteLine ( " Cannot ▁ Divide ▁ by ▁ 0" ) ; return ; } if ( dividend == 0 ) { Console . WriteLine ( a + " ▁ / ▁ " + b + " ▁ is ▁ equal ▁ to ▁ : ▁ " + 0 ) ; return ; } if ( divisor == 1 ) { Console . WriteLine ( a + " ▁ / ▁ " + b + " ▁ is ▁ equal ▁ to ▁ : ▁ " + sign * dividend ) ; return ; } Console . WriteLine ( a + " ▁ / ▁ " + b + " ▁ is ▁ equal ▁ to ▁ : ▁ " + Math . Floor ( sign * ( Math . Exp ( Math . Log ( dividend ) - Math . Log ( divisor ) ) ) ) ) ; } public static void Main ( ) { int a = 10 , b = 5 ; Divide ( a , b ) ; a = 49 ; b = - 7 ; Divide ( a , b ) ; } }


"Divisibility by 12 for a large number | C # Program to check if number is divisible by 12 ; if number greater then 3 ; find last digit ; no is odd ; find second last digit ; find sum of all digits ; if number is less then or equal to 100 ; Driver function"
Solution in C#:

using System ; class GFG { static bool isDvisibleBy12 ( string num ) { if ( num . Length >= 3 ) { int d1 = ( int ) num [ num . Length - 1 ] ; if ( d1 % 2 != 0 ) return false ; int d2 = ( int ) num [ num . Length - 2 ] ; int sum = 0 ; for ( int i = 0 ; i < num . Length ; i ++ ) sum += num [ i ] ; return ( sum % 3 == 0 && ( d2 * 10 + d1 ) % 4 == 0 ) ; } else { int number = int . Parse ( num ) ; return ( number % 12 == 0 ) ; } } public static void Main ( ) { String num = "12244824607284961224" ; if ( isDvisibleBy12 ( num ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Divisibility by 3 where each digit is the sum of all prefix digits modulo 10 | C # code to check divisibility by 3 ; Function to check the divisibility ; Cycle ; no of residual terms ; sum of residual terms ; if no of residue term = 0 ; if no of residue term = 1 ; if no of residue term = 2 ; if no of residue term = 3 ; sum of all digits ; divisibility check ; Driver Code"
Solution in C#:


"Divisibility by 64 with removal of bits allowed | C # program to find if given binary string can become divisible by 64 after removing some bits ; function to check if it is possible to make it a multiple of 64. ; counter to count 0 's ; length of the string ; loop which traverses right to left and calculates the number of zeros before 1. ; Driver code"
Solution in C#:

using System ; class GFG { static bool checking ( string s ) { int c = 0 ; int n = s . Length ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == '0' ) c ++ ; if ( c >= 6 && s [ i ] == '1' ) return true ; } return false ; } public static void Main ( ) { String s = "100010001" ; if ( checking ( s ) ) Console . WriteLine ( " Possible " ) ; else Console . WriteLine ( " Not ▁ possible " ) ; } }


"Division without using ' / ' operator | C # program to divide a number by other without using / operator ; Function to find division without using ' / ' operator ; Handling negative numbers ; if num1 is greater than equal to num2 subtract num2 from num1 and increase quotient by one . ; checking if neg equals to 1 then making quotient negative ; Driver Code"
Solution in C#:

using System ; class GFG { static int division ( int num1 , int num2 ) { if ( num1 == 0 ) return 0 ; if ( num2 == 0 ) return int . MaxValue ; bool negResult = false ; if ( num1 < 0 ) { num1 = - num1 ; if ( num2 < 0 ) num2 = - num2 ; else negResult = true ; } else if ( num2 < 0 ) { num2 = - num2 ; negResult = true ; } int quotient = 0 ; while ( num1 >= num2 ) { num1 = num1 - num2 ; quotient ++ ; } if ( negResult ) quotient = - quotient ; return quotient ; } public static void Main ( ) { int num1 = 13 , num2 = 2 ; Console . Write ( division ( num1 , num2 ) ) ; } }


"Divisors of n | C # program to count number of divisors of n ^ 2 which are not divisible by divisor of n ; Function to count divisors of n ^ 2 having no factors of ' n ' ; Increment count of i - th prime divisor ; Find next prime divisor ; Increment count if divisor still remains ; Initialize variable for counting the factors of n ^ 2 and n as ans1 and ans2 respectively ; Range based for - loop ; Use formula as discussed in above ; return the difference of answers ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int factors ( int n ) { Dictionary < int , int > prime = new Dictionary < int , int > ( ) ; for ( int i = 2 ; i <= Math . Sqrt ( n ) ; ++ i ) { while ( n % i == 0 ) { if ( prime . ContainsKey ( i ) ) { prime [ i ] = prime [ i ] + 1 ; } else { prime . Add ( i , 1 ) ; } n = n / i ; } } if ( n > 2 ) { if ( prime . ContainsKey ( n ) ) { prime [ n ] = prime [ n ] + 1 ; } else { prime . Add ( n , 1 ) ; } } int ans1 = 1 , ans2 = 1 ; foreach ( KeyValuePair < int , int > it in prime ) { ans1 *= 2 * it . Value + 1 ; ans2 *= it . Value + 1 ; } return ans1 - ans2 ; } public static void Main ( String [ ] args ) { int n = 5 ; Console . WriteLine ( factors ( n ) ) ; n = 8 ; Console . WriteLine ( factors ( n ) ) ; } }


"Dodecagonal number | C # program to find the nth Dodecagonal number ; function for Dodecagonal number ; formula for find Dodecagonal nth term ; Driver Code"
Solution in C#:

using System ; class GFG { static int Dodecagonal_number ( int n ) { return 5 * n * n - 4 * n ; } static void Main ( ) { int n = 7 ; Console . WriteLine ( Dodecagonal_number ( n ) ) ; n = 12 ; Console . WriteLine ( Dodecagonal_number ( n ) ) ; } }


"Dodecahedral number | C # program to find nth dodecahedral number ; Function to find dodecahedral number ; Formula to calculate nth dodecahedral number and return it into main function . ; Driver Code ; print result"
Solution in C#:

using System ; class GFG { static int dodecahedral_num ( int n ) { return n * ( 3 * n - 1 ) * ( 3 * n - 2 ) / 2 ; } public static void Main ( ) { int n = 5 ; Console . Write ( n + " the ▁ Dodecahedral " + " ▁ number : " ) ; Console . WriteLine ( dodecahedral_num ( n ) ) ; } }


"Domino and Tromino tiling problem | C # program for the above approach ; Function to find the total number of ways to tile a 2 * N board using the given types of tile ; If N is less than 3 ; Store all dp - states ; Base Case ; Traverse the range [ 2 , N ] ; Update the value of dp [ i , 0 ] ; Update the value of dp [ i , 1 ] ; Update the value of dp [ i , 2 ] ; Return the number of ways as the value of dp [ N , 0 ] ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MOD = 1000000007 ; static int numTilings ( int N ) { if ( N < 3 ) { return N ; } int [ , ] dp = new int [ N + 1 , 3 ] ; dp [ 0 , 0 ] = dp [ 1 , 0 ] = 1 ; dp [ 1 , 1 ] = dp [ 1 , 2 ] = 1 ; for ( int i = 2 ; i <= N ; i ++ ) { dp [ i , 0 ] = ( dp [ i - 1 , 0 ] + dp [ i - 2 , 0 ] + dp [ i - 2 , 1 ] + dp [ i - 2 , 2 ] ) % MOD ; dp [ i , 1 ] = ( dp [ i - 1 , 0 ] + dp [ i - 1 , 2 ] ) % MOD ; dp [ i , 2 ] = ( dp [ i - 1 , 0 ] + dp [ i - 1 , 1 ] ) % MOD ; } return dp [ N , 0 ] ; } public static void Main ( ) { int N = 3 ; Console . Write ( numTilings ( N ) ) ; } }


"Doolittle Algorithm : LU Decomposition | C # Program to decompose a matrix into lower and upper triangular matrix ; Decomposing matrix into Upper and Lower triangular matrix ; Upper Triangular ; Summation of L ( i , j ) * U ( j , k ) ; Evaluating U ( i , k ) ; Lower Triangular ; lower [ i , i ] = 1 ; Diagonal as 1 ; Summation of L ( k , j ) * U ( j , i ) ; Evaluating L ( k , i ) ; setw is for displaying nicely ; Displaying the result : ; Lower ; Upper ; Driver code"
Solution in C#:

 using System ; class GFG { static int MAX = 100 ; static String s = " " ; static void luDecomposition ( int [ , ] mat , int n ) { int [ , ] lower = new int [ n , n ] ; int [ , ] upper = new int [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int k = i ; k < n ; k ++ ) { int sum = 0 ; for ( int j = 0 ; j < i ; j ++ ) sum += ( lower [ i , j ] * upper [ j , k ] ) ; upper [ i , k ] = mat [ i , k ] - sum ; } for ( int k = i ; k < n ; k ++ ) { if ( i == k ) else { int sum = 0 ; for ( int j = 0 ; j < i ; j ++ ) sum += ( lower [ k , j ] * upper [ j , i ] ) ; lower [ k , i ] = ( mat [ k , i ] - sum ) / upper [ i , i ] ; } } } Console . WriteLine ( setw ( 2 ) + " 	 Lower ▁ Triangular " + setw ( 10 ) + " Upper ▁ Triangular " ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) Console . Write ( setw ( 4 ) + lower [ i , j ] + " 	 " ) ; Console . Write ( " 	 " ) ; for ( int j = 0 ; j < n ; j ++ ) Console . Write ( setw ( 4 ) + upper [ i , j ] + " 	 " ) ; Console . Write ( "
 " ) ; } } static String setw ( int noOfSpace ) { s = " " ; for ( int i = 0 ; i < noOfSpace ; i ++ ) s += " ▁ " ; return s ; } public static void Main ( String [ ] arr ) { int [ , ] mat = { { 2 , - 1 , - 2 } , { - 4 , 6 , 3 } , { - 4 , - 2 , 8 } } ; luDecomposition ( mat , 3 ) ; } }


"Doubly Circular Linked List | Set 1 ( Introduction and Insertion ) | C # program to illustrate inserting a Node in a Cicular Doubly Linked list in begging , end and middle ; Structure of a Node ; Function to insert at the end ; If the list is empty , create a single node circular and doubly list ; Find last node ; Create Node dynamically ; Start is going to be next of new_node ; Make new node previous of start ; Make last preivous of new node ; Make new node next of old last ; Function to insert Node at the beginning of the List , ; Pointer points to last Node ; Inserting the data ; setting up previous and next of new node ; Update next and previous pointers of start and last . ; Update start pointer ; Function to insert node with value as value1 . The new node is inserted after the node with with value2 ; Inserting the data ; Find node having value2 and next node of it ; insert new_node between temp and next . ; Driver code ; Start with the empty list ; Insert 5. So linked list becomes 5. null ; Insert 4 at the beginning . So linked list becomes 4.5 ; Insert 7 at the end . So linked list becomes 4.5 . 7 ; Insert 8 at the end . So linked list becomes 4.5 . 7.8 ; Insert 6 , after 5. So linked list becomes 4.5 . 6.7 . 8"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static Node start ; public class Node { public int data ; public Node next ; public Node prev ; } ; static void insertEnd ( int value ) { Node new_node ; if ( start == null ) { new_node = new Node ( ) ; new_node . data = value ; new_node . next = new_node . prev = new_node ; start = new_node ; return ; } Node last = ( start ) . prev ; new_node = new Node ( ) ; new_node . data = value ; new_node . next = start ; ( start ) . prev = new_node ; new_node . prev = last ; last . next = new_node ; } static void insertBegin ( int value ) { Node last = ( start ) . prev ; Node new_node = new Node ( ) ; new_node . data = value ; new_node . next = start ; new_node . prev = last ; last . next = ( start ) . prev = new_node ; start = new_node ; } static void insertAfter ( int value1 , int value2 ) { Node new_node = new Node ( ) ; new_node . data = value1 ; Node temp = start ; while ( temp . data != value2 ) temp = temp . next ; Node next = temp . next ; temp . next = new_node ; new_node . prev = temp ; new_node . next = next ; next . prev = new_node ; } static void display ( ) { Node temp = start ; Console . Write ( " in forward direction " while ( temp . next != start ) { Console . Write ( " { 0 } ▁ " , temp . data ) ; = temp . next ; } Console . Write ( " { 0 } ▁ " , temp . data ) ; Console . Write ( " in reverse direction " Node last = start . prev ; temp = last ; while ( temp . prev != last ) { Console . Write ( " { 0 } ▁ " , temp . data ) ; temp = temp . prev ; } Console . Write ( " { 0 } ▁ " , temp . data ) ; } public static void Main ( String [ ] args ) { Node start = null ; insertEnd ( 5 ) ; insertBegin ( 4 ) ; insertEnd ( 7 ) ; insertEnd ( 8 ) ; insertAfter ( 6 , 5 ) ; Console . Write ( " Created ▁ circular ▁ doubly ▁ linked ▁ list ▁ is : ▁ " ) ; display ( ) ; } }


"Doubly Circular Linked List | Set 1 ( Introduction and Insertion ) | Function to insert at the end ; If the list is empty , create a single node circular and doubly list ; If list is not empty Find last node ; Create Node dynamically ; Start is going to be next of new_node ; Make new node previous of start ; Make last preivous of new node ; Make new node next of old last"
Solution in C#:

static void insertEnd ( int value ) { Node new_node ; if ( start == null ) { new_node = new Node ( ) ; new_node . data = value ; new_node . next = new_node . prev = new_node ; start = new_node ; return ; } Node last = ( start ) . prev ; new_node = new Node ( ) ; new_node . data = value ; new_node . next = start ; ( start ) . prev = new_node ; new_node . prev = last ; last . next = new_node ; }


"Doubly Linked List | Set 1 ( Introduction and Insertion ) | Adding a node at the front of the list ; 1. allocate node * 2. put in the data ; 3. Make next of new node as head and previous as NULL ; 4. change prev of head node to new node ; 5. move the head to point to the new node"
Solution in C#:

public void push ( int new_data ) { Node new_Node = new Node ( new_data ) ; new_Node . next = head ; new_Node . prev = null ; if ( head != null ) head . prev = new_Node ; head = new_Node ; }


"Doubly Linked List | Set 1 ( Introduction and Insertion ) | Given a node as prev_node , insert a new node after the given node ; 1. check if the given prev_node is NULL ; 2. allocate node * 3. put in the data ; 4. Make next of new node as next of prev_node ; 5. Make the next of prev_node as new_node ; 6. Make prev_node as previous of new_node ; 7. Change previous of new_node 's next node"
Solution in C#:

public void InsertAfter ( Node prev_Node , int new_data ) { if ( prev_Node == null ) { Console . WriteLine ( " The ▁ given ▁ previous ▁ node ▁ cannot ▁ be ▁ NULL ▁ " ) ; return ; } Node new_node = new Node ( new_data ) ; new_node . next = prev_Node . next ; prev_Node . next = new_node ; new_node . prev = prev_Node ; if ( new_node . next != null ) new_node . next . prev = new_node ; }


"Droll Numbers | C # program for the above approach ; Function to check droll numbers ; To store sum of even prime factors ; To store sum of odd prime factors ; Add the number of 2 s that divide n in sum_even ; N must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Condition to check droll number ; Driver code ; Given Number N ; Function Call"
Solution in C#:

using System ; class GFG { static bool isDroll ( int n ) { if ( n == 1 ) return false ; int sum_even = 0 ; int sum_odd = 0 ; while ( n % 2 == 0 ) { sum_even += 2 ; n = n / 2 ; } for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { sum_odd += i ; n = n / i ; } } if ( n > 2 ) sum_odd += n ; return sum_even == sum_odd ; } public static void Main ( ) { int n = 72 ; if ( isDroll ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Dudeney Numbers | C # implementation of the approach ; Function that returns true if n is a Dudeney number ; If n is not a perfect cube ; Last digit ; Update the digit sum ; Remove the last digit ; If cube root of n is not equal to the sum of its digits ; Driver code"
Solution in C#:

using System ; class GFG { static bool isDudeney ( int n ) { int cube_rt = ( int ) ( Math . Round ( ( Math . Pow ( n , 1.0 / 3.0 ) ) ) ) ; if ( cube_rt * cube_rt * cube_rt != n ) return false ; int dig_sum = 0 ; int temp = n ; while ( temp > 0 ) { int rem = temp % 10 ; dig_sum += rem ; temp /= 10 ; } if ( cube_rt != dig_sum ) return false ; return true ; } public static void Main ( ) { int n = 17576 ; if ( isDudeney ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Duffinian Numbers | C # program for the above approach ; Recursive function to return gcd of a and b ; Function to calculate the sum of all divisors of a given number ; Sum of divisors ; Find all divisors of num ; If ' i ' is divisor of ' n ' ; If both divisors are same then add it once ; Add 1 and n to result as above loop considers proper divisors greater than 1. ; Function to check if n is an Duffinian number ; Calculate the sum of divisors ; If number is prime return false ; Find the gcd of n and sum of divisors of n ; Returns true if N and sumDivisors are relatively prime ; Driver code ; Given number ; Function call"
Solution in C#:

using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } 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 isDuffinian ( int n ) { int sumDivisors = divSum ( n ) ; if ( sumDivisors == n + 1 ) return false ; int hcf = gcd ( n , sumDivisors ) ; return hcf == 1 ; } public static void Main ( string [ ] args ) { int n = 36 ; if ( isDuffinian ( n ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }


"Duplicates in an array in O ( n ) time and by using O ( 1 ) extra space | Set | C # program to print all elements that appear more than once . ; Function to find repeating elements ; Flag variable used to represent whether repeating element is found or not . ; Check if current element is repeating or not . If it is repeating then value will be greater than or equal to n . ; Check if it is first repetition or not . If it is first repetition then value at index arr [ i ] is less than 2 * n . Print arr [ i ] if it is first repetition . ; Add n to index arr [ i ] to mark presence of arr [ i ] or to mark repetition of arr [ i ] . ; If flag variable is not set then no repeating element is found . So print - 1. ; Driver Code"
Solution in C#:

using System ; class GFG { static void printDuplicates ( int [ ] arr , int n ) { int i ; int fl = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ arr [ i ] % n ] >= n ) { if ( arr [ arr [ i ] % n ] < 2 * n ) { Console . Write ( arr [ i ] % n + " ▁ " ) ; fl = 1 ; } } arr [ arr [ i ] % n ] += n ; } if ( ! ( fl > 0 ) ) Console . Write ( " - 1" ) ; } public static void Main ( ) { int [ ] arr = { 1 , 6 , 3 , 1 , 3 , 6 , 6 } ; int arr_size = arr . Length ; printDuplicates ( arr , arr_size ) ; } }


"Dyck path | C # program to count number of Dyck Paths ; Returns count Dyck paths in n x n grid ; Compute value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Driver code"
Solution in C#:

using System ; class GFG { static int countDyckPaths ( int n ) { int res = 1 ; for ( int i = 0 ; i < n ; ++ i ) { res *= ( 2 * n - i ) ; res /= ( i + 1 ) ; } return res / ( n + 1 ) ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( " Number ▁ of ▁ " + " Dyck ▁ Paths ▁ is ▁ " + countDyckPaths ( n ) ) ; } }


"Dynamic Connectivity | Set 1 ( Incremental ) | C # implementation of incremental connectivity ; Finding the root of node i ; union of two nodes a and b ; union based on rank ; Returns true if two nodes have same root ; Performing an operation according to query type ; type 1 query means checking if node x and y are connected or not ; If roots of x and y is same then yes is the answer ; type 2 query refers union of x and y ; If x and y have different roots then union them ; Driver Code ; No . of nodes ; The following two arrays are used to implement disjoint set data structure . arr [ ] holds the parent nodes while rank array holds the rank of subset ; initializing both array and rank ; number of queries"
Solution in C#:

using System ; class GFG { static int root ( int [ ] arr , int i ) { while ( arr [ i ] != i ) { arr [ i ] = arr [ arr [ i ] ] ; i = arr [ i ] ; } return i ; } static void weighted_union ( int [ ] arr , int [ ] rank , int a , int b ) { int root_a = root ( arr , a ) ; int root_b = root ( arr , b ) ; if ( rank [ root_a ] < rank [ root_b ] ) { arr [ root_a ] = arr [ root_b ] ; rank [ root_b ] += rank [ root_a ] ; } else { arr [ root_b ] = arr [ root_a ] ; rank [ root_a ] += rank [ root_b ] ; } } static Boolean areSame ( int [ ] arr , int a , int b ) { return ( root ( arr , a ) == root ( arr , b ) ) ; } static void query ( int type , int x , int y , int [ ] arr , int [ ] rank ) { if ( type == 1 ) { if ( areSame ( arr , x , y ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } else if ( type == 2 ) { if ( areSame ( arr , x , y ) == false ) weighted_union ( arr , rank , x , y ) ; } } public static void Main ( String [ ] args ) { int n = 7 ; int [ ] arr = new int [ n ] ; int [ ] rank = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = i ; rank [ i ] = 1 ; } query ( 1 , 0 , 1 , arr , rank ) ; query ( 2 , 0 , 1 , arr , rank ) ; query ( 2 , 1 , 2 , arr , rank ) ; query ( 1 , 0 , 2 , arr , rank ) ; query ( 2 , 0 , 2 , arr , rank ) ; query ( 2 , 2 , 3 , arr , rank ) ; query ( 2 , 3 , 4 , arr , rank ) ; query ( 1 , 0 , 5 , arr , rank ) ; query ( 2 , 4 , 5 , arr , rank ) ; query ( 2 , 5 , 6 , arr , rank ) ; query ( 1 , 2 , 6 , arr , rank ) ; } }


"Dynamic Convex hull | Adding Points to an Existing Convex Hull | C # program to add given a point p to a given convext hull . The program assumes that the point of given convext hull are in anti - clockwise order . ; checks whether the point crosses the convex hull or not ; Returns the square of distance between two input points ; Checks whether the point is inside the convex hull or not ; Initialize the centroid of the convex hull ; Multiplying with n to avoid floating point ; if the mid and the given point lies always on the same side w . r . t every edge of the convex hull , then the point lies inside the convex hull ; Adds a point p to given convex hull a [ ] ; If point is inside p ; point having minimum distance from the point p ; Find the upper tangent ; Find the lower tangent ; Initialize result ; making the final hull by traversing points from up to low of given convex hull . ; Modify the original vector ; Driver code ; the set of points in the convex hull ; Print the modified Convex Hull"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static int orientation ( List < int > a , List < int > b , List < int > c ) { int res = ( b [ 1 ] - a [ 1 ] ) * ( c [ 0 ] - b [ 0 ] ) - ( c [ 1 ] - b [ 1 ] ) * ( b [ 0 ] - a [ 0 ] ) ; if ( res == 0 ) return 0 ; if ( res > 0 ) return 1 ; return - 1 ; } static int sqDist ( List < int > p1 , List < int > p2 ) { return ( p1 [ 0 ] - p2 [ 0 ] ) * ( p1 [ 0 ] - p2 [ 0 ] ) + ( p1 [ 1 ] - p2 [ 1 ] ) * ( p1 [ 1 ] - p2 [ 1 ] ) ; } static bool inside ( List < List < int > > A , List < int > p ) { List < int > mid = new List < int > ( ) { 0 , 0 } ; int n = A . Count ; for ( int i = 0 ; i < n ; i ++ ) { mid [ 0 ] += A [ i ] [ 0 ] ; mid [ 1 ] += A [ i ] [ 1 ] ; } for ( int i = 0 , j ; i < n ; i ++ ) { j = ( i + 1 ) % n ; int x1 = A [ i ] [ 0 ] * n , x2 = A [ j ] [ 0 ] * n ; int y1 = A [ i ] [ 1 ] * n , y2 = A [ j ] [ 1 ] * n ; int a1 = y1 - y2 ; int b1 = x2 - x1 ; int c1 = x1 * y2 - y1 * x2 ; int for_mid = a1 * mid [ 0 ] + b1 * mid [ 1 ] + c1 ; int for_p = a1 * p [ 0 ] * n + b1 * p [ 1 ] * n + c1 ; if ( for_mid * for_p < 0 ) return false ; } return true ; } static void addPoint ( List < List < int > > a , List < int > p ) { if ( inside ( a , p ) ) return ; int ind = 0 ; int n = a . Count ; for ( int i = 1 ; i < n ; i ++ ) { if ( sqDist ( p , a [ i ] ) < sqDist ( p , a [ ind ] ) ) { ind = i ; } } int up = ind ; while ( orientation ( p , a [ up ] , a [ ( up + 1 ) % n ] ) >= 0 ) up = ( up + 1 ) % n ; int low = ind ; while ( orientation ( p , a [ low ] , a [ ( n + low - 1 ) % n ] ) <= 0 ) low = ( n + low - 1 ) % n ; List < List < int > > ret = new List < List < int > > ( ) ; int curr = up ; ret . Add ( a [ curr ] ) ; while ( curr != low ) { curr = ( curr + 1 ) % n ; ret . Add ( a [ curr ] ) ; } ret . Add ( p ) ; a . Clear ( ) ; for ( int i = 0 ; i < ret . Count ; i ++ ) { a . Add ( ret [ i ] ) ; } } static public void Main ( ) { List < List < int > > a = new List < List < int > > ( ) ; a . Add ( new List < int > ( ) { 0 , 0 } ) ; a . Add ( new List < int >...


"Dynamic Programming on Trees | Set 2 | Function to pre - calculate the array in [ ] which stores the maximum height when travelled via branches ; Function to pre - calculate the array in [ ] which stores the maximum height when travelled via branches ; Initially every node has 0 height ; Traverse in the subtree of u ; If child is same as parent ; dfs called ; Recursively calculate the max height ; Function to pre - calculate the array ouut [ ] which stores the maximum height when traveled via parent ; Stores the longest and second longest branches ; Traverse in the subtress of u ; Compare and store the longest and second longest ; Traverse in the subtree of u ; If longest branch has the node , then consider the second longest branch ; Recursively calculate out [ i ] ; dfs function call ; Function to print all the maximum heights from every node ; Traversal to calculate in [ ] array ; Traversal to calculate out [ ] array ; Print all maximum heights ; Driver Code ; Initialize the tree given in the diagram ; Function to print the maximum height from every node"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static int MAX_NODES = 100 ; static int [ ] In = new int [ MAX_NODES ] ; static int [ ] Out = new int [ MAX_NODES ] ; static void dfs1 ( List < List < int > > v , int u , int parent ) { In [ u ] = 0 ; for ( int j = 0 ; j < v [ u ] . Count ; j ++ ) { int child = v [ u ] [ j ] ; if ( child == parent ) continue ; dfs1 ( v , child , u ) ; In [ u ] = Math . Max ( In [ u ] , 1 + In [ child ] ) ; } } static void dfs2 ( List < List < int > > v , int u , int parent ) { int mx1 = - 1 , mx2 = - 1 ; for ( int j = 0 ; j < v [ u ] . Count ; j ++ ) { int child = v [ u ] [ j ] ; if ( child == parent ) continue ; if ( In [ child ] >= mx1 ) { mx2 = mx1 ; mx1 = In [ child ] ; } else if ( In [ child ] > mx2 ) mx2 = In [ child ] ; } for ( int j = 0 ; j < v [ u ] . Count ; j ++ ) { int child = v [ u ] [ j ] ; if ( child == parent ) continue ; int longest = mx1 ; if ( mx1 == In [ child ] ) longest = mx2 ; Out [ child ] = 1 + Math . Max ( Out [ u ] , 1 + longest ) ; dfs2 ( v , child , u ) ; } } static void addEdge ( List < List < int > > adj , int u , int v ) { adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } static void printHeights ( List < List < int > > v , int n ) { dfs1 ( v , 1 , 0 ) ; dfs2 ( v , 1 , 0 ) ; for ( int i = 1 ; i < n ; i ++ ) Console . WriteLine ( " The ▁ maximum ▁ height ▁ when ▁ node ▁ " + i + " ▁ is ▁ considered ▁ as ▁ root ▁ is ▁ " + Math . Max ( In [ i ] , Out [ i ] ) ) ; } static public void Main ( ) { int V = 12 ; List < List < int > > adj = new List < List < int > > ( ) ; for ( int i = 0 ; i < V ; i ++ ) adj . Add ( new List < int > ( ) ) ; addEdge ( adj , 1 , 2 ) ; addEdge ( adj , 1 , 3 ) ; addEdge ( adj , 1 , 4 ) ; addEdge ( adj , 2 , 5 ) ; addEdge ( adj , 2 , 6 ) ; addEdge ( adj , 3 , 7 ) ; addEdge ( adj , 7 , 10 ) ; addEdge ( adj , 7 , 11 ) ; addEdge ( adj , 4 , 8 ) ; addEdge ( adj , 4 , 9 ) ; printHeights ( adj , V ) ; } }


"Dynamic Programming on Trees | Set | C # code to find the maximum path sum ; function for dfs traversal and to store the maximum value in [ ] dp for every node till the leaves ; initially dp [ u ] is always a [ u ] ; stores the maximum value from nodes ; traverse the tree ; if child is parent , then we continue without recursing further ; call dfs for further traversal ; store the maximum of previous visited node and present visited node ; add the maximum value returned to the parent node ; function that returns the maximum value ; Driver Code ; Number of nodes ; adjacency list ; create undirected edges initialize the tree given in the diagram ; values of node 1 , 2 , 3. ... 14 ; function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int [ ] dp = new int [ 100 ] ; static void dfs ( int [ ] a , List < int > [ ] v , int u , int parent ) { dp [ u ] = a [ u - 1 ] ; int maximum = 0 ; foreach ( int child in v [ u ] ) { if ( child == parent ) continue ; dfs ( a , v , child , u ) ; maximum = Math . Max ( maximum , dp [ child ] ) ; } dp [ u ] += maximum ; } static int maximumValue ( int [ ] a , List < int > [ ] v ) { dfs ( a , v , 1 , 0 ) ; return dp [ 1 ] ; } public static void Main ( String [ ] args ) { int n = 14 ; List < int > [ ] v = new List < int > [ n + 1 ] ; for ( int i = 0 ; i < v . Length ; i ++ ) v [ i ] = new List < int > ( ) ; v [ 1 ] . Add ( 2 ) ; v [ 2 ] . Add ( 1 ) ; v [ 1 ] . Add ( 3 ) ; v [ 3 ] . Add ( 1 ) ; v [ 1 ] . Add ( 4 ) ; v [ 4 ] . Add ( 1 ) ; v [ 2 ] . Add ( 5 ) ; v [ 5 ] . Add ( 2 ) ; v [ 2 ] . Add ( 6 ) ; v [ 6 ] . Add ( 2 ) ; v [ 3 ] . Add ( 7 ) ; v [ 7 ] . Add ( 3 ) ; v [ 4 ] . Add ( 8 ) ; v [ 8 ] . Add ( 4 ) ; v [ 4 ] . Add ( 9 ) ; v [ 9 ] . Add ( 4 ) ; v [ 4 ] . Add ( 10 ) ; v [ 10 ] . Add ( 4 ) ; v [ 5 ] . Add ( 11 ) ; v [ 11 ] . Add ( 5 ) ; v [ 5 ] . Add ( 12 ) ; v [ 12 ] . Add ( 5 ) ; v [ 7 ] . Add ( 13 ) ; v [ 13 ] . Add ( 7 ) ; v [ 7 ] . Add ( 14 ) ; v [ 14 ] . Add ( 7 ) ; int [ ] a = { 3 , 2 , 1 , 10 , 1 , 3 , 9 , 1 , 5 , 3 , 4 , 5 , 9 , 8 } ; Console . WriteLine ( maximumValue ( a , v ) ) ; } }


"Dynamic Programming | Wildcard Pattern Matching | Linear Time and Constant Space | C # program to implement wildcard pattern matching algorithm ; Function that matches input text with given wildcard pattern ; empty pattern can only match with empty string . Base Case : ; step - 1 : initialize markers : ; For step - ( 2 , 5 ) ; For step - ( 3 ) ; For step - ( 4 ) ; For step - ( 5 ) ; For step - ( 6 ) ; For step - ( 7 ) ; Final Check ; Driver code"
Solution in C#:

using System ; class GFG { static Boolean strmatch ( char [ ] txt , char [ ] pat , int n , int m ) { if ( m == 0 ) return ( n == 0 ) ; int i = 0 , j = 0 , index_txt = - 1 , index_pat = - 1 ; while ( i < n ) { if ( j < m && txt [ i ] == pat [ j ] ) { i ++ ; j ++ ; } else if ( j < m && pat [ j ] == ' ? ' ) { i ++ ; j ++ ; } else if ( j < m && pat [ j ] == ' * ' ) { index_txt = i ; index_pat = j ; j ++ ; } else if ( index_pat != - 1 ) { j = index_pat + 1 ; i = index_txt + 1 ; index_txt ++ ; } else { return false ; } } while ( j < m && pat [ j ] == ' * ' ) { j ++ ; } if ( j == m ) { return true ; } return false ; } public static void Main ( String [ ] args ) { char [ ] str = " baaabab " . ToCharArray ( ) ; char [ ] pattern = " * * * * * ba * * * * * ab " . ToCharArray ( ) ; char pattern [ ] = " ba * * * * * ab " ; char pattern [ ] = " ba * ab " ; char pattern [ ] = " a * ab " ; if ( strmatch ( str , pattern , str . Length , pattern . Length ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; char [ ] pattern2 = " a * * * * * ab " . ToCharArray ( ) ; if ( strmatch ( str , pattern2 , str . Length , pattern2 . Length ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Economical Numbers | C # implementation to find Economical Numbers till n ; Array to store all prime less than and equal to MAX . ; Utility function for sieve of sundaram ; In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x . Since we want primes smaller than MAX , we reduce MAX to half ; Main logic of Sundaram . Mark all numbers which do not generate prime number by doing 2 * i + 1 ; Since 2 is a prime number ; Print other primes . Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; Function to check if a number is a Economical number ; Count digits in original number ; Count all digits in prime factors of n pDigit is going to hold this value . ; Count powers of p in n ; If primes [ i ] is a prime factor , ; Count the power of prime factors ; Add its digits to pDigit . ; Add digits of power of prime factors to pDigit . ; If n != 1 then one prime factor still to be summed up ; ; If digits in prime factors is less than digits in original number then return true . Else return false . ; Driver code ; Finding all prime numbers before limit . These numbers are used to find prime factors ."
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MAX = 10000 ; static List < int > primes = new List < int > ( ) ; static void sieveSundaram ( ) { bool [ ] marked = new bool [ MAX / 2 + 1 ] ; for ( int i = 1 ; i <= ( Math . Sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked [ j ] = true ; primes . Add ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked [ i ] == false ) primes . Add ( 2 * i + 1 ) ; } static bool isEconomical ( int n ) { if ( n == 1 ) return false ; int original_no = n ; int sumDigits = 0 ; while ( original_no > 0 ) { sumDigits ++ ; original_no = original_no / 10 ; } int pDigit = 0 , count_exp = 0 , p = 0 ; for ( int i = 0 ; primes [ i ] <= n / 2 ; i ++ ) { while ( n % primes [ i ] == 0 ) { p = primes [ i ] ; n = n / p ; count_exp ++ ; } while ( p > 0 ) { pDigit ++ ; p = p / 10 ; } while ( count_exp > 1 ) { pDigit ++ ; count_exp = count_exp / 10 ; } } if ( n != 1 ) { while ( n > 0 ) { pDigit ++ ; n = n / 10 ; } } return ( pDigit < sumDigits ) ; } public static void Main ( String [ ] args ) { sieveSundaram ( ) ; for ( int i = 1 ; i < 200 ; i ++ ) if ( isEconomical ( i ) ) Console . Write ( i + " ▁ " ) ; } }


"Edit Distance | DP |  ; If any string is empty , return the remaining characters of other string ; To check if the recursive tree for given n & m has already been executed ; If characters are equal , execute recursive function for n - 1 , m - 1 ; If characters are nt equal , we need to find the minimum cost out of all 3 operations . ; temp variables ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int minDis ( string s1 , string s2 , int n , int m , List < List < int > > dp ) { if ( n == 0 ) return m ; if ( m == 0 ) return n ; if ( dp [ n ] [ m ] != - 1 ) return dp [ n ] [ m ] ; if ( s1 [ n - 1 ] == s2 [ m - 1 ] ) { if ( dp [ n - 1 ] [ m - 1 ] == - 1 ) { return dp [ n ] [ m ] = minDis ( s1 , s2 , n - 1 , m - 1 , dp ) ; } else return dp [ n ] [ m ] = dp [ n - 1 ] [ m - 1 ] ; } else { int m1 , m2 , m3 ; if ( dp [ n - 1 ] [ m ] != - 1 ) { m1 = dp [ n - 1 ] [ m ] ; } else { m1 = minDis ( s1 , s2 , n - 1 , m , dp ) ; } if ( dp [ n ] [ m - 1 ] != - 1 ) { m2 = dp [ n ] [ m - 1 ] ; } else { m2 = minDis ( s1 , s2 , n , m - 1 , dp ) ; } if ( dp [ n - 1 ] [ m - 1 ] != - 1 ) { m3 = dp [ n - 1 ] [ m - 1 ] ; } else { m3 = minDis ( s1 , s2 , n - 1 , m - 1 , dp ) ; } return dp [ n ] [ m ] = 1 + Math . Min ( m1 , Math . Min ( m2 , m3 ) ) ; } } static void Main ( ) { string str1 = " voldemort " ; string str2 = " dumbledore " ; int n = str1 . Length , m = str2 . Length ; List < List < int > > dp = new List < List < int > > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { dp . Add ( new List < int > ( ) ) ; for ( int j = 0 ; j < m + 1 ; j ++ ) { dp [ i ] . Add ( - 1 ) ; } } Console . WriteLine ( minDis ( str1 , str2 , n , m , dp ) ) ; } }


"Edit Distance | DP | A Space efficient Dynamic Programming based C # program to find minimum number operations to convert str1 to str2 ; Create a DP array to memoize result of previous computations ; Base condition when second String is empty then we remove all characters ; Start filling the DP This loop run for every character in second String ; This loop compares the char from second String with first String characters ; if first String is empty then we have to perform add character operation to get second String ; if character from both String is same then we do not perform any operation . here i % 2 is for bound the row number . ; if character from both String is not same then we take the minimum from three specified operation ; after complete fill the DP array if the len2 is even then we end up in the 0 th row else we end up in the 1 th row so we take len2 % 2 to get row ; Driver program"
Solution in C#:

 using System ; class GFG { static void EditDistDP ( String str1 , String str2 ) { int len1 = str1 . Length ; int len2 = str2 . Length ; int [ , ] DP = new int [ 2 , len1 + 1 ] ; for ( int i = 0 ; i <= len1 ; i ++ ) DP [ 0 , i ] = i ; for ( int i = 1 ; i <= len2 ; i ++ ) { for ( int j = 0 ; j <= len1 ; j ++ ) { if ( j == 0 ) DP [ i % 2 , j ] = i ; else if ( str1 [ j - 1 ] == str2 [ i - 1 ] ) { DP [ i % 2 , j ] = DP [ ( i - 1 ) % 2 , j - 1 ] ; } else { DP [ i % 2 , j ] = 1 + Math . Min ( DP [ ( i - 1 ) % 2 , j ] , Math . Min ( DP [ i % 2 , j - 1 ] , DP [ ( i - 1 ) % 2 , j - 1 ] ) ) ; } } } Console . Write ( DP [ len2 % 2 , len1 ] + "
 " ) ; } public static void Main ( String [ ] args ) { String str1 = " food " ; String str2 = " money " ; EditDistDP ( str1 , str2 ) ; } }


"Edit distance and LCS ( Longest Common Subsequence ) | C # program to find Edit Distance ( when only two operations areallowed , insert and delete ) using LCS . ; Find LCS ; Edit distance is delete operations + insert operations . ; Driver Code"
Solution in C#:

using System ; class GFG { static int editDistanceWith2Ops ( String X , String Y ) { int m = X . Length , n = Y . Length ; int [ , ] L = new int [ m + 1 , n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) { L [ i , j ] = 0 ; } else if ( X [ i - 1 ] == Y [ j - 1 ] ) { L [ i , j ] = L [ i - 1 , j - 1 ] + 1 ; } else { L [ i , j ] = Math . Max ( L [ i - 1 , j ] , L [ i , j - 1 ] ) ; } } } int lcs = L [ m , n ] ; return ( m - lcs ) + ( n - lcs ) ; } public static void Main ( ) { String X = " abc " , Y = " acd " ; Console . Write ( editDistanceWith2Ops ( X , Y ) ) ; } }


"Efficient Program to Compute Sum of Series 1 / 1 ! + 1 / 2 ! + 1 / 3 ! + 1 / 4 ! + . . + 1 / n ! | A simple C # program to compute sum of series 1 / 1 ! + 1 / 2 ! + . . + 1 / n ! ; An Efficient Function to return value of 1 / 1 ! + 1 / 2 ! + . . + 1 / n ! ; Update factorial ; Update series sum ; Driver program"
Solution in C#:

using System ; class GFG { static double sum ( int n ) { double sum = 0 ; int fact = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { fact *= i ; sum += 1.0 / fact ; } return sum ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( sum ( n ) ) ; } }


"Efficient Program to Compute Sum of Series 1 / 1 ! + 1 / 2 ! + 1 / 3 ! + 1 / 4 ! + . . + 1 / n ! | A simple C # program to compute sum of series 1 / 1 ! + 1 / 2 ! + . . + 1 / n ! ; Utility function to find ; A Simple Function to return value of 1 / 1 ! + 1 / 2 ! + . . + 1 / n ! ; Driver program"
Solution in C#:

using System ; class GFG { static int factorial ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res *= i ; return res ; } static double sum ( int n ) { double sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += 1.0 / factorial ( i ) ; return sum ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( sum ( n ) ) ; } }


"Efficient method for 2 's complement of a binary string | An efficient c # program to find 2 's complement ; Method to find two 's complement ; Traverse the string to get first '1' from the last of string ; If there exists no '1' concat 1 at the starting of string ; Continue traversal after the position of first '1' ; Just flip the values ; return the modified string ; Driver Code"
Solution in C#:

using System ; using System . Text ; class GFG { public static string findTwoscomplement ( StringBuilder str ) { int n = str . Length ; int i ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == '1' ) { break ; } } if ( i == - 1 ) { return "1" + str ; } for ( int k = i - 1 ; k >= 0 ; k -- ) { if ( str [ k ] == '1' ) { str . Remove ( k , k + 1 - k ) . Insert ( k , "0" ) ; } else { str . Remove ( k , k + 1 - k ) . Insert ( k , "1" ) ; } } return str . ToString ( ) ; } public static void Main ( string [ ] args ) { StringBuilder str = new StringBuilder ( "00000101" ) ; Console . WriteLine ( findTwoscomplement ( str ) ) ; } }


"Efficient method to store a Lower Triangular Matrix using row | C # program for the above approach ; Dimensions of a matrix ; Structure of the efficient matrix ; Function to set the values in the Matrix ; Function to store the values in the Matrix ; Function to display the elements of the matrix ; Traverse the matrix ; Function to generate an efficient matrix ; Declare efficient Matrix ; Initialize the Matrix ; Set the values in matrix ; Return the matrix ; Driver Code ; Stores the efficient matrix ; Print the Matrix"
Solution in C#:

 using System ; public class GFG { static int N = 5 ; class Matrix { public int [ ] A ; public int size ; } ; static void Set ( Matrix mat , int i , int j , int x ) { if ( i >= j ) mat . A [ i * ( i - 1 ) / 2 + j - 1 ] = x ; } static int Get ( Matrix mat , int i , int j ) { if ( i >= j ) { return mat . A [ i * ( i - 1 ) / 2 + j - 1 ] ; } else { return 0 ; } } static void Display ( Matrix mat ) { int i , j ; for ( i = 1 ; i <= mat . size ; i ++ ) { for ( j = 1 ; j <= mat . size ; j ++ ) { if ( i >= j ) { Console . Write ( " { 0 } ▁ " , mat . A [ i * ( i - 1 ) / 2 + j - 1 ] ) ; } else { Console . Write ( "0 ▁ " ) ; } } Console . Write ( "
 " ) ; } } static Matrix createMat ( int [ , ] Mat ) { Matrix mat = new Matrix ( ) ; mat . size = N ; mat . A = new int [ ( mat . size * ( mat . size + 1 ) ) / 2 ] ; int i , j ; for ( i = 1 ; i <= mat . size ; i ++ ) { for ( j = 1 ; j <= mat . size ; j ++ ) { Set ( mat , i , j , Mat [ i - 1 , j - 1 ] ) ; } } return mat ; } public static void Main ( String [ ] args ) { int [ , ] Mat = { { 1 , 0 , 0 , 0 , 0 } , { 1 , 2 , 0 , 0 , 0 } , { 1 , 2 , 3 , 0 , 0 } , { 1 , 2 , 3 , 4 , 0 } , { 1 , 2 , 3 , 4 , 5 } } ; Matrix mat = createMat ( Mat ) ; Display ( mat ) ; } }


"Efficient program to calculate e ^ x | C # efficient program to calculate e raise to the power x ; Function returns approximate value of e ^ x using sum of first n terms of Taylor Series ; initialize sum of series ; driver program"
Solution in C#:

using System ; class GFG { static float exponential ( int n , float x ) { float sum = 1 ; for ( int i = n - 1 ; i > 0 ; -- i ) sum = 1 + x * sum / i ; return sum ; } public static void Main ( ) { int n = 10 ; float x = 1 ; Console . Write ( " e ^ x ▁ = ▁ " + exponential ( n , x ) ) ; } }


"Efficient program to print the number of factors of n numbers | C # program to count number of factors of an array of integers ; function to generate all prime factors of numbers from 1 to 10 ^ 6 ; Initializes all the positions with their value . ; Initializes all multiples of 2 with 2 ; A modified version of Sieve of Eratosthenes to store the smallest prime factor that divides every number . ; check if it has no prime factor . ; Initializes of j starting from i * i ; if it has no prime factor before , then stores the smallest prime divisor ; function to calculate number of factors ; stores the smallest prime number that divides n ; stores the count of number of times a prime number divides n . ; reduces to the next number after prime factorization of n ; false when prime factorization is done ; if the same prime number is dividing n , then we increase the count ; if its a new prime factor that is factorizing n , then we again set c = 1 and change dup to the new prime factor , and apply the formula explained above . ; prime factorizes a number ; for the last prime factor ; Driver program to test above function ; array to store prime factors ; generate prime factors of number upto 10 ^ 6"
Solution in C#:

using System ; class GFG { static int MAX = 1000001 ; static int [ ] factor ; static void generatePrimeFactors ( ) { factor [ 1 ] = 1 ; for ( int i = 2 ; i < MAX ; i ++ ) factor [ i ] = i ; for ( int i = 4 ; i < MAX ; i += 2 ) factor [ i ] = 2 ; for ( int i = 3 ; i * i < MAX ; i ++ ) { if ( factor [ i ] == i ) { for ( int j = i * i ; j < MAX ; j += i ) { if ( factor [ j ] == j ) factor [ j ] = i ; } } } } static int calculateNoOFactors ( int n ) { if ( n == 1 ) return 1 ; int ans = 1 ; int dup = factor [ n ] ; int c = 1 ; int j = n / factor [ n ] ; while ( j != 1 ) { if ( factor [ j ] == dup ) c += 1 ; else { dup = factor [ j ] ; ans = ans * ( c + 1 ) ; c = 1 ; } j = j / factor [ j ] ; } ans = ans * ( c + 1 ) ; return ans ; } public static void Main ( ) { factor = new int [ MAX ] ; factor [ 0 ] = 0 ; generatePrimeFactors ( ) ; int [ ] a = { 10 , 30 , 100 , 450 , 987 } ; int q = a . Length ; for ( int i = 0 ; i < q ; i ++ ) Console . Write ( calculateNoOFactors ( a [ i ] ) + " ▁ " ) ; } }


"Efficiently check if a string has all unique characters without using any additional data structure | A space efficient program to check if all characters of string are unique . ; Returns true if all characters of str are unique . Assumptions : ( 1 ) str contains only characters from ' a ' to ' z ' . ( 2 ) integers are stored using 32 bits ; An integer to store presence or absence of 26 characters using its 32 bits . ; If bit corresponding to current character is already set ; set bit in checker ; Driver code"
Solution in C#:

using System ; class GFG { static bool areChractersUnique ( string str ) { int checker = 0 ; for ( int i = 0 ; i < str . Length ; ++ i ) { int val = ( str [ i ] - ' a ' ) ; if ( ( checker & ( 1 << val ) ) > 0 ) return false ; checker |= ( 1 << val ) ; } return true ; } public static void Main ( ) { string s = " aaabbccdaa " ; if ( areChractersUnique ( s ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Efficiently check if a string has all unique characters without using any additional data structure | Driver code"
Solution in C#:

using System ; public class GFG { static bool unique ( String s ) { Array . Sort ( s . ToCharArray ( ) ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == s [ i + 1 ] ) { return false ; } } return true ; } public static void Main ( String [ ] args ) { if ( unique ( " abcdd " ) == true ) { Console . WriteLine ( " String ▁ is ▁ Unique " ) ; } else { Console . WriteLine ( " String ▁ is ▁ not ▁ Unique " ) ; } } }


"Efficiently compute sums of diagonals of a matrix | A simple C # program to find sum of diagonals ; Condition for principal diagonal ; Condition for secondary diagonal ; Driver code"
Solution in C#:

using System ; public class GFG { static void printDiagonalSums ( int [ , ] mat , int n ) { int principal = 0 , secondary = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) principal += mat [ i , j ] ; if ( ( i + j ) == ( n - 1 ) ) secondary += mat [ i , j ] ; } } Console . WriteLine ( " Principal ▁ Diagonal : " + principal ) ; Console . WriteLine ( " Secondary ▁ Diagonal : " + secondary ) ; } static public void Main ( ) { int [ , ] a = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } } ; printDiagonalSums ( a , 4 ) ; } }


"Efficiently compute sums of diagonals of a matrix | An efficient C # program to find sum of diagonals ; Driver code"
Solution in C#:

using System ; public class GFG { static void printDiagonalSums ( int [ , ] mat , int n ) { int principal = 0 , secondary = 0 ; for ( int i = 0 ; i < n ; i ++ ) { principal += mat [ i , i ] ; secondary += mat [ i , n - i - 1 ] ; } Console . WriteLine ( " Principal ▁ Diagonal : " + principal ) ; Console . WriteLine ( " Secondary ▁ Diagonal : " + secondary ) ; } static public void Main ( ) { int [ , ] a = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } } ; printDiagonalSums ( a , 4 ) ; } }


"Efficiently find first repeated character in a string without using any additional data structure in one traversal | C # program to Efficiently check First repeated character ; Returns - 1 if all characters of str are unique . Assumptions : ( 1 ) str contains only characters from ' a ' to ' z ' ( 2 ) integers are stored using 32 bits ; An integer to store presence / absence of 26 characters using its 32 bits . ; If bit corresponding to current character is already set ; set bit in checker ; Driver code"
Solution in C#:

using System ; public class First_Repeated_char { static int FirstRepeated ( string str ) { int checker = 0 ; for ( int i = 0 ; i < str . Length ; ++ i ) { int val = ( str [ i ] - ' a ' ) ; if ( ( checker & ( 1 << val ) ) > 0 ) return i ; checker |= ( 1 << val ) ; } return - 1 ; } public static void Main ( ) { string s = " abcfdeacf " ; int i = FirstRepeated ( s ) ; if ( i != - 1 ) Console . WriteLine ( " Char ▁ = ▁ " + s [ i ] + " ▁ and ▁ Index ▁ = ▁ " + i ) ; else Console . WriteLine ( " No ▁ repeated ▁ Char " ) ; } }


"Efficiently merging two sorted arrays with O ( 1 ) extra space |  ; Find maximum element of both array ; Increment one two avoid collision of 0 and maximum element of array in modulo operation ; Recover back original element to compare ; Update element by adding multiplication with new number ; Update element by adding multiplication with new number ; Process those elements which are left in array a ; Process those elements which are left in array a ; Finally update elements by dividing with maximum element ; Finally update elements by dividing with maximum element ; Driver code ; Length of a ; length of b ; Function Call"
Solution in C#:

using System ; public class GFG { static void mergeArray ( int [ ] a , int [ ] b , int n , int m ) { int mx = 0 ; for ( int I = 0 ; I < n ; I ++ ) { mx = Math . Max ( mx , a [ I ] ) ; } for ( int I = 0 ; I < m ; I ++ ) { mx = Math . Max ( mx , b [ I ] ) ; } mx ++ ; int i = 0 , j = 0 , k = 0 ; while ( i < n && j < m && k < ( n + m ) ) { int e1 = a [ i ] % mx ; int e2 = b [ j ] % mx ; if ( e1 <= e2 ) { if ( k < n ) a [ k ] += ( e1 * mx ) ; else b [ k - n ] += ( e1 * mx ) ; i ++ ; k ++ ; } else { if ( k < n ) a [ k ] += ( e2 * mx ) ; else b [ k - n ] += ( e2 * mx ) ; j ++ ; k ++ ; } } while ( i < n ) { int el = a [ i ] % mx ; if ( k < n ) a [ k ] += ( el * mx ) ; else b [ k - n ] += ( el * mx ) ; i ++ ; k ++ ; } while ( j < m ) { int el = b [ j ] % mx ; if ( k < n ) b [ k ] += ( el * mx ) ; else b [ k - n ] += ( el * mx ) ; j ++ ; k ++ ; } for ( int In = 0 ; In < n ; In ++ ) a [ In ] = a [ In ] / mx ; for ( int In = 0 ; In < m ; In ++ ) b [ In ] = b [ In ] / mx ; return ; } static public void Main ( ) { int [ ] a = { 3 , 5 , 6 , 8 , 12 } ; int [ ] b = { 1 , 4 , 9 , 13 } ; int n = a . Length ; int m = b . Length ; mergeArray ( a , b , n , m ) ; Console . Write ( " First ▁ array ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; Console . Write ( " Second ▁ array ▁ : ▁ " ) ; for ( int i = 0 ; i < m ; i ++ ) Console . Write ( b [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } }


"Efficiently merging two sorted arrays with O ( 1 ) extra space |  ; Length of first arr1 ; Length of second arr2 ; Now traverse the array1 and if arr2 first element is less than arr1 then swap ; Swap ; We will store the firstElement of array2 and left shift all the element and store the firstElement in arr2 [ k - 1 ] ; Read the arr1 ; Read the arr2 ; Driver Code"
Solution in C#:

using System ; class GFG { public static void mergeArray ( int [ ] arr1 , int [ ] arr2 ) { int n = arr1 . Length ; int m = arr2 . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr1 [ i ] > arr2 [ 0 ] ) { int temp = arr1 [ i ] ; arr1 [ i ] = arr2 [ 0 ] ; arr2 [ 0 ] = temp ; int firstElement = arr2 [ 0 ] ; int k ; for ( k = 1 ; k < m && arr2 [ k ] < firstElement ; k ++ ) { arr2 [ k - 1 ] = arr2 [ k ] ; } arr2 [ k - 1 ] = firstElement ; } } foreach ( int i in arr1 ) { Console . Write ( i + " ▁ " ) ; } Console . WriteLine ( ) ; foreach ( int i in arr2 ) { Console . Write ( i + " ▁ " ) ; } } static public void Main ( ) { int [ ] arr1 = { 1 , 3 , 5 , 7 } ; int [ ] arr2 = { 0 , 2 , 6 , 8 , 9 } ; mergeArray ( arr1 , arr2 ) ; } }


"Efficiently merging two sorted arrays with O ( 1 ) extra space | C # program for Merging two sorted arrays with O ( 1 ) extra space ; Function to find next gap . ; comparing elements in the first array . ; comparing elements in both arrays . ; comparing elements in the second array . ; Driver code ; Function Call"
Solution in C#:

using System ; class GFG { static int nextGap ( int gap ) { if ( gap <= 1 ) return 0 ; return ( gap / 2 ) + ( gap % 2 ) ; } private static void merge ( int [ ] arr1 , int [ ] arr2 , int n , int m ) { int i , j , gap = n + m ; for ( gap = nextGap ( gap ) ; gap > 0 ; gap = nextGap ( gap ) ) { for ( i = 0 ; i + gap < n ; i ++ ) if ( arr1 [ i ] > arr1 [ i + gap ] ) { int temp = arr1 [ i ] ; arr1 [ i ] = arr1 [ i + gap ] ; arr1 [ i + gap ] = temp ; } for ( j = gap > n ? gap - n : 0 ; i < n && j < m ; i ++ , j ++ ) if ( arr1 [ i ] > arr2 [ j ] ) { int temp = arr1 [ i ] ; arr1 [ i ] = arr2 [ j ] ; arr2 [ j ] = temp ; } if ( j < m ) { for ( j = 0 ; j + gap < m ; j ++ ) if ( arr2 [ j ] > arr2 [ j + gap ] ) { int temp = arr2 [ j ] ; arr2 [ j ] = arr2 [ j + gap ] ; arr2 [ j + gap ] = temp ; } } } } public static void Main ( ) { int [ ] a1 = { 10 , 27 , 38 , 43 , 82 } ; int [ ] a2 = { 3 , 9 } ; merge ( a1 , a2 , a1 . Length , a2 . Length ) ; Console . Write ( " First ▁ Array : ▁ " ) ; for ( int i = 0 ; i < a1 . Length ; i ++ ) { Console . Write ( a1 [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; Console . Write ( " Second ▁ Array : ▁ " ) ; for ( int i = 0 ; i < a2 . Length ; i ++ ) { Console . Write ( a2 [ i ] + " ▁ " ) ; } } }


"Egg Dropping Puzzle with 2 Eggs and K Floors | C # program to find optimal number of trials for k floors and 2 eggs . ; Driver code"
Solution in C#:

class GFG { static int twoEggDrop ( int k ) { return ( int ) System . Math . Ceiling ( ( - 1.0 + System . Math . Sqrt ( 1 + 8 * k ) ) / 2.0 ) ; } public static void Main ( ) { int k = 100 ; System . Console . WriteLine ( twoEggDrop ( k ) ) ; } }


"Egg Dropping Puzzle | DP |  ; Function to get minimum number of trials needed in worst case with n eggs and k floors ; If there are no floors , then no trials needed . OR if there is one floor , one trial needed . ; We need k trials for one egg and k floors ; Consider all droppings from 1 st floor to kth floor and return the minimum of these values plus 1. ; Driver code"
Solution in C#:

using System ; class GFG { static int eggDrop ( int n , int k ) { if ( k == 1 k == 0 ) return k ; if ( n == 1 ) return k ; int min = int . MaxValue ; int x , res ; for ( x = 1 ; x <= k ; x ++ ) { res = Math . Max ( eggDrop ( n - 1 , x - 1 ) , eggDrop ( n , k - x ) ) ; if ( res < min ) min = res ; } return min + 1 ; } static void Main ( ) { int n = 2 , k = 10 ; Console . Write ( " Minimum ▁ number ▁ of ▁ " + " trials ▁ in ▁ worst ▁ case ▁ with ▁ " + n + " ▁ eggs ▁ and ▁ " + k + " ▁ floors ▁ is ▁ " + eggDrop ( n , k ) ) ; } }


"Egg Dropping Puzzle | DP | A Dynamic Programming based C # Program for the Egg Dropping Puzzle ; A utility function to get maximum of two integers ; Function to get minimum number of trials needed in worst case with n eggs and k floors ; A 2D table where entry eggFloor [ i ] [ j ] will represent minimum number of trials needed for i eggs and j floors . ; We need one trial for one floor and0 trials for 0 floors ; We always need j trials for one egg and j floors . ; Fill rest of the entries in table using optimal substructure property ; eggFloor [ n ] [ k ] holds the result ; Driver function"
Solution in C#:

using System ; class GFG { static int max ( int a , int b ) { return ( a > b ) ? a : b ; } static int eggDrop ( int n , int k ) { int [ , ] eggFloor = new int [ n + 1 , k + 1 ] ; int res ; int i , j , x ; for ( i = 1 ; i <= n ; i ++ ) { eggFloor [ i , 1 ] = 1 ; eggFloor [ i , 0 ] = 0 ; } for ( j = 1 ; j <= k ; j ++ ) eggFloor [ 1 , j ] = j ; for ( i = 2 ; i <= n ; i ++ ) { for ( j = 2 ; j <= k ; j ++ ) { eggFloor [ i , j ] = int . MaxValue ; for ( x = 1 ; x <= j ; x ++ ) { res = 1 + max ( eggFloor [ i - 1 , x - 1 ] , eggFloor [ i , j - x ] ) ; if ( res < eggFloor [ i , j ] ) eggFloor [ i , j ] = res ; } } } return eggFloor [ n , k ] ; } public static void Main ( ) { int n = 2 , k = 36 ; Console . WriteLine ( " Minimum ▁ number ▁ of ▁ trials ▁ " + " in ▁ worst ▁ case ▁ with ▁ " + n + " ▁ eggs ▁ and ▁ " + k + " floors ▁ is ▁ " + eggDrop ( n , k ) ) ; } }


"Eggs dropping puzzle ( Binomial Coefficient and Binary Search Solution ) | C # program to find minimum number of trials in worst case . ; Find sum of binomial coefficients xCi ( where i varies from 1 to n ) . If the sum becomes more than K ; Do binary search to find minimum number of trials in worst case . ; Initialize low and high as 1 st and last floors ; Do binary search , for every mid , find sum of binomial coefficients and check if the sum is greater than k or not . ; Driver Code"
Solution in C#:

using System ; class GFG { static int binomialCoeff ( int x , int n , int k ) { int sum = 0 , term = 1 ; for ( int i = 1 ; i <= n && sum < k ; ++ i ) { term *= x - i + 1 ; term /= i ; sum += term ; } return sum ; } static int minTrials ( int n , int k ) { int low = 1 , high = k ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( binomialCoeff ( mid , n , k ) < k ) low = mid + 1 ; else high = mid ; } return low ; } public static void Main ( ) { Console . WriteLine ( minTrials ( 2 , 10 ) ) ; } }


"Eggs dropping puzzle | Set 2 | C # implementation of the approach ; Function to return the minimum number of trials needed in the worst case with n eggs and k floors ; Fill all the entries in table using optimal substructure property ; Return the minimum number of moves ; Driver code"
Solution in C#:

using System ; class GFG { static int eggDrop ( int n , int k ) { int [ , ] dp = new int [ k + 1 , n + 1 ] ; int x = 0 ; while ( dp [ x , n ] < k ) { x ++ ; for ( int i = 1 ; i <= n ; i ++ ) dp [ x , i ] = dp [ x - 1 , i - 1 ] + dp [ x - 1 , i ] + 1 ; } return x ; } public static void Main ( String [ ] args ) { int n = 2 , k = 36 ; Console . WriteLine ( eggDrop ( n , k ) ) ; } }


"Element equal to the sum of all the remaining elements | C # implementation of the above approach ; Function to find the element ; sum is use to store sum of all elements of array ; iterate over all elements ; Driver code"
Solution in C#:

using System ; class GFG { static int findEle ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == sum - arr [ i ] ) return arr [ i ] ; return - 1 ; } static public void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 6 } ; int n = arr . Length ; Console . WriteLine ( findEle ( arr , n ) ) ; } }


"Elements greater than the previous and next element in an Array | c # program to print elements greater than the previous and next element in an Array ; Function to print elements greater than the previous and next element in an Array ; Traverse array from index 1 to n - 2 and check for the given condition ; Driver Code"
Solution in C#:

using System ; public class GfG { public static void printElements ( int [ ] arr , int n ) { for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] && arr [ i ] > arr [ i + 1 ] ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 2 , 3 , 1 , 5 , 4 , 9 , 8 , 7 , 5 } ; int n = arr . Length ; printElements ( arr , n ) ; } }


"Elements of Array which can be expressed as power of prime numbers | C # program to print all elements of Array which can be expressed as power of prime numbers ; Function to mark all the exponent of prime numbers ; If number is prime then marking all of its exponent true ; Function to display all required elements ; Function to print the required numbers ; To find the largest number ; Function call to mark all the Exponential prime nos . ; Function call ; Driver code"
Solution in C#:

using System ; class GFG { static void ModifiedSieveOfEratosthenes ( int N , bool [ ] Expo_Prime ) { bool [ ] primes = new bool [ N ] ; for ( int i = 0 ; i < N ; i ++ ) primes [ i ] = true ; for ( int i = 2 ; i < N ; i ++ ) { if ( primes [ i ] ) { int no = i ; while ( no <= N ) { Expo_Prime [ no ] = true ; no *= i ; } for ( int j = i * i ; j < N ; j += i ) primes [ j ] = false ; } } } static void Display ( int [ ] arr , bool [ ] Expo_Prime , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( Expo_Prime [ arr [ i ] ] ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void FindExpoPrime ( int [ ] arr , int n ) { int max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( max < arr [ i ] ) max = arr [ i ] ; } bool [ ] Expo_Prime = new bool [ max + 1 ] ; ModifiedSieveOfEratosthenes ( max + 1 , Expo_Prime ) ; Display ( arr , Expo_Prime , n ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 6 , 9 , 16 , 1 , 3 , 12 , 36 , 625 , 1000 } ; int n = arr . Length ; FindExpoPrime ( arr , n ) ; } }


"Elements of Array which can be expressed as power of some integer to given exponent K | C # implementation to print elements of the Array which can be expressed as power of some integer to given exponent K ; Method returns Nth power of A ; Smaller eps , denotes more accuracy ; Initializing difference between two roots by int . MaxValue ; x ^ K denotes current value of x ; loop untill we reach desired accuracy ; calculating current value from previous value by newton 's method ; Function to check whether its k root is an integer or not ; Function to find the numbers ; Driver code"
Solution in C#:

using System ; class GFG { static double nthRoot ( long A , long N ) { double xPre = 7 ; double eps = 1e-3 ; double delX = int . MaxValue ; double xK = 0 ; while ( delX > eps ) { xK = ( ( N - 1.0 ) * xPre + ( double ) A / Math . Pow ( xPre , N - 1 ) ) / ( double ) N ; delX = Math . Abs ( xK - xPre ) ; xPre = xK ; } return xK ; } static bool check ( long no , int k ) { double kth_root = nthRoot ( no , k ) ; long num = ( long ) kth_root ; if ( Math . Abs ( num - kth_root ) < 1e-4 ) return true ; return false ; } static void printExpo ( long [ ] arr , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( check ( arr [ i ] , k ) ) Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int K = 6 ; long [ ] arr = { 46656 , 64 , 256 , 729 , 16 , 1000 } ; int n = arr . Length ; printExpo ( arr , n , K ) ; } }


"Elements that occurred only once in the array | C # implementation of above approach ; Function to find the elements that appeared only once in the array ; Sort the array ; Check for first element ; Check for all the elements if it is different its adjacent elements ; Check for the last element ; Driver code"
Solution in C#:

using System ; class GFG { static void occurredOnce ( int [ ] arr , int n ) { Array . Sort ( arr ) ; if ( arr [ 0 ] != arr [ 1 ] ) Console . Write ( arr [ 0 ] + " ▁ " ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) if ( arr [ i ] != arr [ i + 1 ] && arr [ i ] != arr [ i - 1 ] ) Console . Write ( arr [ i ] + " ▁ " ) ; if ( arr [ n - 2 ] != arr [ n - 1 ] ) Console . Write ( arr [ n - 1 ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 } ; int n = arr . Length ; occurredOnce ( arr , n ) ; } }


"Elements that occurred only once in the array | C # implementation to find elements that appeared only once ; Function to find the elements that appeared only once in the array ; Check if the first and last element is equal . If yes , remove those elements ; Start traversing the remaining elements ; Check if current element is equal to the element at immediate previous index If yes , check the same for next element ; Else print the current element ; Check for the last element ; Driver code"
Solution in C#:

using System ; class GFG { static void occurredOnce ( int [ ] arr , int n ) { int i = 1 , len = n ; if ( arr [ 0 ] == arr [ len - 1 ] ) { i = 2 ; len -- ; } for ( ; i < n ; i ++ ) if ( arr [ i ] == arr [ i - 1 ] ) i ++ ; else Console . Write ( arr [ i - 1 ] + " ▁ " ) ; if ( arr [ n - 1 ] != arr [ 0 ] && arr [ n - 1 ] != arr [ n - 2 ] ) Console . Write ( arr [ n - 1 ] ) ; } public static void Main ( ) { int [ ] arr = { 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 } ; int n = arr . Length ; occurredOnce ( arr , n ) ; } }


"Elements that occurred only once in the array | C # implementation to find elements that appeared only once ; Function to find the elements that appeared only once in the array ; Store all the elements in the map with their occurrence ; Traverse the map and print all the elements with occurrence 1 ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void occurredOnce ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) mp [ arr [ i ] ] = 1 + mp [ arr [ i ] ] ; else mp . Add ( arr [ i ] , 1 ) ; } foreach ( KeyValuePair < int , int > entry in mp ) { if ( Int32 . Parse ( String . Join ( " " , entry . Value ) ) == 1 ) Console . Write ( entry . Key + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 } ; int n = arr . Length ; occurredOnce ( arr , n ) ; } }


"Elements to be added so that all elements of a range are present in array | C # implementation of the approach ; Function to count numbers to be added ; Make a hash of elements and store minimum and maximum element ; Traverse all elements from minimum to maximum and count if it is not in the hash ; Drivers code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countNum ( int [ ] arr , int n ) { HashSet < int > s = new HashSet < int > ( ) ; int count = 0 , maxm = int . MinValue , minm = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { s . Add ( arr [ i ] ) ; if ( arr [ i ] < minm ) minm = arr [ i ] ; if ( arr [ i ] > maxm ) maxm = arr [ i ] ; } for ( int i = minm ; i <= maxm ; i ++ ) if ( ! s . Contains ( i ) ) count ++ ; return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 5 , 8 , 6 } ; int n = arr . Length ; Console . WriteLine ( countNum ( arr , n ) ) ; } }


"Elements to be added so that all elements of a range are present in array | C # program for above implementation ; Function to count numbers to be added ; Sort the array ; Check if elements are consecutive or not . If not , update count ; Drivers code"
Solution in C#:

using System ; public class GFG { static int countNum ( int [ ] arr , int n ) { int count = 0 ; Array . Sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] != arr [ i + 1 ] && arr [ i ] != arr [ i + 1 ] - 1 ) count += arr [ i + 1 ] - arr [ i ] - 1 ; return count ; } static public void Main ( ) { int [ ] arr = { 3 , 5 , 8 , 6 } ; int n = arr . Length ; Console . WriteLine ( countNum ( arr , n ) ) ; } }


"Emirp numbers | C # program to print Emirp numbers less than n ; Function to find reverse of any number ; Sieve method used for generating emirp number ( use of sieve of Eratosthenes ) ; Create a boolean 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 ; Traverse all prime numbers ; Find reverse a number ; A number is emrip if it is not a palindrome number and its reverse is also prime . ; Mark reverse prime as false so that it 's not printed again ; Driver code"
Solution in C#:

using System ; class GFG { static int reverse ( int x ) { int rev = 0 ; while ( x > 0 ) { rev = ( rev * 10 ) + x % 10 ; x = x / 10 ; } return rev ; } static void printEmirp ( int n ) { bool [ ] prime = new bool [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; 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 ; } } for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) { int rev = reverse ( p ) ; if ( p != rev && rev <= n && prime [ rev ] ) { Console . Write ( p + " ▁ " + rev + " ▁ " ) ; prime [ rev ] = false ; } } } } public static void Main ( ) { int n = 100 ; printEmirp ( n ) ; } }


"Encoding a word into Pig Latin | C # program to encode a word to a Pig Latin . ; the index of the first vowel is stored . ; Pig Latin is possible only if vowels is present ; Take all characters after index ( including index ) . Append all characters which are before index . Finally append " ay " ; Driver code"
Solution in C#:

using System ; class GFG { static bool isVowel ( char c ) { return ( c == ' A ' c == ' E ' c == ' I ' c == ' O ' c == ' U ' c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) ; } static string pigLatin ( string s ) { int len = s . Length ; int index = - 1 ; for ( int i = 0 ; i < len ; i ++ ) { if ( isVowel ( s [ i ] ) ) { index = i ; break ; } } if ( index == - 1 ) return " - 1" ; return s . Substring ( index ) + s . Substring ( 0 , index ) + " ay " ; } public static void Main ( ) { string str = pigLatin ( " graphic " ) ; if ( str == " - 1" ) Console . WriteLine ( " No ▁ vowels " + " found . ▁ Pig ▁ Latin " + " ▁ not ▁ possible " ) ; else Console . WriteLine ( str ) ; } }


"Encryption and Decryption of String according to given technique | C # implementation for Custom Encryption and Decryption of String ; Function to encrypt the String ; Matrix to generate the Encrypted String ; Fill the matrix row - wise ; Loop to generate encrypted String ; Function to decrypt the String ; Matrix to generate the Encrypted String ; Fill the matrix column - wise ; Loop to generate decrypted String ; Driver Code ; Encryption of String ; Decryption of String"
Solution in C#:

 using System ; class GFG { static String encryption ( char [ ] s ) { int l = s . Length ; int b = ( int ) Math . Ceiling ( Math . Sqrt ( l ) ) ; int a = ( int ) Math . Floor ( Math . Sqrt ( l ) ) ; String encrypted = " " ; if ( b * a < l ) { if ( Math . Min ( b , a ) == b ) { b = b + 1 ; } else { a = a + 1 ; } } char [ , ] arr = new char [ a , b ] ; int k = 0 ; for ( int j = 0 ; j < a ; j ++ ) { for ( int i = 0 ; i < b ; i ++ ) { if ( k < l ) { arr [ j , i ] = s [ k ] ; } k ++ ; } } for ( int j = 0 ; j < b ; j ++ ) { for ( int i = 0 ; i < a ; i ++ ) { encrypted = encrypted + arr [ i , j ] ; } } return encrypted ; } static String decryption ( char [ ] s ) { int l = s . Length ; int b = ( int ) Math . Ceiling ( Math . Sqrt ( l ) ) ; int a = ( int ) Math . Floor ( Math . Sqrt ( l ) ) ; String decrypted = " " ; char [ , ] arr = new char [ a , b ] ; int k = 0 ; for ( int j = 0 ; j < b ; j ++ ) { for ( int i = 0 ; i < a ; i ++ ) { if ( k < l ) { arr [ j , i ] = s [ k ] ; } k ++ ; } } for ( int j = 0 ; j < a ; j ++ ) { for ( int i = 0 ; i < b ; i ++ ) { decrypted = decrypted + arr [ i , j ] ; } } return decrypted ; } public static void Main ( String [ ] args ) { String s = " Geeks ▁ For ▁ Geeks " ; String encrypted ; String decrypted ; encrypted = encryption ( s . ToCharArray ( ) ) ; Console . Write ( encrypted + "
 " ) ; decrypted = decryption ( encrypted . ToCharArray ( ) ) ; Console . Write ( decrypted ) ; } }


"Enlightened Numbers | C # implementation of the above approach ; Function to check if N is a Composite Number ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to return concatenation of distinct prime factors of a given number n ; Handle prime factor 2 explicitly so that can optimally handle other prime factors . ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Function to check if a number is is an enlightened number ; Number should not be prime ; Converting N to string ; Function call ; Driver code"
Solution in C#:

using System ; class GFG { static bool isComposite ( int n ) { if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } static String concatenatePrimeFactors ( int n ) { String concatenate = " " ; if ( n % 2 == 0 ) { concatenate += "2" ; while ( n % 2 == 0 ) n = n / 2 ; } for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { concatenate += i ; while ( n % i == 0 ) n = n / i ; } } if ( n > 2 ) concatenate += n ; return concatenate ; } static bool isEnlightened ( int N ) { if ( ! isComposite ( N ) ) return false ; String num = String . Join ( " " , N ) ; String prefixConc = concatenatePrimeFactors ( N ) ; return num . StartsWith ( prefixConc ) ; } public static void Main ( String [ ] args ) { int n = 250 ; if ( isEnlightened ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Enneacontagon Number | C # program for above approach ; Finding the nth enneacontagon number ; Driver code"
Solution in C#:

using System ; class GFG { public static int enneacontagonNum ( int n ) { return ( 88 * n * n - 86 * n ) / 2 ; } public static void Main ( String [ ] args ) { int n = 3 ; Console . WriteLine ( "3rd ▁ enneacontagon ▁ Number ▁ is ▁ = ▁ " + enneacontagonNum ( n ) ) ; } }


"Entringer Number | C # Code For Entringer Number ; Return Entringer Number E ( n , k ) ; Base Case ; Base Case ; Recursive step ; Driver program to test above function"
Solution in C#:

using System ; class GFG { static int zigzag ( int n , int k ) { if ( n == 0 && k == 0 ) return 1 ; if ( k == 0 ) return 0 ; return zigzag ( n , k - 1 ) + zigzag ( n - 1 , n - k ) ; } public static void Main ( ) { int n = 4 , k = 3 ; Console . WriteLine ( zigzag ( n , k ) ) ; } }


"Entringer Number | C # Code For Entringer Number ; Return Entringer Number E ( n , k ) ; Base cases ; Finding dp [ i ] [ j ] ; Driver program to test above function"
Solution in C#:

using System ; class GFG { static int zigzag ( int n , int k ) { int [ , ] dp = new int [ n + 1 , k + 1 ] ; dp [ 0 , 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) dp [ i , 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= Math . Min ( i , k ) ; j ++ ) dp [ i , j ] = dp [ i , j - 1 ] + dp [ i - 1 , i - j ] ; } return dp [ n , k ] ; } public static void Main ( ) { int n = 4 , k = 3 ; Console . WriteLine ( zigzag ( n , k ) ) ; } }


"Equal Sum and XOR of three Numbers | C # implementation of the approach ; Function to calculate power of 3 ; Function to return the count of the unset bit ( zeros ) ; Check the bit is 0 or not ; Right shifting ( dividing by 2 ) ; Driver Code"
Solution in C#:

using System ; class GFG { static long calculate ( int bit_cnt ) { long res = 1 ; while ( bit_cnt -- > 0 ) { res = res * 3 ; } return res ; } static int unset_bit_count ( long n ) { int count = 0 ; while ( n > 0 ) { if ( ( n & 1 ) == 0 ) count ++ ; n = n >> 1 ; } return count ; } public static void Main ( String [ ] args ) { long n ; n = 2 ; int count = unset_bit_count ( n ) ; long ans = calculate ( count ) ; Console . WriteLine ( ans ) ; } }