"Pair of strings having longest common prefix of maximum length in given array | C # program for the above approach ; class of Trie ; Function to insert a string into Trie ; Stores length of the string ; Traverse the string str ; If str [ i ] is not present in current path of Trie ; Create a new node of Trie ; Update root ; Function to find the maximum length of longest common prefix in Trie with str ; Stores length of str ; Stores length of longest common prefix in Trie with str ; Traverse the string str ; If str [ i ] is present in the current path of Trie ; Update len ; Update root ; Function to print the pair having maximum length of the longest common prefix ; Stores index of the string having maximum length of longest common prefix ; Stores maximum length of longest common prefix . ; Create root node of Trie ; Insert arr [ 0 ] into Trie ; Traverse the array . ; Stores maximum length of longest common prefix in Trie with arr [ i ] ; If temp is greater than len ; Update len ; Update idx ; Traverse array arr [ ] ; Stores length of arr [ i ] ; Check if maximum length of longest common prefix > M ; Traverse string arr [ i ] and arr [ j ] ; If current character of both string does not match . ; Print pairs having maximum length of the longest common prefix ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { public class TrieNode { public TrieNode [ ] child = new TrieNode [ 256 ] ; public TrieNode ( ) { } } ; public static void insertTrie ( TrieNode root , String str ) { int M = str . Length ; for ( int i = 0 ; i < M ; i ++ ) { if ( root . child [ str [ i ] ] == null ) { root . child [ str [ i ] ] = new TrieNode ( ) ; } root = root . child [ str [ i ] ] ; } } public static int findStrLen ( TrieNode root , String str ) { int M = str . Length ; int len = 0 ; for ( int i = 0 ; i < M ; i ++ ) { if ( root . child [ str [ i ] ] != null ) { len ++ ; root = root . child [ str [ i ] ] ; } else { return len ; } } return len ; } public static void findMaxLenPair ( List < string > arr , int N ) { int idx = - 1 ; int len = 0 ; TrieNode root = new TrieNode ( ) ; insertTrie ( root , arr [ 0 ] ) ; for ( int i = 1 ; i < N ; i ++ ) { int temp = findStrLen ( root , arr [ i ] ) ; if ( temp > len ) { len = temp ; idx = i ; } insertTrie ( root , arr [ i ] ) ; } for ( int i = 0 ; i < N ; i ++ ) { int M = arr [ i ] . Length ; if ( i != idx && M >= len ) { bool found = true ; for ( int j = 0 ; j < len ; j ++ ) { if ( arr [ i ] [ j ] != arr [ idx ] [ j ] ) { found = false ; break ; } } if ( found ) { Console . WriteLine ( " ( " + arr [ i ] + " , ▁ " + arr [ idx ] + " ) " ) ; return ; } } } } public static void Main ( ) { List < string > arr = new List < string > ( ) { " geeksforgeeks " , " geeks " , " geeksforcse " } ; int N = arr . Count ; findMaxLenPair ( arr , N ) ; } }


"Pair with given product | Set 1 ( Find if any pair exists ) | C # program if there exists a pair for given product ; Returns true if there is a pair in arr [ 0. . n - 1 ] with product equal to x . ; Create an empty set and insert first element into it ; Traverse remaining elements ; 0 case must be handles explicitly as x % 0 is undefined ; x / arr [ i ] exists in hash , then we found a pair ; Insert arr [ i ] ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static bool isProduct ( int [ ] arr , int n , int x ) { HashSet < int > hset = new HashSet < int > ( ) ; if ( n < 2 ) { return false ; } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { if ( x == 0 ) { return true ; } else { continue ; } } if ( x % arr [ i ] == 0 ) { if ( hset . Contains ( x / arr [ i ] ) ) { return true ; } hset . Add ( arr [ i ] ) ; } } return false ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 10 , 20 , 9 , 40 } ; int x = 400 ; int n = arr . Length ; if ( isProduct ( arr , arr . Length , x ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } x = 190 ; if ( isProduct ( arr , arr . Length , x ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }


"Pair with given product | Set 1 ( Find if any pair exists ) | C # program to find if there is a pair with given product . ; Returns true if there is a pair in arr [ 0. . n - 1 ] with product equal to x . ; Consider all possible pairs and check for every pair . ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isProduct ( int [ ] arr , int n , int x ) { for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] * arr [ j ] == x ) return true ; return false ; } static void Main ( ) { int [ ] arr = { 10 , 20 , 9 , 40 } ; int x = 400 ; int n = arr . Length ; if ( isProduct ( arr , n , x ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; x = 190 ; if ( isProduct ( arr , n , x ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Pair with given sum and maximum shortest distance from end | C # code to find maximum shortest distance from endpoints ; Store counts of all elements . ; function to find maximum shortest distance ; stores the shortest distance of every element in original array . ; shortest distance from ends ; if duplicates are found , b [ x ] is replaced with minimum of the previous and current position 's  shortest distance ; similar elements ignore them cause we need distinct elements ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void makePermutation ( int [ ] a , int n ) { Dictionary < int , int > count = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( count . ContainsKey ( a [ i ] ) ) { count [ a [ i ] ] = count [ a [ i ] ] + 1 ; } else { count . Add ( a [ i ] , 1 ) ; } } } static int find_maximum ( int [ ] a , int n , int k ) { Dictionary < int , int > b = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; int d = Math . Min ( 1 + i , n - i ) ; if ( ! b . ContainsKey ( x ) ) b . Add ( x , d ) ; else { b [ x ] = Math . Min ( d , b [ x ] ) ; } } int ans = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; if ( x != k - x && b . ContainsKey ( k - x ) ) ans = Math . Min ( Math . Max ( b [ x ] , b [ k - x ] ) , ans ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] a = { 3 , 5 , 8 , 6 , 7 } ; int K = 11 ; int n = a . Length ; Console . WriteLine ( find_maximum ( a , n , K ) ) ; } }


"Pair with largest sum which is less than K in the array | C # program to find pair with largest sum which is less than K in the array ; Function to find pair with largest sum which is less than K in the array ; To store the break point ; Sort the given array ; Find the break point ; No need to look beyond i 'th index ; Find the required pair ; Print the required answer ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static void Max_Sum ( int [ ] arr , int n , int k ) { int p = n ; Array . Sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= k ) { p = i ; break ; } } int maxsum = 0 , a = 0 , b = 0 ; for ( int i = 0 ; i < p ; i ++ ) { for ( int j = i + 1 ; j < p ; j ++ ) { if ( arr [ i ] + arr [ j ] < k && arr [ i ] + arr [ j ] > maxsum ) { maxsum = arr [ i ] + arr [ j ] ; a = arr [ i ] ; b = arr [ j ] ; } } } Console . WriteLine ( a + " ▁ " + b ) ; } public static void Main ( ) { int [ ] arr = { 5 , 20 , 110 , 100 , 10 } ; int k = 85 ; int n = arr . Length ; Max_Sum ( arr , n , k ) ; } }


"Pair with maximum GCD from two arrays | C # program to find maximum GCD pair from two arrays ; Find the maximum GCD pair with maximum sum ; array to keep a count of existing elements ; first [ i ] and second [ i ] are going to store maximum multiples of i in a [ ] and b [ ] respectively . ; traverse through the first array to mark the elements in cnt ; Find maximum multiple of every number in first array ; Find maximum multiple of every number in second array . We re - initialise cnt [ ] and traverse through the second array to mark the elements in cnt ; if the multiple is present in the second array then store the max of number or the pre - existing element ; traverse for every elements and checks the maximum N that is present in both the arrays ; Driver Code ; Maximum possible value of elements in both arrays ."
Solution in C#:

using System ; class GFG { static void gcdMax ( int [ ] a , int [ ] b , int n , int N ) { int [ ] cnt = new int [ N ] ; int [ ] first = new int [ N ] ; int [ ] second = new int [ N ] ; for ( int i = 0 ; i < n ; ++ i ) cnt [ a [ i ] ] = 1 ; for ( int i = 1 ; i < N ; ++ i ) for ( int j = i ; j < N ; j += i ) if ( cnt [ j ] > 0 ) first [ i ] = Math . Max ( first [ i ] , j ) ; cnt = new int [ N ] ; for ( int i = 0 ; i < n ; ++ i ) cnt [ b [ i ] ] = 1 ; for ( int i = 1 ; i < N ; ++ i ) for ( int j = i ; j < N ; j += i ) if ( cnt [ j ] > 0 ) second [ i ] = Math . Max ( second [ i ] , j ) ; int x ; for ( x = N - 1 ; x >= 0 ; x -- ) if ( first [ x ] > 0 && second [ x ] > 0 ) break ; Console . WriteLine ( first [ x ] + " ▁ " + second [ x ] ) ; } static int Main ( ) { int [ ] a = { 3 , 1 , 4 , 2 , 8 } ; int [ ] b = { 5 , 2 , 12 , 8 , 3 } ; int n = a . Length ; int N = 20 ; gcdMax ( a , b , n , N ) ; return 0 ; } }


"Pair with min absolute difference and whose product is N + 1 or N + 2 | C # program for the above approach ; Function to print pair ( a , b ) such that a * b = N + 1 or N + 2 ; Loop to iterate over the desired possible values ; Check for condition 1 ; Check for condition 2 ; Driver Code ; Given Number ; Function Call"
Solution in C#:

using System ; class GFG { static void closestDivisors ( int n ) { for ( int i = ( int ) Math . Sqrt ( n + 2 ) ; i > 0 ; i -- ) { if ( ( n + 1 ) % i == 0 ) { Console . Write ( i + " , ▁ " + ( n + 1 ) / i ) ; break ; } if ( ( n + 2 ) % i == 0 ) { Console . Write ( i + " , ▁ " + ( n + 2 ) / i ) ; break ; } } } public static void Main ( string [ ] args ) { int N = 123 ; closestDivisors ( N ) ; } }


"Pairs from an array that satisfy the given condition | C # implementation of the approach ; Function to return the number of set bits in n ; Function to return the count of required pairs ; Set bits for first element of the pair ; Set bits for second element of the pair ; Set bits of the resultant number which is the XOR of both the elements of the pair ; If the condition is satisfied ; Increment the count ; Return the total count ; Driver code"
Solution in C#:

using System ; class GFG { static int setBits ( int n ) { int count = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } static int countPairs ( int [ ] a , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int setbits_x = setBits ( a [ i ] ) ; for ( int j = i + 1 ; j < n ; j ++ ) { int setbits_y = setBits ( a [ j ] ) ; int setbits_xor_xy = setBits ( a [ i ] ^ a [ j ] ) ; if ( setbits_x + setbits_y == setbits_xor_xy ) count ++ ; } } return count ; } public static void Main ( ) { int [ ] a = { 2 , 3 , 4 , 5 , 6 } ; int n = a . Length ; Console . Write ( countPairs ( a , n ) ) ; } }


"Pairs of Amicable Numbers | Efficient C # program to count Amicable pairs in an array . ; Calculate the sum of proper divisors ; 1 is a proper divisor ; To handle perfect squares ; Check if pair is amicable ; This function prints count of amicable pairs present in the input array ; Map to store the numbers ; Iterate through each number , and find the sum of proper divisors and check if it 's  also present in the array ; It 's sum of proper divisors ; As the pairs are counted twice , thus divide by 2 ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int sumOfDiv ( int x ) { int sum = 1 ; for ( int i = 2 ; i <= Math . Sqrt ( x ) ; i ++ ) { if ( x % i == 0 ) { sum += i ; if ( x / i != i ) sum += x / i ; } } return sum ; } static Boolean isAmicable ( int a , int b ) { return ( sumOfDiv ( a ) == b && sumOfDiv ( b ) == a ) ; } static int countPairs ( int [ ] arr , int n ) { HashSet < int > s = new HashSet < int > ( ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) s . Add ( arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . Contains ( sumOfDiv ( arr [ i ] ) ) ) { int sum = sumOfDiv ( arr [ i ] ) ; if ( isAmicable ( arr [ i ] , sum ) ) count ++ ; } } return count / 2 ; } public static void Main ( String [ ] args ) { int [ ] arr1 = { 220 , 284 , 1184 , 1210 , 2 , 5 } ; int n1 = arr1 . Length ; Console . WriteLine ( countPairs ( arr1 , n1 ) ) ; int [ ] arr2 = { 2620 , 2924 , 5020 , 5564 , 6232 , 6368 } ; int n2 = arr2 . Length ; Console . WriteLine ( countPairs ( arr2 , n2 ) ) ; } }


"Pairs of Positive Negative values in an array | C # program to find pairs of positive and negative values present in an array . ; Print pair with negative and positive value ; For each element of array . ; Try to find the negative value of arr [ i ] from i + 1 to n ; If absolute values are equal print pair . ; If size of vector is 0 , therefore there is no element with positive negative value , print "0" ; Sort the vector ; Print the pair with negative positive value . ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static void printPairs ( int [ ] arr , int n ) { List < int > v = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( Math . Abs ( arr [ i ] ) == Math . Abs ( arr [ j ] ) ) v . Add ( Math . Abs ( arr [ i ] ) ) ; if ( v . Count == 0 ) return ; v . Sort ( ) ; for ( int i = 0 ; i < v . Count ; i ++ ) Console . Write ( - v [ i ] + " ▁ " + v [ i ] ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 } ; int n = arr . Length ; printPairs ( arr , n ) ; } }


"Pairs of strings which on concatenating contains each character of " string " | C # implementation of the above approach ; Function to return the bitmask for the string ; Function to return the count of pairs ; bitMask [ i ] will store the count of strings from the array whose bitmask is i ; To store the count of pairs ; MAX - 1 = 63 i . e . 111111 in binary ; arr [ i ] cannot make s pair with itself i . e . ( arr [ i ] , arr [ i ] ) ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MAX = 64 ; static int getBitmask ( char [ ] s ) { int temp = 0 ; for ( int j = 0 ; j < s . Length ; j ++ ) { switch ( s [ j ] ) { case ' s ' : temp = temp | ( 1 ) ; break ; case ' t ' : temp = temp | ( 2 ) ; break ; case ' r ' : temp = temp | ( 4 ) ; break ; case ' i ' : temp = temp | ( 8 ) ; break ; case ' n ' : temp = temp | ( 16 ) ; break ; case ' g ' : temp = temp | ( 32 ) ; break ; default : break ; } } return temp ; } static int countPairs ( String [ ] arr , int n ) { int [ ] bitMask = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) bitMask [ getBitmask ( arr [ i ] . ToCharArray ( ) ) ] ++ ; int cnt = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = i ; j < MAX ; j ++ ) { if ( ( i j ) == ( MAX - 1 ) ) { if ( i == j ) cnt += ( ( bitMask [ i ] * bitMask [ i ] - 1 ) / 2 ) ; else cnt += ( bitMask [ i ] * bitMask [ j ] ) ; } } } return cnt ; } public static void Main ( String [ ] args ) { String [ ] arr = { " strrr " , " string " , " gstrin " } ; int n = arr . Length ; Console . WriteLine ( countPairs ( arr , n ) ) ; } }


"Pairs such that one is a power multiple of other | C # program to find pairs count ; function to count the required pairs ; sort the given array ; for each A [ i ] traverse rest array ; count Aj such that Ai * k ^ x = Aj ; increase x till Ai * k ^ x <= largest element ; Driver program"
Solution in C#:

using System ; class GFG { static int countPairs ( int [ ] A , int n , int k ) { int ans = 0 ; Array . Sort ( A ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int x = 0 ; while ( ( A [ i ] * Math . Pow ( k , x ) ) <= A [ j ] ) { if ( ( A [ i ] * Math . Pow ( k , x ) ) == A [ j ] ) { ans ++ ; break ; } x ++ ; } } } return ans ; } public static void Main ( ) { int [ ] A = { 3 , 8 , 9 , 12 , 18 , 4 , 24 , 2 , 6 } ; int n = A . Length ; int k = 3 ; Console . WriteLine ( countPairs ( A , n , k ) ) ; } }


"Pairs whose concatenation contain all digits | C # Program to find number of pairs whose concatenation contains all digits from 0 to 9. ; Function to return number of pairs whose concatenation contain all digits from 0 to 9 ; making the mask for each of the number . ; for each of the possible pair which can make OR value equal to 1023 ; finding the count of pair from the given numbers . ; Driver Code"
Solution in C#:

 using System ; class GFG { static readonly int N = 20 ; static int countPair ( String [ ] str , int n ) { int [ ] cnt = new int [ 1 << 10 ] ; for ( int i = 0 ; i < n ; i ++ ) { int mask = 0 ; for ( int j = 0 ; j < str [ i ] . Length ; ++ j ) mask |= ( 1 << ( str [ i ] [ j ] - '0' ) ) ; cnt [ mask ] ++ ; } int ans = 0 ; for ( int m1 = 0 ; m1 <= 1023 ; m1 ++ ) for ( int m2 = 0 ; m2 <= 1023 ; m2 ++ ) if ( ( m1 m2 ) == 1023 ) { ans += ( ( m1 == m2 ) ? ( cnt [ m1 ] * ( cnt [ m1 ] - 1 ) ) : ( cnt [ m1 ] * cnt [ m2 ] ) ) ; } return ans / 2 ; } public static void Main ( String [ ] args ) { int n = 5 ; String [ ] str = { "129300455" , "5559948277" , "012334556" , "56789" , "123456879" } ; Console . Write ( countPair ( str , n ) + "
 " ) ; } }


"Pairs with Difference less than K | C # code to find count of Pairs with difference less than K . ; Function to count pairs ; Driver code"
Solution in C#:

using System ; class GFG { static int countPairs ( int [ ] a , int n , int k ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( Math . Abs ( a [ j ] - a [ i ] ) < k ) res ++ ; return res ; } public static void Main ( ) { int [ ] a = { 1 , 10 , 4 , 2 } ; int k = 3 ; int n = a . Length ; Console . WriteLine ( countPairs ( a , n , k ) ) ; } }


"Pairs with Difference less than K | C # code to find count of Pairs with difference less than K . ; to sort the array . ; Keep incrementing result while subsequent elements are within limits . ; Driver code"
Solution in C#:

using System ; class GFG { static int countPairs ( int [ ] a , int n , int k ) { Array . Sort ( a ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int j = i + 1 ; while ( j < n && a [ j ] - a [ i ] < k ) { res ++ ; j ++ ; } } return res ; } public static void Main ( ) { int [ ] a = { 1 , 10 , 4 , 2 } ; int k = 3 ; int n = a . Length ; Console . WriteLine ( countPairs ( a , n , k ) ) ; } }


"Pairs with GCD equal to one in the given range | C # program to print all pairs ; Function to print all pairs ; check if even ; We can print all adjacent pairs for ( int i = l ; i < r ; i += 2 ) { System . out . print ( " { " + i + " , ▁ " + i + 1 + " } , ▁ " ) ; } ; Driver Code"
Solution in C#:

using System ; class GFG { static bool checkPairs ( int l , int r ) { if ( ( l - r ) % 2 == 0 ) return false ; return true ; } static public void Main ( ) { int l = 1 , r = 8 ; if ( checkPairs ( l , r ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Pairwise swap elements of a given linked list | Recursive function to pairwise swap elements of a linked list ; There must be at - least two nodes in the list ; Swap the node 's data with data of next node  ; Call pairWiseSwap ( ) for rest of the list"
Solution in C#:

static void pairWiseSwap ( node head ) { if ( head != null && head . next != null ) { swap ( head . data , head . next . data ) ; pairWiseSwap ( head . next . next ) ; } }


"Palindrome Partitioning | DP |  ; minCut function ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int minCut ( string a ) { int [ ] cut = new int [ a . Length ] ; bool [ , ] palindrome = new bool [ a . Length , a . Length ] ; for ( int i = 0 ; i < a . Length ; i ++ ) { int minCut = i ; for ( int j = 0 ; j <= i ; j ++ ) { if ( a [ i ] == a [ j ] && ( i - j < 2 palindrome [ j + 1 , i - 1 ] ) ) { palindrome [ j , i ] = true ; minCut = Math . Min ( minCut , j == 0 ? 0 : ( cut [ j - 1 ] + 1 ) ) ; } } cut [ i ] = minCut ; } return cut [ a . Length - 1 ] ; } static void Main ( ) { Console . WriteLine ( minCut ( " aab " ) ) ; Console . WriteLine ( minCut ( " aabababaxx " ) ) ; } }


"Palindrome Partitioning | DP | C # Code for Palindrome Partitioning Problem ; Driver code"
Solution in C#:

using System ; public class GFG { static bool isPalindrome ( string String , int i , int j ) { while ( i < j ) { if ( String [ i ] != String [ j ] ) return false ; i ++ ; j -- ; } return true ; } static int minPalPartion ( string String , int i , int j ) { if ( i >= j || isPalindrome ( String , i , j ) ) return 0 ; int ans = Int32 . MaxValue , count ; for ( int k = i ; k < j ; k ++ ) { count = minPalPartion ( String , i , k ) + minPalPartion ( String , k + 1 , j ) + 1 ; ans = Math . Min ( ans , count ) ; } return ans ; } static public void Main ( ) { string str = " ababbbabbababa " ; Console . WriteLine ( " Min ▁ cuts ▁ needed ▁ for ▁ " + " Palindrome ▁ Partitioning ▁ is ▁ " + minPalPartion ( str , 0 , str . Length - 1 ) ) ; } }


"Palindrome Partitioning | DP | C # Code for Palindrome Partitioning Problem ; Returns the minimum number of cuts needed to partition a string such that every part is a palindrome ; Get the length of the string ; Create two arrays to build the solution in bottom up manner C [ i ] [ j ] = Minimum number of cuts needed for palindrome partitioning of substring str [ i . . j ] P [ i ] [ j ] = true if substring str [ i . . j ] is palindrome , else false Note that C [ i ] [ j ] is 0 if P [ i ] [ j ] is true ; different looping variables ; Every substring of length 1 is a palindrome ; L is substring length . Build the solution in bottom up manner by considering all substrings of length starting from 2 to n . The loop structure is same as Matrix Chain Multiplication problem ( See https : www . geeksforgeeks . org / matrix - chain - multiplication - dp - 8 / ) ; For substring of length L , set different possible starting indexes ; Set ending index ; If L is 2 , then we just need to compare two characters . Else need to check two corner characters and value of P [ i + 1 ] [ j - 1 ] ; IF str [ i . . j ] is palindrome , then C [ i ] [ j ] is 0 ; Make a cut at every possible localtion starting from i to j , and get the minimum cost cut . ; Return the min cut value for complete string . i . e . , str [ 0. . n - 1 ] ; Driver program"
Solution in C#:

using System ; class GFG { static int minPalPartion ( String str ) { int n = str . Length ; int [ , ] C = new int [ n , n ] ; bool [ , ] P = new bool [ n , n ] ; int i , j , k , L ; for ( i = 0 ; i < n ; i ++ ) { P [ i , i ] = true ; C [ i , i ] = 0 ; } for ( L = 2 ; L <= n ; L ++ ) { for ( i = 0 ; i < n - L + 1 ; i ++ ) { j = i + L - 1 ; if ( L == 2 ) P [ i , j ] = ( str [ i ] == str [ j ] ) ; else P [ i , j ] = ( str [ i ] == str [ j ] ) && P [ i + 1 , j - 1 ] ; if ( P [ i , j ] == true ) C [ i , j ] = 0 ; else { C [ i , j ] = int . MaxValue ; for ( k = i ; k <= j - 1 ; k ++ ) C [ i , j ] = Math . Min ( C [ i , j ] , C [ i , k ] + C [ k + 1 , j ] + 1 ) ; } } } return C [ 0 , n - 1 ] ; } public static void Main ( ) { String str = " ababbbabbababa " ; Console . Write ( " Min ▁ cuts ▁ needed ▁ for ▁ " + " Palindrome ▁ Partitioning ▁ is ▁ " + minPalPartion ( str ) ) ; } }


"Palindrome Partitioning | DP | Driver code"
Solution in C#:


"Palindrome Substring Queries | A C # program to answer queries to check whether the substrings are palindrome or not efficiently ; Structure to represent a query . A query consists of ( L , R ) and we have to answer whether the substring from index - L to R is a palindrome or not ; A function to check if a string str is palindrome in the ranfe L to R ; Keep comparing characters while they are same ; A Function to find pow ( base , exponent ) % MOD in log ( exponent ) time ; A Function to calculate Modulo Multiplicative Inverse of ' n ' ; A Function to calculate the prefix hash ; A Function to calculate the suffix hash Suffix hash is nothing but the prefix hash of the reversed string ; A Function to answer the Queries ; Hash Value of Substring [ L , R ] ; Reverse Hash Value of Substring [ L , R ] ; If both are equal then the substring is a palindrome ; A Dynamic Programming Based Approach to compute the powers of 101 ; 101 ^ 0 = 1 ; Driver code ; A Table to store the powers of 101 ; Arrays to hold prefix and suffix hash values ; Compute Prefix Hash and Suffix Hash Arrays"
Solution in C#:

 using System ; class GFG { static int p = 101 ; static int MOD = 1000000007 ; public class Query { public int L , R ; public Query ( int L , int R ) { this . L = L ; this . R = R ; } } ; static Boolean isPalindrome ( String str , int L , int R ) { while ( R > L ) { if ( str [ L ++ ] != str [ R -- ] ) { return ( false ) ; } } return ( true ) ; } static int modPow ( int Base , int exponent ) { if ( exponent == 0 ) { return 1 ; } if ( exponent == 1 ) { return Base ; } int temp = modPow ( Base , exponent / 2 ) ; if ( exponent % 2 == 0 ) { return ( temp % MOD * temp % MOD ) % MOD ; } else { return ( ( ( temp % MOD * temp % MOD ) % MOD ) * Base % MOD ) % MOD ; } } static int findMMI ( int n ) { return modPow ( n , MOD - 2 ) ; } static void computePrefixHash ( String str , int n , int [ ] prefix , int [ ] power ) { prefix [ 0 ] = 0 ; prefix [ 1 ] = str [ 0 ] ; for ( int i = 2 ; i <= n ; i ++ ) { prefix [ i ] = ( prefix [ i - 1 ] % MOD + ( str [ i - 1 ] % MOD * power [ i - 1 ] % MOD ) % MOD ) % MOD ; } return ; } static void computeSuffixHash ( String str , int n , int [ ] suffix , int [ ] power ) { suffix [ 0 ] = 0 ; suffix [ 1 ] = str [ n - 1 ] ; for ( int i = n - 2 , j = 2 ; i >= 0 && j <= n ; i -- , j ++ ) { suffix [ j ] = ( suffix [ j - 1 ] % MOD + ( str [ i ] % MOD * power [ j - 1 ] % MOD ) % MOD ) % MOD ; } return ; } static void queryResults ( String str , Query [ ] q , int m , int n , int [ ] prefix , int [ ] suffix , int [ ] power ) { for ( int i = 0 ; i <= m - 1 ; i ++ ) { int L = q [ i ] . L ; int R = q [ i ] . R ; long hash_LR = ( ( prefix [ R + 1 ] - prefix [ L ] + MOD ) % MOD * findMMI ( power [ L ] ) % MOD ) % MOD ; long reverse_hash_LR = ( ( suffix [ n - L ] - suffix [ n - R - 1 ] + MOD ) % MOD * findMMI ( power [ n - R - 1 ] ) % MOD ) % MOD ; if ( hash_LR == reverse_hash_LR ) { if ( isPalindrome ( str , L , R ) == true ) { Console . Write ( " The ▁ Substring ▁ [ { 0 } ▁ { 1 } ] ▁ is ▁ a ▁ " + " palindrome
 " , L , R ) ; } else { Console . Write ( " The ▁ Substring ▁ [ { 0 } ▁ { 1 } ] ▁ is ▁ not ▁ a...


"Palindrome by swapping only one character | C # program palindrome by swapping only one character ; convert the string to character array ; counts the number of differences which prevents the string from being palindrome . ; keeps a record of the characters that prevents the string from being palindrome . ; loops from the start of a string till the midpoint of the string ; difference is encountered preventing the string from being palindrome ; 3 rd differences encountered and its no longer possible to make is palindrome by one swap ; record the different character ; store the different characters ; its already palindrome ; only one difference is found ; if the middleChar matches either of the difference producing characters , return true ; two differences are found ; if the characters contained in the two sets are same , return true ; Driver code"
Solution in C#:

using System ; class GFG { public static bool isPalindromePossible ( String input ) { int len = input . Length , i ; char [ ] charStr = input . ToCharArray ( ) ; int diffCount = 0 ; char [ , ] diff = new char [ 2 , 2 ] ; for ( i = 0 ; i < len / 2 ; i ++ ) { if ( charStr [ i ] != charStr [ len - i - 1 ] ) { if ( diffCount == 2 ) return false ; diff [ diffCount , 0 ] = charStr [ i ] ; diff [ diffCount ++ , 1 ] = charStr [ len - i - 1 ] ; } } switch ( diffCount ) { case 0 : return true ; case 1 : char midChar = charStr [ i ] ; if ( len % 2 != 0 && ( diff [ 0 , 0 ] == midChar diff [ 0 , 1 ] == midChar ) ) return true ; break ; case 2 : if ( ( diff [ 0 , 0 ] == diff [ 1 , 0 ] && diff [ 0 , 1 ] == diff [ 1 , 1 ] ) || ( diff [ 0 , 0 ] == diff [ 1 , 1 ] && diff [ 0 , 1 ] == diff [ 1 , 0 ] ) ) return true ; break ; } return false ; } public static void Main ( String [ ] args ) { Console . WriteLine ( isPalindromePossible ( " bbg " ) ) ; Console . WriteLine ( isPalindromePossible ( " bdababd " ) ) ; Console . WriteLine ( isPalindromePossible ( " gcagac " ) ) ; } }


"Palindrome pair in an array of words ( or strings ) | C # program to check if there is a pair that of above method using Trie ; Alphabet size ( # of symbols ) ; Trie node ; public List < int > pos ; To store palindromic positions in str ; isLeaf is true if the node represents end of a word ; constructor ; Utility function to check if a string is a palindrome ; compare each character from starting with its corresponding character from last ; If not present , inserts reverse of key into Trie . If the key is prefix of a Trie node , just mark leaf node ; Start traversing word from the last ; If it is not available in Trie , then store it ; If current word is palindrome till this level , store index of current word . ; mark last node as leaf ; list to store result ; Returns true if key presents in Trie , else false ; If it is present also check upto which index it is palindrome ; If not present then return ; Function to check if a palindrome pair exists ; Construct trie ; Search for different keys ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static readonly int ALPHABET_SIZE = 26 ; class TrieNode { public TrieNode [ ] children = new TrieNode [ ALPHABET_SIZE ] ; public int id ; public Boolean isLeaf ; public TrieNode ( ) { isLeaf = false ; pos = new List < int > ( ) ; for ( int i = 0 ; i < ALPHABET_SIZE ; i ++ ) children [ i ] = null ; } } static Boolean isPalindrome ( String str , int i , int len ) { while ( i < len ) { if ( str [ i ] != str [ len ] ) return false ; i ++ ; len -- ; } return true ; } static void insert ( TrieNode root , String key , int id ) { TrieNode pCrawl = root ; for ( int level = key . Length - 1 ; level >= 0 ; level -- ) { int index = key [ level ] - ' a ' ; if ( pCrawl . children [ index ] == null ) pCrawl . children [ index ] = new TrieNode ( ) ; if ( isPalindrome ( key , 0 , level ) ) ( pCrawl . pos ) . Add ( id ) ; pCrawl = pCrawl . children [ index ] ; } pCrawl . id = id ; pCrawl . pos . Add ( id ) ; pCrawl . isLeaf = true ; } static List < List < int > > result ; static void search ( TrieNode root , String key , int id ) { TrieNode pCrawl = root ; for ( int level = 0 ; level < key . Length ; level ++ ) { int index = key [ level ] - ' a ' ; if ( pCrawl . id >= 0 && pCrawl . id != id && isPalindrome ( key , level , key . Length - 1 ) ) { List < int > l = new List < int > ( ) ; l . Add ( id ) ; l . Add ( pCrawl . id ) ; result . Add ( l ) ; } if ( pCrawl . children [ index ] == null ) return ; pCrawl = pCrawl . children [ index ] ; } foreach ( int i in pCrawl . pos ) { if ( i == id ) continue ; List < int > l = new List < int > ( ) ; l . Add ( id ) ; l . Add ( i ) ; result . Add ( l ) ; } } static Boolean checkPalindromePair ( List < String > vect ) { TrieNode root = new TrieNode ( ) ; for ( int i = 0 ; i < vect . Count ; i ++ ) insert ( root , vect [ i ] , i ) ; result = new List < List < int > > ( ) ; for ( int i = 0 ; i < vect . Count ; i ++ ) { search ( root , vect [ i ] , i ) ; if ( result . Count > 0 ) return true ; } return false ; } public static...


"Palindrome pair in an array of words ( or strings ) | C # program to find if there is a pair that can form a palindrome . ; Utility function to check if a string is a palindrome ; compare each character from starting with its corresponding character from last ; Function to check if a palindrome pair exists ; Consider each pair one by one ; concatenate both strings ; check if the concatenated string is palindrome ; check if the concatenated string is palindrome ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static Boolean isPalindrome ( String str ) { int len = str . Length ; for ( int i = 0 ; i < len / 2 ; i ++ ) if ( str [ i ] != str [ len - i - 1 ] ) return false ; return true ; } static Boolean checkPalindromePair ( List < String > vect ) { for ( int i = 0 ; i < vect . Count - 1 ; i ++ ) { for ( int j = i + 1 ; j < vect . Count ; j ++ ) { String check_str = " " ; check_str = check_str + vect [ i ] + vect [ j ] ; if ( isPalindrome ( check_str ) ) return true ; check_str = vect [ j ] + vect [ j ] ; if ( isPalindrome ( check_str ) ) return true ; } } return false ; } public static void Main ( String [ ] args ) { List < String > vect = new List < String > ( ) { " geekf " , " geeks " , " or " , " keeg " , " abc " , " bc " } ; if ( checkPalindromePair ( vect ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Palindromic Primes | C # Program to print all palindromic primes smaller than or equal to n . ; A function that returns true only if num contains one digit ; comparison operation is faster than division operation . So using following instead of " return ▁ num ▁ / ▁ 10 ▁ = = ▁ 0 ; " ; A recursive function to find out whether num is palindrome or not . Initially , dupNum contains address of a copy of num . ; Base case ( needed for recursion termination ) : This statement / mainly compares the first digit with the last digit ; This is the key line in this method . Note that all recursive / calls have a separate copy of num , but they all share same copy of dupNum . We divide num while moving up the recursion tree ; The following statements are executed when we move up the recursion call tree ; At this point , if num % 10 contains ith digit from beginning , then ( dupNum ) % 10 contains ith digit from end ; The main function that uses recursive function isPalUtil ( ) to find out whether num is palindrome or not ; If num is negative , make it positive ; Create a separate copy of num , so that modifications made to address dupNum don 't  change the input number.  int dupNum = num; dupNum = num ; Function to generate all primes and checking whether number is palindromic or not ; 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 ; Print all palindromic prime numbers ; checking whether the given number is prime palindromic or not ; Driver function"
Solution in C#:

 using System ; class GFG { static bool oneDigit ( int num ) { return ( num >= 0 && num < 10 ) ; } static bool isPalUtil ( int num , int dupNum ) { if ( oneDigit ( num ) ) return ( num == ( dupNum ) % 10 ) ; if ( ! isPalUtil ( num / 10 , dupNum ) ) return false ; dupNum /= 10 ; return ( num % 10 == ( dupNum ) % 10 ) ; } static bool isPal ( int num ) { if ( num < 0 ) num = - num ; return isPalUtil ( num , dupNum ) ; } static void printPalPrimesLessThanN ( 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 ] ) { for ( int i = p * 2 ; i <= n ; i += p ) { prime [ i ] = false ; } } } for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] && isPal ( p ) ) { Console . Write ( p + " ▁ " ) ; } } } public static void Main ( ) { int n = 100 ; Console . Write ( " Palindromic ▁ primes ▁ smaller ▁ than ▁ or ▁ " + " equal ▁ to ▁ are ▁ :
 " , n ) ; printPalPrimesLessThanN ( n ) ; } }


"Palindromic Selfie Numbers | C # program to find palindromic selfie numbers ; To store all permutations of digits in the number ; int number ; input number ; Function to reverse the digits of a number ; Append it at the beg ; num = num / 10 ; Reduce number until 0 ; Function to check palindromic selfie ; Length of the number required for calculating all permutations of the digits ; Remove the number and its palindrome from the obtained set as this is the LHS of multiplicative equality ; Iterate over all other numbers ; Check for equality x * palin ( x ) = y * palin ( y ) ; flag = true ; Answer found ; If no such number found ; Function to get all possible possible permutations of the digits in num ; Adds the new permutation obtained in the set ; Swap digits to get a different ordering ; Recurse to next pair of digits ; num = swap ( num , l , i ) ; Swap back ; Function that swaps the digits i and j in the num ; Convert int to char array ; Swap the ith and jth character ; Convert back to int and return ; Driver code ; First example , input = 145572 ; Second example , input = 19362 ; Third example , input = 4669"
Solution in C#:

using System ; using System . Collections . Generic ; public class palindrome_selfie { HashSet < int > all_permutes = new HashSet < int > ( ) ; public palindrome_selfie ( int num ) { number = num ; } public int palindrome ( int num ) { int reversednum = 0 ; int d ; while ( num > 0 ) { reversednum = reversednum * 10 + d ; } return reversednum ; } public void palin_selfie ( ) { int l = String . Join ( " " , number ) . Length - 1 ; all_permutes . Remove ( palindrome ( number ) ) ; all_permutes . Remove ( number ) ; foreach ( var number2 in all_permutes ) { if ( number * palindrome ( number ) == number2 * palindrome ( number2 ) ) { Console . WriteLine ( " Palindrome ▁ multiplicative " + " selfie ▁ of ▁ " + number + " ▁ is ▁ : ▁ " + number2 ) ; break ; } } if ( flag == false ) { Console . WriteLine ( " Given ▁ number ▁ has ▁ " + " no ▁ palindrome ▁ selfie . " ) ; } } public void permute ( int num , int l , int r ) { if ( l == r ) all_permutes . Add ( num ) ; else { for ( int i = l ; i <= r ; i ++ ) { num = swap ( num , l , i ) ; permute ( num , l + 1 , r ) ; } } } public int swap ( int num , int i , int j ) { char temp ; char [ ] charArray = String . Join ( " " , num ) . ToCharArray ( ) ; temp = charArray [ i ] ; charArray [ i ] = charArray [ j ] ; charArray [ j ] = temp ; return int . Parse ( String . Join ( " " , charArray ) ) ; } public static void Main ( String [ ] args ) { palindrome_selfie example1 = new palindrome_selfie ( 145572 ) ; example1 . palin_selfie ( ) ; palindrome_selfie example2 = new palindrome_selfie ( 19362 ) ; example2 . palin_selfie ( ) ; palindrome_selfie example3 = new palindrome_selfie ( 4669 ) ; example3 . palin_selfie ( ) ; } }


"Panalphabetic window in a string | C # Program to check whether given string contain panalphabetic window or not ; Return if given string contain panalphabetic window . ; traversing the string ; if character of string is equal to ch , increment ch . ; if all characters are found , return true . ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPanalphabeticWindow ( string s , int n ) { char ch = ' a ' ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ch ) ch ++ ; if ( ch == ' z ' + 1 ) return true ; } return false ; } public static void Main ( ) { string s = " abujm ▁ zvcd ▁ acefc ▁ deghf " + " ▁ gijklem ▁ n ▁ o ▁ p ▁ pafqrstuvwxyzfap " ; int n = s . Length ; if ( isPanalphabeticWindow ( s , n ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }


"Panarithmic numbers within a given range | C # program to print practical Numbers in given range ; Function to compute divisors of a number ; To store divisors ; 1 will always be a divisor ; Check if i is squareroot of A then only one time insert it in ans ; Function to check that a number can be represented as sum of distinct divisor or not ; The value of subset [ i ] [ j ] will be true if there is a subset of set [ 0. . j - 1 ] with sum equal to i ; If sum is 0 , then answer is true ; If sum is not 0 and set is empty , then answer is false ; Fill the subset table in bottom up manner ; Return the possibility of given sum ; Function to check a number is Practical or not ; To store divisors ; If all numbers can be represented as sum of unique divisors ; Function to print Practical Numbers in a range ; Driver code"
Solution in C#:

using System ; using System . Collections ; class GFG { static ArrayList get_divisors ( int A ) { ArrayList ans = new ArrayList ( ) ; ans . Add ( 1 ) ; for ( int i = 2 ; i <= ( int ) Math . Sqrt ( A ) ; i ++ ) { if ( A % i == 0 ) { ans . Add ( i ) ; if ( ( i * i ) != A ) ans . Add ( A / i ) ; } } return ans ; } static bool Sum_Possible ( ArrayList set , int sum ) { int n = set . Count ; bool [ , ] subset = new bool [ n + 1 , sum + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) subset [ i , 0 ] = true ; for ( int i = 1 ; i <= sum ; i ++ ) subset [ 0 , i ] = false ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( j < ( int ) set [ i - 1 ] ) subset [ i , j ] = subset [ i - 1 , j ] ; if ( j >= ( int ) set [ i - 1 ] ) subset [ i , j ] = subset [ i - 1 , j ] || subset [ i - 1 , j - ( int ) set [ i - 1 ] ] ; } } return subset [ n , sum ] ; } static bool Is_Practical ( int A ) { ArrayList divisors = new ArrayList ( ) ; divisors = get_divisors ( A ) ; for ( int i = 2 ; i < A ; i ++ ) { if ( Sum_Possible ( divisors , i ) == false ) return false ; } return true ; } static void print_practica_No ( int A , int B ) { for ( int i = A ; i <= B ; i ++ ) { if ( Is_Practical ( i ) == true ) { Console . Write ( i + " ▁ " ) ; } } } public static void Main ( string [ ] args ) { int A = 1 , B = 100 ; print_practica_No ( A , B ) ; } }


"Pancake sorting | C # program to sort array using pancake sort ; Reverses arr [ 0. . i ] ; Returns index of the maximum element in arr [ 0. . n - 1 ] ; The main function that sorts given array using flip operations ; Start from the complete array and one by one reduce current size by one ; Find index of the maximum element in arr [ 0. . curr_size - 1 ] ; Move the maximum element to end of current array if it 's not already at  the end ; To move at the end , first move maximum number to beginning ; Now move the maximum number to end by reversing current array ; Utility function to print array arr [ ] ; Driver function to check for above functions"
Solution in C#:

using System ; class GFG { static void flip ( int [ ] arr , int i ) { int temp , start = 0 ; while ( start < i ) { temp = arr [ start ] ; arr [ start ] = arr [ i ] ; arr [ i ] = temp ; start ++ ; i -- ; } } static int findMax ( int [ ] arr , int n ) { int mi , i ; for ( mi = 0 , i = 0 ; i < n ; ++ i ) if ( arr [ i ] > arr [ mi ] ) mi = i ; return mi ; } static int pancakeSort ( int [ ] arr , int n ) { for ( int curr_size = n ; curr_size > 1 ; -- curr_size ) { int mi = findMax ( arr , curr_size ) ; if ( mi != curr_size - 1 ) { flip ( arr , mi ) ; flip ( arr , curr_size - 1 ) ; } } return 0 ; } static void printArray ( int [ ] arr , int arr_size ) { for ( int i = 0 ; i < arr_size ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . Write ( " " ) ; } public static void Main ( ) { int [ ] arr = { 23 , 10 , 20 , 11 , 12 , 6 , 7 } ; int n = arr . Length ; pancakeSort ( arr , n ) ; Console . Write ( " Sorted ▁ Array : ▁ " ) ; printArray ( arr , n ) ; } }


"Pandigital Product | C # code to check the number is Pandigital Product or not . ; calculate the multiplicand , multiplier , and product eligible for pandigital ; To check the string formed from multiplicand multiplier and product is pandigital ; Driver function"
Solution in C#:

using System ; class GFG { public static bool PandigitalProduct_1_9 ( int n ) { for ( int i = 1 ; i * i <= n ; i ++ ) if ( n % i == 0 && isPandigital ( " " + n + i + n / i ) ) return true ; return false ; } public static bool isPandigital ( String str ) { if ( str . Length != 9 ) return false ; char [ ] ch = str . ToCharArray ( ) ; Array . Sort ( ch ) ; return new String ( ch ) . Equals ( "123456789" ) ; } public static void Main ( ) { int n = 6952 ; if ( PandigitalProduct_1_9 ( n ) == true ) Console . Write ( " yes " ) ; else Console . Write ( " no " ) ; } }


"Paper Cut into Minimum Number of Squares | C # program to find minimum number of squares to cut a paper . ; To swap two numbers ; Returns min number of squares needed ; swap if a is small size side . ; Iterate until small size side is greater then 0 ; Update result ; Driver code"
Solution in C#:

using System ; class GFG { static void swap ( int a , int b ) { int temp = a ; a = b ; b = temp ; } static int minimumSquare ( int a , int b ) { int result = 0 , rem = 0 ; if ( a < b ) swap ( a , b ) ; while ( b > 0 ) { result += a / b ; rem = a % b ; a = b ; b = rem ; } return result ; } public static void Main ( String [ ] args ) { int n = 13 , m = 29 ; Console . WriteLine ( minimumSquare ( n , m ) ) ; } }


"Paper Cut into Minimum Number of Squares | Set 2 | C # program to find minimum number of squares to cut a paper using Dynamic Programming ; Returns min number of squares needed ; Initializing max values to vertical_min and horizontal_min ; N = 11 & M = 13 is a special case ; If the given rectangle is already a square ; If the answer for the given rectangle is previously calculated return that answer ; The rectangle is cut horizontally and vertically into two parts and the cut with minimum value is found for every recursive call . ; Calculating the minimum answer for the rectangles with width equal to n and length less than m for finding the cut point for the minimum answer ; Calculating the minimum answer for the rectangles with width less than n and length equal to m for finding the cut point for the minimum answer ; Minimum of the vertical cut or horizontal cut to form a square is the answer ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int [ , ] dp = new int [ 300 , 300 ] ; static int minimumSquare ( int m , int n ) { int vertical_min = int . MaxValue ; int horizontal_min = int . MaxValue ; if ( n == 13 && m == 11 ) return 6 ; if ( m == 13 && n == 11 ) return 6 ; if ( m == n ) return 1 ; if ( dp [ m , n ] != 0 ) return dp [ m , n ] ; for ( int i = 1 ; i <= m / 2 ; i ++ ) { horizontal_min = Math . Min ( minimumSquare ( i , n ) + minimumSquare ( m - i , n ) , horizontal_min ) ; } for ( int j = 1 ; j <= n / 2 ; j ++ ) { vertical_min = Math . Min ( minimumSquare ( m , j ) + minimumSquare ( m , n - j ) , vertical_min ) ; } dp [ m , n ] = Math . Min ( vertical_min , horizontal_min ) ; return dp [ m , n ] ; } public static void Main ( ) { int m = 30 , n = 35 ; Console . WriteLine ( minimumSquare ( m , n ) ) ; } }


"Parity of the given mathematical expression using given N numbers | C # program to determine the parity of the given mathematical expression ; Iterating through the given integers ; If any odd number is present , then S is even parity ; Else , S is odd parity ; Driver code"
Solution in C#:

using System ; public class GFG { static void getParity ( int n , int [ ] A ) { foreach ( int x in A ) { if ( ( x & 1 ) == 1 ) { Console . WriteLine ( " Even " ) ; return ; } } Console . WriteLine ( " Odd " ) ; } public static void Main ( string [ ] args ) { int N = 3 ; int [ ] A = { 2 , 3 , 1 } ; getParity ( N , A ) ; } }


"Partition a Linked List into K continuous groups with difference in their sizes at most 1 | C # program for the above approach ; Link List Node ; Function to insert a node into the Linked List ; Allocate a new dynamic node ; Update new_node . val ; Stores the head_ref ; Update head_ref ; Function to split the linked list in K groups ; Stores the K groups ; If head is null ; Iterate until K is non - zero ; Stores the length of the linked list ; Stores the head node of the linked list ; Iterate over the linked list ; Update p ; Update N ; Iterate over the linked list ; Stores the length of the current group ; Stores the current node ; Stores the previous node ; If rem is greater than 0 ; Update p ; Decrement rem by 1 ; Iterate until x is non - zero ; If the last is equal to p ; Otherwise ; Join the link between last and the current element ; Update the last node ; Update p node ; Assign null to last . next ; Push the current linked list in ans ; Decrement K ; While K greater than 0 ; Update the value of ans ; Increment K ; Print the result ; Print the value ; Update ans [ i ] ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { class ListNode { public int val ; public ListNode next ; } ; static ListNode push ( ListNode head_ref , int node_val ) { ListNode new_node = new ListNode ( ) ; new_node . val = node_val ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static void splitListInParts ( ListNode head , int K ) { List < ListNode > ans = new List < ListNode > ( ) ; if ( head == null ) { while ( K -- > 0 ) ans . Add ( null ) ; } int N = 0 ; ListNode p = head ; while ( p . next != null ) { p = p . next ; N ++ ; } int len = N / K ; int rem = N % K ; p = head ; while ( K > 0 && p . next != null ) { int x = len ; ListNode curr_head = p ; ListNode last = p ; if ( rem > 0 ) { p = p . next ; rem -- ; } while ( x -- > 0 ) { if ( last == p ) p = p . next ; else { last . next = p ; last = p ; p = p . next ; } } last . next = null ; ans . Add ( curr_head ) ; K -- ; } while ( K > 0 ) { ans . Add ( null ) ; K -- ; } Console . Write ( " { " ) ; for ( int i = 0 ; i < ans . Count ; i ++ ) { Console . Write ( " { " ) ; while ( ans [ i ] != null ) { Console . Write ( ans [ i ] . val + " ▁ " ) ; ans [ i ] = ans [ i ] . next ; } Console . Write ( " } " ) ; if ( i != ans . Count - 1 ) Console . Write ( " , ▁ " ) ; } Console . Write ( " } " ) ; } public static void Main ( String [ ] args ) { ListNode root = new ListNode ( ) ; root = push ( root , 8 ) ; root = push ( root , 7 ) ; root = push ( root , 6 ) ; root = push ( root , 5 ) ; root = push ( root , 4 ) ; root = push ( root , 3 ) ; root = push ( root , 2 ) ; root = push ( root , 1 ) ; int K = 3 ; splitListInParts ( root , K ) ; } }


"Partition a number into two divisible parts | C # program to check if a string can be splitted into two strings such that one is divisible by ' a ' and other is divisible by ' b ' . ; Finds if it is possible to partition str into two parts such that first part is divisible by a and second part is divisible by b . ; Create an array of size len + 1 and initialize it with 0. Store remainders from left to right when divided by ' a ' ; Compute remainders from right to left when divided by ' b ' ; Find a point that can partition a number ; If split is not possible at this point ; We can split at i if one of the following two is true . a ) All characters after str [ i ] are 0 b ) String after str [ i ] is divisible by b , i . e . , str [ i + 1. . n - 1 ] is divisible by b . ; Driver code"
Solution in C#:

using System ; class GFG { static void findDivision ( string str , int a , int b ) { int len = str . Length ; int [ ] lr = new int [ len + 1 ] ; lr [ 0 ] = ( ( int ) str [ 0 ] - ( int ) '0' ) % a ; for ( int i = 1 ; i < len ; i ++ ) lr [ i ] = ( ( lr [ i - 1 ] * 10 ) % a + ( ( int ) str [ i ] - ( int ) '0' ) ) % a ; int [ ] rl = new int [ len + 1 ] ; rl [ len - 1 ] = ( ( int ) str [ len - 1 ] - ( int ) '0' ) % b ; int power10 = 10 ; for ( int i = len - 2 ; i >= 0 ; i -- ) { rl [ i ] = ( rl [ i + 1 ] + ( ( int ) str [ i ] - ( int ) '0' ) * power10 ) % b ; power10 = ( power10 * 10 ) % b ; } for ( int i = 0 ; i < len - 1 ; i ++ ) { if ( lr [ i ] != 0 ) continue ; if ( rl [ i + 1 ] == 0 ) { Console . WriteLine ( " YES " ) ; for ( int k = 0 ; k <= i ; k ++ ) Console . Write ( str [ k ] ) ; Console . Write ( " , ▁ " ) ; for ( int k = i + 1 ; k < len ; k ++ ) Console . Write ( str [ k ] ) ; return ; } } Console . WriteLine ( " NO " ) ; } static void Main ( ) { string str = "123" ; int a = 12 , b = 3 ; findDivision ( str , a , b ) ; } }


"Partition a set into two non | C # Program for above approach ; Function to return the maximum difference between the subset sums ; Stores the total sum of the array ; Checks for positive and negative elements ; Stores the minimum element from the given array ; Traverse the array ; Calculate total sum ; Mark positive element present in the set ; Mark negative element present in the set ; Find the minimum element of the set ; If the array contains both positive and negative elements ; Otherwise ; Driver Code ; Given the array ; Length of the array ; Function Call"
Solution in C#:

using System ; class GFG { static int maxDiffSubsets ( int [ ] arr ) { int totalSum = 0 ; bool pos = false , neg = false ; int min = int . MaxValue ; for ( int i = 0 ; i < arr . Length ; i ++ ) { totalSum += Math . Abs ( arr [ i ] ) ; if ( arr [ i ] > 0 ) pos = true ; if ( arr [ i ] < 0 ) neg = true ; if ( arr [ i ] < min ) min = arr [ i ] ; } if ( pos && neg ) return totalSum ; else return totalSum - 2 * min ; } public static void Main ( String [ ] args ) { int [ ] S = { 1 , 2 , 1 } ; int N = S . Length ; if ( N < 2 ) Console . WriteLine ( " Not ▁ Possible " ) ; else Console . WriteLine ( maxDiffSubsets ( S ) ) ; } }


"Partition a set into two subsets such that difference between max of one and min of other is minimized | C # program for the above approach ; Function to split the array ; Sort the array in increasing order ; Calculating the max difference between consecutive elements ; Return the final minimum difference ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call"
Solution in C#:

using System ; class GFG { static int splitArray ( int [ ] arr , int N ) { Array . Sort ( arr ) ; int result = Int32 . MaxValue ; for ( int i = 1 ; i < N ; i ++ ) { result = Math . Min ( result , arr [ i ] - arr [ i - 1 ] ) ; } return result ; } public static void Main ( ) { int [ ] arr = { 3 , 1 , 2 , 6 , 4 } ; int N = arr . Length ; Console . Write ( splitArray ( arr , N ) ) ; } }


"Partition a set into two subsets such that the difference of subset sums is minimum |  ; dp [ i ] gives whether is it possible to get i as sum of elements dd is helper variable we use dd to ignoring duplicates ; Initialising dp and dd ; sum = 0 is possible ; updating dd [ k ] as true if k can be formed using elements from 1 to i + 1 ; updating dd ; dd [ j ] = false ; reset dd ; checking the number from sum / 2 to 1 which is possible to get as sum ; since i is possible to form then another number is sum - i so mindifference is sum - i - i"
Solution in C#:

 using System ; public class GFG { static int minDifference ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; int y = sum / 2 + 1 ; bool [ ] dp = new bool [ y ] ; bool [ ] dd = new bool [ y ] ; for ( int i = 0 ; i < y ; i ++ ) { dp [ i ] = dd [ i ] = false ; } dd [ 0 ] = true ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j + arr [ i ] < y ; j ++ ) { if ( dp [ j ] ) dd [ j + arr [ i ] ] = true ; } for ( int j = 0 ; j < y ; j ++ ) { if ( dd [ j ] ) dp [ j ] = true ; } } for ( int i = y - 1 ; i >= 0 ; i -- ) { if ( dp [ i ] ) return ( sum - 2 * i ) ; } return 0 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 6 , 11 , 5 } ; int n = arr . Length ; Console . Write ( " The ▁ Minimum ▁ difference ▁ of ▁ 2 ▁ sets ▁ is ▁ " + minDifference ( arr , n ) + '
 ' ) ; } }


"Partition a set into two subsets such that the difference of subset sums is minimum | A Recursive C # program to solve minimum sum partition problem . ; Returns the minimum value of the difference of the two sets . ; Calculate sum of all elements ; Create an array to store results of subproblems ; Initialize first column as true . 0 sum is possible with all elements . ; Initialize top row , except dp [ 0 , 0 ] , as false . With 0 elements , no other sum except 0 is possible ; Fill the partition table in bottom up manner ; If i 'th element is excluded ; If i 'th element is included ; Initialize difference of two sums . ; Find the largest j such that dp [ n , j ] is true where j loops from sum / 2 t0 0 ; Driver code"
Solution in C#:

using System ; class GFG { static int findMin ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; bool [ , ] dp = new bool [ n + 1 , sum + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i , 0 ] = true ; for ( int i = 1 ; i <= sum ; i ++ ) dp [ 0 , i ] = false ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { dp [ i , j ] = dp [ i - 1 , j ] ; if ( arr [ i - 1 ] <= j ) dp [ i , j ] |= dp [ i - 1 , j - arr [ i - 1 ] ] ; } } int diff = int . MaxValue ; for ( int j = sum / 2 ; j >= 0 ; j -- ) { if ( dp [ n , j ] == true ) { diff = sum - 2 * j ; break ; } } return diff ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 1 , 4 , 2 , 2 , 1 } ; int n = arr . Length ; Console . WriteLine ( " The ▁ minimum ▁ difference ▁ " + " between ▁ 2 ▁ sets ▁ is ▁ " + findMin ( arr , n ) ) ; } }


"Partition a set into two subsets such that the difference of subset sums is minimum | C # code to partition a set into two subsets such that the difference of subset sums is minimum ; Function to find the minimum sum ; If we have reached last element . Sum of one subset is sumCalculated , sum of other subset is sumTotal - sumCalculated . Return absolute difference of two sums . ; For every item arr [ i ] , we have two choices ( 1 ) We do not include it first set ( 2 ) We include it in first set We return minimum of two choices ; Returns minimum possible difference between sums of two subsets ; Compute total sum of elements ; Compute result using recursive function ; Driver program to test above function"
Solution in C#:

using System ; class GFG { public static int findMinRec ( int [ ] arr , int i , int sumCalculated , int sumTotal ) { if ( i == 0 ) return Math . Abs ( ( sumTotal - sumCalculated ) - sumCalculated ) ; return Math . Min ( findMinRec ( arr , i - 1 , sumCalculated + arr [ i - 1 ] , sumTotal ) , findMinRec ( arr , i - 1 , sumCalculated , sumTotal ) ) ; } public static int findMin ( int [ ] arr , int n ) { int sumTotal = 0 ; for ( int i = 0 ; i < n ; i ++ ) sumTotal += arr [ i ] ; return findMinRec ( arr , n , 0 , sumTotal ) ; } public static void Main ( ) { int [ ] arr = { 3 , 1 , 4 , 2 , 2 , 1 } ; int n = arr . Length ; Console . Write ( " The ▁ minimum ▁ difference " + " ▁ between ▁ two ▁ sets ▁ is ▁ " + findMin ( arr , n ) ) ; } }


"Partition a string into palindromic strings of at least length 2 with every character present in a single string | C # program for the above approach ; Function to check if a string can be split into palindromic strings of at least length 2 by including every character exactly once ; Stores the frequency of each character ; Store the frequency of characters with frequencies 1 and even respectively ; Traverse the string s ; Iterate over all the characters ; If the frequency is 1 ; If frequency is even ; Print the result ; Stores the number of characters with frequency equal to 1 that are not part of a palindromic string ; Iterate over all the characters ; If o becomes less than 0 , then break out of the loop ; If frequency of the current character is > 2 and is odd ; Update the value of o ; If a single character is still remaining ; Increment o by 1 ; Set a [ i ] to 1 ; Print the result ; Driver code"
Solution in C#:

using System ; class GFG { static void checkPalindrome ( string s ) { int [ ] a = new int [ 26 ] ; int o = 0 , e = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) a [ s [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( a [ i ] == 1 ) o ++ ; else if ( a [ i ] % 2 == 0 && a [ i ] != 0 ) e += ( a [ i ] / 2 ) ; } if ( e >= o ) Console . WriteLine ( " Yes " ) ; else { o = o - e ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( o <= 0 ) break ; if ( o > 0 && a [ i ] % 2 == 1 && a [ i ] > 2 ) { int k = o ; o = o - a [ i ] / 2 ; if ( o > 0 2 * k + 1 == a [ i ] ) { o ++ ; a [ i ] = 1 ; } } } if ( o <= 0 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } } static void Main ( ) { string S = " abbbaddzcz " ; checkPalindrome ( S ) ; } }


"Partition an array into two subsets with equal count of unique elements | C # program for the above approach ; Function to partition the array into two subsets such that count of unique elements in both subsets is the same ; Stores the subset number for each array elements ; Stores the count of unique array elements ; Stores the frequency of elements ; Traverse the array ; Count of elements having a frequency of 1 ; Check if there exists any element with frequency > 2 ; Count of elements needed to have frequency exactly 1 in each subset ; Initialize all values in the / array ans [ ] as 1 ; Traverse the array ans [ ] ; This array element is a part of first subset ; Half array elements with frequency 1 are part of the second subset ; If count of elements is exactly 1 are odd and has no element with frequency > 2 ; If count of elements that occurs exactly once are even ; Print the result ; If the count of elements has exactly 1 frequency are odd and there is an element with frequency greater than 2 ; Print the result ; Driver Codea"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void arrayPartition ( int [ ] a , int n ) { int [ ] ans = new int [ n ] ; int cnt = 0 ; int ind = 0 , flag = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( a [ i ] ) ) mp [ a [ i ] ] ++ ; else mp . Add ( a [ i ] , 1 ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( a [ i ] ) && mp [ a [ i ] ] == 1 ) cnt ++ ; if ( mp . ContainsKey ( a [ i ] ) && mp [ a [ i ] ] > 2 && flag == 0 ) { flag = 1 ; ind = i ; } } int p = ( cnt + 1 ) / 2 ; int ans1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans [ i ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( a [ i ] ) && mp [ a [ i ] ] == 1 && ans1 < p ) { ans [ i ] = 1 ; ans1 ++ ; } else if ( mp . ContainsKey ( a [ i ] ) && mp [ a [ i ] ] == 1 ) { ans [ i ] = 2 ; } } if ( cnt % 2 == 1 && flag == 0 ) { Console . Write ( - 1 ) ; return ; } if ( cnt % 2 == 0 ) { for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } else { for ( int i = 0 ; i < n ; i ++ ) { if ( ind == i ) Console . Write ( 2 + " ▁ " ) ; else Console . Write ( ans [ i ] + " ▁ " ) ; } } } public static void Main ( ) { int [ ] arr = { 1 , 1 , 2 , 3 , 4 , 4 } ; int N = arr . Length ; arrayPartition ( arr , N ) ; } }


"Partition an array such into maximum increasing segments | C # program to divide into maximum number of segments ; Returns the maximum number of sorted subarrays in a valid partition ; Find minimum value from right for every index ; Finding the shortest prefix such that all the elements in the prefix are less than or equal to the elements in the rest of the array . ; if current max is less than the right prefix min , we increase number of partitions . ; Driver code ; Find minimum value from right for every index"
Solution in C#:

using System ; class GFG { static int sorted_partitions ( int [ ] arr ) { int n = arr . Length ; int [ ] right_min = new int [ n + 1 ] ; right_min [ n ] = int . MaxValue ; for ( int i = n - 1 ; i >= 0 ; i -- ) { right_min [ i ] = Math . Min ( right_min [ i + 1 ] , arr [ i ] ) ; } int partitions = 0 ; for ( int current_max = arr [ 0 ] , i = 0 ; i < n ; i ++ ) { current_max = Math . Max ( current_max , arr [ i ] ) ; if ( current_max <= right_min [ i + 1 ] ) partitions ++ ; } return partitions ; } public static void Main ( ) { int [ ] arr = { 3 , 1 , 2 , 4 , 100 , 7 , 9 } ; int ans = sorted_partitions ( arr ) ; Console . WriteLine ( ans ) ; } }


"Partition array into minimum number of equal length subsets consisting of a single distinct value | C # program to implement the above approach ; Function to find the minimum count of subsets by partitioning the array with given conditions ; Store frequency of each distinct element of the array ; Traverse the array ; Update frequency of arr [ i ] ; Stores GCD of frequency of each distinct element of the array ; Update freqGCD ; Recursive function to return gcd of a and b ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int CntOfSubsetsByPartitioning ( int [ ] arr , int N ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( freq . ContainsKey ( arr [ i ] ) ) { freq [ arr [ i ] ] = freq [ arr [ i ] ] + 1 ; } else { freq . Add ( arr [ i ] , 1 ) ; } } int freqGCD = 0 ; foreach ( KeyValuePair < int , int > i in freq ) { freqGCD = __gcd ( freqGCD , i . Value ) ; } return ( N ) / freqGCD ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 4 , 3 , 2 , 1 } ; int N = arr . Length ; Console . Write ( CntOfSubsetsByPartitioning ( arr , N ) ) ; } }


"Partition array into two subsets with minimum Bitwise XOR between their maximum and minimum | C # program for the above approach ; Function to split the array into two subset such that the Bitwise XOR between the maximum of one subset and minimum of other is minimum ; Sort the array in increasing order ; Calculating the min Bitwise XOR between consecutive elements ; Return the final minimum Bitwise XOR ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call"
Solution in C#:

using System ; class GFG { static int splitArray ( int [ ] arr , int N ) { Array . Sort ( arr ) ; int result = Int32 . MaxValue ; for ( int i = 1 ; i < N ; i ++ ) { result = Math . Min ( result , arr [ i ] ^ arr [ i - 1 ] ) ; } return result ; } public static void Main ( ) { int [ ] arr = { 3 , 1 , 2 , 6 , 4 } ; int N = arr . Length ; Console . Write ( splitArray ( arr , N ) ) ; } }


"Partition first N natural number into two sets such that their sum is not coprime | C # implementation of the approach ; Function to find the required sets ; Impossible case ; Sum of first n - 1 natural numbers ; Driver code"
Solution in C#:

using System ; class GFG { static void find_set ( int n ) { if ( n <= 2 ) { Console . WriteLine ( " - 1" ) ; return ; } int sum1 = ( n * ( n - 1 ) ) / 2 ; int sum2 = n ; Console . WriteLine ( sum1 + " ▁ " + sum2 ) ; } public static void Main ( ) { int n = 8 ; find_set ( n ) ; } }


"Partition into two subarrays of lengths k and ( N | C # program to calculate max_difference between the sum of two subarrays of length k and N - k ; Function to calculate max_difference ; Sum of the array ; Sort the array in descending order ; Calculating max_difference ; Driver Code"
Solution in C#:

using System ; class GFG { static int maxDifference ( int [ ] arr , int N , int k ) { int M , S = 0 , S1 = 0 , max_difference = 0 ; for ( int i = 0 ; i < N ; i ++ ) S += arr [ i ] ; int temp ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] < arr [ j ] ) { temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } } M = Math . Max ( k , N - k ) ; for ( int i = 0 ; i < M ; i ++ ) S1 += arr [ i ] ; max_difference = S1 - ( S - S1 ) ; return max_difference ; } public static void Main ( ) { int [ ] arr = { 8 , 4 , 5 , 2 , 10 } ; int N = arr . Length ; int k = 2 ; Console . Write ( maxDifference ( arr , N , k ) ) ; } }


"Partition of a set into K subsets with equal sum using BitMask and DP | C # program to check if the given array can be partitioned into K subsets with equal sum ; Function to check whether K required partitions are possible or not ; Return true as the entire array is the answer ; If total number of partitions exceeds size of the array ; If the array sum is not divisible by K ; No such partitions are possible ; Required sum of each subset ; Initialize dp array with - 1 ; Sum of empty subset is zero ; Iterate over all subsets / masks ; If current mask is invalid , continue ; Iterate over all array elements ; Check if the current element can be added to the current subset / mask ; Transition ; Driver Code"
Solution in C#:

 using System ; class GFG { static bool isKPartitionPossible ( int [ ] arr , int N , int K ) { if ( K == 1 ) return true ; if ( N < K ) return false ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; if ( sum % K != 0 ) return false ; int target = sum / K ; int [ ] dp = new int [ ( 1 << 15 ) ] ; for ( int i = 0 ; i < ( 1 << N ) ; i ++ ) dp [ i ] = - 1 ; dp [ 0 ] = 0 ; for ( int mask = 0 ; mask < ( 1 << N ) ; mask ++ ) { if ( dp [ mask ] == - 1 ) continue ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( ( mask & ( 1 << i ) ) == 0 ) && dp [ mask ] + arr [ i ] <= target ) { dp [ mask ( 1 << i ) ] = ( dp [ mask ] + arr [ i ] ) % target ; } } } if ( dp [ ( 1 << N ) - 1 ] == 0 ) return true ; else return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 4 , 5 , 3 , 3 } ; int N = arr . Length ; int K = 3 ; if ( isKPartitionPossible ( arr , N , K ) ) { Console . Write ( " Partitions ▁ into ▁ equal ▁ " ) ; Console . Write ( " sum ▁ is ▁ possible .
 " ) ; } else { Console . Write ( " Partitions ▁ into ▁ equal ▁ " ) ; Console . Write ( " sum ▁ is ▁ not ▁ possible .
 " ) ; } } }


"Partition of a set into K subsets with equal sum | C # program to check whether an array can be partitioned into K subsets of equal sum ; * array - given input array subsetSum array - sum to store each subset of the array taken - boolean array to check whether element is taken into sum partition or not K - number of partitions needed N - total number of element in array curIdx - current subsetSum index limitIdx - lastIdx from where array element should be taken ; current index ( K - 2 ) represents ( K - 1 ) subsets of equal sum last partition will already remain with sum ' subset ' ; recursive call for next subsetition ; start from limitIdx and include elements into current partition ; if already taken , continue ; if temp is less than subset then only include the element and call recursively ; mark the element and include into current partition sum ; after recursive call unmark the element and remove from subsetition sum ; Method returns true if arr can be partitioned into K subsets with equal sum ; If K is 1 , then complete array will be our answer ; If total number of partitions are more than N , then division is not possible ; if array sum is not divisible by K then we can 't divide  array into K partitions ; the sum of each subset should be subset ( = sum / K ) ; Initialize sum of each subset from 0 ; mark all elements as not taken ; initialize first subsubset sum as last element of array and mark that as taken ; call recursive method to check K - substitution condition ; Driver code"
Solution in C#:

using System ; class GFG { static bool isKPartitionPossibleRec ( int [ ] arr , int [ ] subsetSum , bool [ ] taken , int subset , int K , int N , int curIdx , int limitIdx ) { if ( subsetSum [ curIdx ] == subset ) { if ( curIdx == K - 2 ) return true ; return isKPartitionPossibleRec ( arr , subsetSum , taken , subset , K , N , curIdx + 1 , N - 1 ) ; } for ( int i = limitIdx ; i >= 0 ; i -- ) { if ( taken [ i ] ) continue ; int tmp = subsetSum [ curIdx ] + arr [ i ] ; if ( tmp <= subset ) { taken [ i ] = true ; subsetSum [ curIdx ] += arr [ i ] ; bool nxt = isKPartitionPossibleRec ( arr , subsetSum , taken , subset , K , N , curIdx , i - 1 ) ; taken [ i ] = false ; subsetSum [ curIdx ] -= arr [ i ] ; if ( nxt ) return true ; } } return false ; } static bool isKPartitionPossible ( int [ ] arr , int N , int K ) { if ( K == 1 ) return true ; if ( N < K ) return false ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; if ( sum % K != 0 ) return false ; int subset = sum / K ; int [ ] subsetSum = new int [ K ] ; bool [ ] taken = new bool [ N ] ; for ( int i = 0 ; i < K ; i ++ ) subsetSum [ i ] = 0 ; for ( int i = 0 ; i < N ; i ++ ) taken [ i ] = false ; subsetSum [ 0 ] = arr [ N - 1 ] ; taken [ N - 1 ] = true ; return isKPartitionPossibleRec ( arr , subsetSum , taken , subset , K , N , 0 , N - 1 ) ; } static public void Main ( ) { int [ ] arr = { 2 , 1 , 4 , 5 , 3 , 3 } ; int N = arr . Length ; int K = 3 ; if ( isKPartitionPossible ( arr , N , K ) ) Console . WriteLine ( " Partitions ▁ into ▁ equal ▁ sum ▁ is ▁ possible . " ) ; else Console . WriteLine ( " Partitions ▁ into ▁ equal ▁ sum ▁ is ▁ not ▁ possible . " ) ; } }


"Partition string into two substrings having maximum number of common non | C # program to implement the above approach ; Function to count maximum common non - repeating characters that can be obtained by partitioning the String into two non - empty subStrings ; Stores count of non - repeating characters present in both the subStrings ; Stores distinct characters in left subString ; Stores distinct characters in right subString ; Traverse left subString ; Insert S [ i ] into ls ; Traverse right subString ; Insert S [ i ] into rs ; Traverse distinct characters of left subString ; If current character is present in right subString ; Update cnt ; Return count ; Function to partition the String into two non - empty subStrings in all possible ways ; Stores maximum common distinct characters present in both the subString partitions ; Traverse the String ; Update ans ; Print count of maximum common non - repeating characters ; Driver Code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static int countCommonChar ( int ind , String S ) { int cnt = 0 ; HashSet < char > ls = new HashSet < char > ( ) ; HashSet < char > rs = new HashSet < char > ( ) ; for ( int i = 0 ; i < ind ; ++ i ) { ls . Add ( S [ i ] ) ; } for ( int i = ind ; i < S . Length ; ++ i ) { rs . Add ( S [ i ] ) ; } foreach ( char v in ls ) { if ( rs . Contains ( v ) ) { ++ cnt ; } } return cnt ; } static void partitionStringWithMaxCom ( String S ) { int ans = 0 ; for ( int i = 1 ; i < S . Length ; ++ i ) { ans = Math . Max ( ans , countCommonChar ( i , S ) ) ; } Console . Write ( ans + "
 " ) ; } public static void Main ( String [ ] args ) { String str = " aabbca " ; partitionStringWithMaxCom ( str ) ; } }


"Partition the array in K segments such that bitwise AND of individual segment sum is maximized | C # program to find maximum possible AND ; Function to check whether a k - segment partition is possible such that bitwise AND is ' mask ' ; dp [ i , j ] stores whether it is possible to partition first i elements into j - segments such that all j - segments are ' good ' ; Initialising dp ; Filling dp in bottom - up manner ; Finding a cut such that first l elements can be partitioned into j - 1 ' good ' segments and arr [ l + 1 ] + ... + arr [ i ] is a ' good ' segment ; Function to find maximum possible AND ; Array to store prefix sums ; Maximum no of bits in the possible answer ; This will store the final answer ; Constructing answer greedily selecting from the higher most bit ; Checking if array can be partitioned such that the bitwise AND is ans | ( 1 << i ) ; if possible , update the answer ; Return the final answer ; Driver code ; n = 11 , first element is zero to make array 1 based indexing . So , number of elements are 10 ; Function call"
Solution in C#:

using System ; class GFG { static Boolean checkpossible ( int mask , int [ ] arr , int [ ] prefix , int n , int k ) { int i , j ; Boolean [ , ] dp = new Boolean [ n + 1 , k + 1 ] ; for ( i = 0 ; i < n + 1 ; i ++ ) { for ( j = 0 ; j < k + 1 ; j ++ ) { dp [ i , j ] = false ; } } dp [ 0 , 0 ] = true ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= k ; j ++ ) { for ( int l = i - 1 ; l >= 0 ; -- l ) { if ( dp [ l , j - 1 ] && ( ( ( prefix [ i ] - prefix [ l ] ) & mask ) == mask ) ) { dp [ i , j ] = true ; break ; } } } } return dp [ n , k ] ; } static int Partition ( int [ ] arr , int n , int k ) { int [ ] prefix = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] + arr [ i ] ; } int LOGS = 20 ; int ans = 0 ; for ( int i = LOGS ; i >= 0 ; -- i ) { if ( checkpossible ( ans | ( 1 << i ) , arr , prefix , n , k ) ) { ans = ans | ( 1 << i ) ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 1 , 2 , 7 , 10 , 23 , 21 , 6 , 8 , 7 , 3 } ; int k = 2 ; int n = arr . Length - 1 ; Console . WriteLine ( Partition ( arr , n , k ) ) ; } }


"Partition the array into three equal sum segments | C # implementation of the approach ; First segment 's end index ; Third segment 's start index ; This function returns true if the array can be divided into three equal sum segments ; Prefix Sum Array ; Suffix Sum Array ; Stores the total sum of the array ; We can also take pre [ pos2 - 1 ] - pre [ pos1 ] == total_sum / 3 here . ; Driver Code"
Solution in C#:

using System ; class GFG { public static int pos1 = - 1 ; public static int pos2 = - 1 ; public static bool equiSumUtil ( int [ ] arr ) { int n = arr . Length ; int [ ] pre = new int [ n ] ; int sum = 0 , i ; for ( i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; pre [ i ] = sum ; } int [ ] suf = new int [ n ] ; sum = 0 ; for ( i = n - 1 ; i >= 0 ; i -- ) { sum += arr [ i ] ; suf [ i ] = sum ; } int total_sum = sum ; int j = n - 1 ; i = 0 ; while ( i < j - 1 ) { if ( pre [ i ] == total_sum / 3 ) { pos1 = i ; } if ( suf [ j ] == total_sum / 3 ) { pos2 = j ; } if ( pos1 != - 1 && pos2 != - 1 ) { if ( suf [ pos1 + 1 ] - suf [ pos2 ] == total_sum / 3 ) { return true ; } else { return false ; } } if ( pre [ i ] < suf [ j ] ) { i ++ ; } else { j -- ; } } return false ; } public static void equiSum ( int [ ] arr ) { bool ans = equiSumUtil ( arr ) ; if ( ans ) { Console . Write ( " First ▁ Segment ▁ : ▁ " ) ; for ( int i = 0 ; i <= pos1 ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; Console . Write ( " Second ▁ Segment ▁ : ▁ " ) ; for ( int i = pos1 + 1 ; i < pos2 ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; Console . Write ( " Third ▁ Segment ▁ : ▁ " ) ; for ( int i = pos2 ; i < arr . Length ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; } else { Console . WriteLine ( " Array ▁ cannot ▁ be ▁ " + " divided ▁ into ▁ three ▁ equal ▁ sum ▁ segments " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 6 , 2 , 7 , 1 , 2 , 8 } ; equiSum ( arr ) ; } }


"Partition the array into two odd length groups with minimized absolute difference between their median | C # program to minimise the median between partition array ; Function to find minimise the median between partition array ; Sort the given array [ ] arr ; Return the difference of two middle element of the [ ] arr ; Driver Code ; Size of [ ] arr ; Function that returns the minimum the absolute difference between median of partition array"
Solution in C#:

using System ; class GFG { static int minimiseMedian ( int [ ] arr , int n ) { Array . Sort ( arr ) ; return Math . Abs ( arr [ n / 2 ] - arr [ ( n / 2 ) - 1 ] ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 15 , 25 , 35 , 50 } ; int n = arr . Length ; Console . WriteLine ( minimiseMedian ( arr , n ) ) ; } }


"Partition the digits of an integer such that it satisfies a given condition | C # implementation of the approach ; Function to generate sequence from the given String ; Initialize vector to store sequence ; First add all the digits of group A from left to right ; Then add all the digits of group B from left to right ; Return the sequence ; Function that returns true if the sequence is non - decreasing ; Initialize result ; Function to partition the digits of an integer such that it satisfies the given conditions ; Convert the integer to String ; Length of the String ; Array to store the digits ; Storing the digits of X in array ; Initialize the result ; Loop through the digits ; Put into group A if digit less than D ; Put into group B if digit greater than D ; Put into group C if digit equal to D ; Loop through the digits to decide for group C digits ; Set flag equal to true if group B digit present ; If flag is true put in group A or else put in B ; Generate the sequence from partition ; Check if the sequence is non decreasing ; Return - 1 if no such partition is possible ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < int > makeSeq ( String s , int [ ] a ) { List < int > seq = new List < int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) if ( s [ i ] == ' A ' ) seq . Add ( a [ i ] ) ; for ( int i = 0 ; i < s . Length ; i ++ ) if ( s [ i ] == ' B ' ) seq . Add ( a [ i ] ) ; return seq ; } static bool checkSeq ( List < int > v ) { bool check = true ; for ( int i = 1 ; i < v . Count ; i ++ ) if ( v [ i ] < v [ i - 1 ] ) check = false ; return check ; } static String digitPartition ( int X ) { String num = String . Join ( " " , X ) ; int l = num . Length ; int [ ] a = new int [ l ] ; for ( int i = 0 ; i < l ; i ++ ) a [ i ] = ( num [ i ] - '0' ) ; for ( int D = 0 ; D < 10 ; D ++ ) { String res = " " ; for ( int i = 0 ; i < l ; i ++ ) { if ( a [ i ] < D ) res += ' A ' ; else if ( a [ i ] > D ) res += ' B ' ; else res += ' C ' ; } bool flag = false ; for ( int i = 0 ; i < l ; i ++ ) { if ( res [ i ] == ' B ' ) flag = true ; if ( res [ i ] == ' C ' ) res = res . Substring ( 0 , i ) + ( flag ? ' A ' : ' B ' ) + res . Substring ( i + 1 ) ; } List < int > seq = makeSeq ( res , a ) ; if ( checkSeq ( seq ) ) return res ; } return " - 1" ; } public static void Main ( String [ ] args ) { int X = 777147777 ; Console . Write ( digitPartition ( X ) ) ; } }


"Partition the string in two parts such that both parts have at least k different characters | C # implementation of the approach ; Function to find the partition of the string such that both parts have at least k different characters ; Length of the string ; To check if the current character is already found ; Count number of different characters in the left part ; If current character is not already found , increase cnt by 1 ; If count becomes equal to k , we 've  got the first part, therefore,  store current index and break the loop ; Increment i by 1 ; Clear the map ; Assign cnt as 0 ; If the current character is not already found , increase cnt by 1 ; If cnt becomes equal to k , the second part also have k different characters so break it ; If the second part has less than k different characters , then print " Not ▁ Possible " ; Otherwise print both parts ; Driver code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void division_of_string ( char [ ] str , int k ) { int n = str . Length ; Dictionary < char , bool > has = new Dictionary < char , bool > ( ) ; int ans = 0 , cnt = 0 , i = 0 ; while ( i < n ) { if ( ! has . ContainsKey ( str [ i ] ) ) { cnt ++ ; has . Add ( str [ i ] , true ) ; } if ( cnt == k ) { ans = i ; break ; } i ++ ; } i ++ ; has . Clear ( ) ; cnt = 0 ; while ( i < n ) { if ( ! has . ContainsKey ( str [ i ] ) ) { cnt ++ ; has . Add ( str [ i ] , true ) ; } if ( cnt == k ) { break ; } i ++ ; } if ( cnt < k ) { Console . WriteLine ( " Not ▁ possible " ) ; } else { i = 0 ; while ( i <= ans ) { Console . Write ( str [ i ] ) ; i ++ ; } Console . WriteLine ( " " ) ; while ( i < n ) { Console . Write ( str [ i ] ) ; i ++ ; } Console . WriteLine ( " " ) ; } Console . WriteLine ( " " ) ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; int k = 4 ; division_of_string ( str . ToCharArray ( ) , k ) ; } }


"Partitioning a linked list around a given value and keeping the original order | C # program to partition a linked list around a given value . ; Link list Node ; A utility function to create a new node ; Function to make two separate lists and return head after concatenating ; Let us initialize first and last nodes of three linked lists 1 ) Linked list of values smaller than x . 2 ) Linked list of values equal to x . 3 ) Linked list of values greater than x . ; Now iterate original list and connect nodes of appropriate linked lists . ; If current node is equal to x , append it to the list of x values ; If current node is less than X , append it to the list of smaller values ; Append to the list of greater values ; Fix end of greater linked list to NULL if this list has some nodes ; If smaller list is empty ; If smaller list is not empty and equal list is empty ; If both smaller and equal list are non - empty ; Function to print linked list ; Driver code ; Start with the empty list"
Solution in C#:

using System ; public class GfG { public class Node { public int data ; public Node next ; } static Node newNode ( int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } static Node partition ( Node head , int x ) { Node smallerHead = null , smallerLast = null ; Node greaterLast = null , greaterHead = null ; Node equalHead = null , equalLast = null ; while ( head != null ) { if ( head . data == x ) { if ( equalHead == null ) equalHead = equalLast = head ; else { equalLast . next = head ; equalLast = equalLast . next ; } } else if ( head . data < x ) { if ( smallerHead == null ) smallerLast = smallerHead = head ; else { smallerLast . next = head ; smallerLast = head ; } } else { if ( greaterHead == null ) greaterLast = greaterHead = head ; else { greaterLast . next = head ; greaterLast = head ; } } head = head . next ; } if ( greaterLast != null ) greaterLast . next = null ; if ( smallerHead == null ) { if ( equalHead == null ) return greaterHead ; equalLast . next = greaterHead ; return equalHead ; } if ( equalHead == null ) { smallerLast . next = greaterHead ; return smallerHead ; } smallerLast . next = equalHead ; equalLast . next = greaterHead ; return smallerHead ; } static void printList ( Node head ) { Node temp = head ; while ( temp != null ) { Console . Write ( temp . data + " ▁ " ) ; temp = temp . next ; } } public static void Main ( ) { Node head = newNode ( 10 ) ; head . next = newNode ( 4 ) ; head . next . next = newNode ( 5 ) ; head . next . next . next = newNode ( 30 ) ; head . next . next . next . next = newNode ( 2 ) ; head . next . next . next . next . next = newNode ( 50 ) ; int x = 3 ; head = partition ( head , x ) ; printList ( head ) ; } }


"Partitioning into two contiguous element subarrays with equal sums | C # Program to find the minimum element to be added such that the array can be partitioned into two contiguous subarrays with equal sums ; Structure to store the minimum element and its position ; initialize prefix and suffix sum arrays with 0 ; add current element to Sum ; add current element to Sum ; initialize the minimum element to be a large value ; check for the minimum absolute difference between current prefix sum and the next suffix sum element ; if prefixsum has a greater value then position is the next element , else it 's the same element. ; return the data in struct . ; Driver Code"
Solution in C#:

 using System ; class GFG { public class data { public int element ; public int position ; } ; static data findMinElement ( int [ ] arr , int n ) { data result = new data ( ) ; int [ ] prefixSum = new int [ n ] ; int [ ] suffixSum = new int [ n ] ; prefixSum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] ; } suffixSum [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { suffixSum [ i ] = suffixSum [ i + 1 ] + arr [ i ] ; } int min = suffixSum [ 0 ] ; int pos = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( Math . Abs ( suffixSum [ i + 1 ] - prefixSum [ i ] ) < min ) { min = Math . Abs ( suffixSum [ i + 1 ] - prefixSum [ i ] ) ; if ( suffixSum [ i + 1 ] < prefixSum [ i ] ) pos = i + 1 ; else pos = i ; } } result . element = min ; result . position = pos ; return result ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 1 , 2 , 3 , 4 } ; int n = arr . Length ; data values ; values = findMinElement ( arr , n ) ; Console . WriteLine ( " Minimum ▁ element ▁ : ▁ " + values . element + "
 Position ▁ : ▁ " + values . position ) ; } }


"Partitions possible such that the minimum element divides all the other elements of the partition | C # implementation of the approach ; Function to return the count partitions possible from the given array such that the minimum element of any partition divides all the other elements of that partition ; Initialize the count variable ; Find the minimum element ; Break if no minimum element present ; Increment the count if minimum element present ; Replace all the element divisible by min_elem ; Driver code"
Solution in C#:

using System ; class GFG { static int INT_MAX = int . MaxValue ; static int min_element ( int [ ] A , int N ) { int min = A [ 0 ] ; int i ; for ( i = 1 ; i < N ; i ++ ) { if ( min > A [ i ] ) { min = A [ i ] ; } } return min ; } static int countPartitions ( int [ ] A , int N ) { int count = 0 ; int i , j ; for ( i = 0 ; i < N ; i ++ ) { int min_elem = min_element ( A , N ) ; if ( min_elem == INT_MAX ) break ; count ++ ; for ( j = 0 ; j < N ; j ++ ) { if ( A [ j ] % min_elem == 0 ) A [ j ] = INT_MAX ; } } return count ; } public static void Main ( ) { int [ ] arr = { 7 , 6 , 5 , 4 , 3 , 2 , 2 , 3 } ; int N = arr . Length ; Console . WriteLine ( countPartitions ( arr , N ) ) ; } }


"Path length having maximum number of bends | C # program to find path length having maximum number of bends ; structure node ; Utility function to create a new node ; Recursive function to calculate the path length having maximum number of bends . The following are parameters for this function . node - . pointer to the current node dir - . determines whether the current node is left or right child of it 's parent node bends -. number of bends so far in the current path. maxBends -. maximum number of bends in a path from root to leaf soFar -. length of the current path so far traversed len -. length of the path having maximum number of bends  ; Base Case ; Leaf node ; Recurring for both left and right child ; Helper function to call findMaxBendsUtil ( ) ; Call for left subtree of the root ; Call for right subtree of the root ; Include the root node as well in the path length ; Driver code ; Constructed binary tree is 10 / \ 8 2 / \ / 3 5 2 \ 1 / 9"
Solution in C#:

using System ; public class GFG { public class Node { public int key ; public Node left ; public Node right ; } ; static Node newNode ( int key ) { Node node = new Node ( ) ; node . left = null ; node . right = null ; node . key = key ; return node ; } static int maxBends ; static int len ; static void findMaxBendsUtil ( Node node , char dir , int bends , int soFar ) { if ( node == null ) return ; if ( node . left == null && node . right == null ) { if ( bends > maxBends ) { maxBends = bends ; len = soFar ; } } else { if ( dir == ' l ' ) { findMaxBendsUtil ( node . left , dir , bends , soFar + 1 ) ; findMaxBendsUtil ( node . right , ' r ' , bends + 1 , soFar + 1 ) ; } else { findMaxBendsUtil ( node . right , dir , bends , soFar + 1 ) ; findMaxBendsUtil ( node . left , ' l ' , bends + 1 , soFar + 1 ) ; } } } static int findMaxBends ( Node node ) { if ( node == null ) return 0 ; len = 0 ; maxBends = - 1 ; int bends = 0 ; if ( node . left != null ) findMaxBendsUtil ( node . left , ' l ' , bends , 1 ) ; if ( node . right != null ) findMaxBendsUtil ( node . right , ' r ' , bends , 1 ) ; len ++ ; return len ; } public static void Main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 8 ) ; root . right = newNode ( 2 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 2 ) ; root . right . left . right = newNode ( 1 ) ; root . right . left . right . left = newNode ( 9 ) ; Console . Write ( findMaxBends ( root ) - 1 ) ; } }


"Path traversed using exactly M coins in K jumps | C # program to print the path using exactly K jumps and M coins ; Function that print the path using exactly K jumps and M coins ; If no path exists ; It decides which box to be jump ; It decides whether to jump on left side or to jump on right side ; Print the path ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static void print_path ( int N , int jump , int coin ) { if ( jump > coin || jump * ( N - 1 ) < coin ) { Console . WriteLine ( " - 1" ) ; } else { int pos = 1 ; while ( jump > 0 ) { int tmp = Math . Min ( N - 1 , coin - ( jump - 1 ) ) ; if ( pos + tmp <= N ) { pos += tmp ; } else { pos -= tmp ; } Console . Write ( pos + " ▁ " ) ; coin -= tmp ; jump -= 1 ; } } } public static void Main ( String [ ] args ) { int N = 5 , K = 4 , M = 12 ; print_path ( N , K , M ) ; } }


"Path with maximum average value | C # Code for Path with maximum average value ; method returns maximum average of all path of cost matrix ; Initialize first column of total cost ( dp ) array ; Initialize first row of dp array ; Construct rest of the dp array ; divide maximum sum by constant path length : ( 2 N - 1 ) for getting average ; Driver Code"
Solution in C#:

using System ; class GFG { public static double maxAverageOfPath ( int [ , ] cost , int N ) { int [ , ] dp = new int [ N + 1 , N + 1 ] ; dp [ 0 , 0 ] = cost [ 0 , 0 ] ; for ( int i = 1 ; i < N ; i ++ ) dp [ i , 0 ] = dp [ i - 1 , 0 ] + cost [ i , 0 ] ; for ( int j = 1 ; j < N ; j ++ ) dp [ 0 , j ] = dp [ 0 , j - 1 ] + cost [ 0 , j ] ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 1 ; j < N ; j ++ ) dp [ i , j ] = Math . Max ( dp [ i - 1 , j ] , dp [ i , j - 1 ] ) + cost [ i , j ] ; return ( double ) dp [ N - 1 , N - 1 ] / ( 2 * N - 1 ) ; } public static void Main ( ) { int [ , ] cost = { { 1 , 2 , 3 } , { 6 , 5 , 4 } , { 7 , 3 , 9 } } ; Console . Write ( maxAverageOfPath ( cost , 3 ) ) ; } }


"Path with maximum product in 2 | C # Program to find maximum product path from ( 0 , 0 ) to ( N - 1 , M - 1 ) ; Function to find maximum product ; It will store the maximum product till a given cell . ; It will store the minimum product till a given cell ( for - ve elements ) ; If we are at topmost or leftmost , just copy the elements . ; If we 're not at the  above, we can consider  the above value. ; If we 're not on the  leftmost, we can consider  the left value. ; Store max & min product till i , j . ; Return the max product path from 0 , 0. N - 1 , M - 1. ; Driver Code ; Print maximum product from ( 0 , 0 ) to ( N - 1 , M - 1 )"
Solution in C#:

 using System ; class GFG { static readonly int N = 3 ; static readonly int M = 3 ; static int maxProductPath ( int [ , ] arr ) { int [ , ] maxPath = new int [ N , M ] ; int [ , ] minPath = new int [ N , M ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { int minVal = int . MaxValue ; int maxVal = int . MinValue ; if ( i == 0 && j == 0 ) { maxVal = arr [ i , j ] ; minVal = arr [ i , j ] ; } if ( i > 0 ) { int tempMax = Math . Max ( maxPath [ i - 1 , j ] * arr [ i , j ] , minPath [ i - 1 , j ] * arr [ i , j ] ) ; maxVal = Math . Max ( maxVal , tempMax ) ; int tempMin = Math . Min ( maxPath [ i - 1 , j ] * arr [ i , j ] , minPath [ i - 1 , j ] * arr [ i , j ] ) ; minVal = Math . Min ( minVal , tempMin ) ; } if ( j > 0 ) { int tempMax = Math . Max ( maxPath [ i , j - 1 ] * arr [ i , j ] , minPath [ i , j - 1 ] * arr [ i , j ] ) ; maxVal = Math . Max ( maxVal , tempMax ) ; int tempMin = Math . Min ( maxPath [ i , j - 1 ] * arr [ i , j ] , minPath [ i , j - 1 ] * arr [ i , j ] ) ; minVal = Math . Min ( minVal , tempMin ) ; } maxPath [ i , j ] = maxVal ; minPath [ i , j ] = minVal ; } } return maxPath [ N - 1 , M - 1 ] ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 1 , - 2 , 3 } , { 4 , - 5 , 6 } , { - 7 , - 8 , 9 } } ; Console . Write ( maxProductPath ( arr ) + "
 " ) ; } }


"Paths from entry to exit in matrix and maximum path sum | C # implementation of the approach ; Recursive function to return the total paths from grid [ i ] [ j ] to grid [ n - 1 ] [ n - 1 ] ; Out of bounds ; If the current state hasn 't been solved before ; Only valid move is right ; Only valid move is down ; Right and down , both are valid moves ; Recursive function to return the maximum sum along the path from grid [ i , j ] to grid [ n - 1 , n - 1 ] ; Out of bounds ; If the current state hasn 't been solved before ; Only valid move is right ; Only valid move is down ; Right and down , both are valid moves ; Driver code ; Fill the dp [ , ] array with - 1 ; When source and destination are same then there is only 1 path ; Print the count of paths from grid [ 0 , 0 ] to grid [ n - 1 , n - 1 ] ; Fill the dp [ , ] array again with - 1 ; When source and destination are same then the sum is grid [ n - 1 , n - 1 ] ; Print the maximum sum among all the paths from grid [ 0 , 0 ] to grid [ n - 1 , n - 1 ]"
Solution in C#:

using System ; class GFG { static int totalPaths ( int i , int j , int n , int [ , ] grid , int [ , ] dp ) { if ( i < 0 j < 0 i >= n j >= n ) return 0 ; if ( dp [ i , j ] == - 1 ) { if ( grid [ i , j ] == 1 ) dp [ i , j ] = totalPaths ( i , j + 1 , n , grid , dp ) ; else if ( grid [ i , j ] == 2 ) dp [ i , j ] = totalPaths ( i + 1 , j , n , grid , dp ) ; else dp [ i , j ] = totalPaths ( i , j + 1 , n , grid , dp ) + totalPaths ( i + 1 , j , n , grid , dp ) ; } return dp [ i , j ] ; } static int maxSumPath ( int i , int j , int n , int [ , ] grid , int [ , ] dp ) { if ( i < 0 j < 0 i >= n j >= n ) return 0 ; if ( dp [ i , j ] == - 1 ) { if ( grid [ i , j ] == 1 ) dp [ i , j ] = grid [ i , j ] + maxSumPath ( i , j + 1 , n , grid , dp ) ; else if ( grid [ i , j ] == 2 ) dp [ i , j ] = grid [ i , j ] + maxSumPath ( i + 1 , j , n , grid , dp ) ; else dp [ i , j ] = grid [ i , j ] + Math . Max ( maxSumPath ( i , j + 1 , n , grid , dp ) , maxSumPath ( i + 1 , j , n , grid , dp ) ) ; } return dp [ i , j ] ; } public static void Main ( String [ ] args ) { int [ , ] grid = { { 1 , 1 , 3 , 2 , 1 } , { 3 , 2 , 2 , 1 , 2 } , { 1 , 3 , 3 , 1 , 3 } , { 1 , 2 , 3 , 1 , 2 } , { 1 , 1 , 1 , 3 , 1 } } ; int n = grid . GetLength ( 0 ) ; int [ , ] dp = new int [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) dp [ i , j ] = - 1 ; dp [ n - 1 , n - 1 ] = 1 ; Console . WriteLine ( " Total ▁ paths : ▁ " + totalPaths ( 0 , 0 , n , grid , dp ) ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) dp [ i , j ] = - 1 ; dp [ n - 1 , n - 1 ] = grid [ n - 1 , n - 1 ] ; Console . WriteLine ( " Maximum ▁ sum : ▁ " + maxSumPath ( 0 , 0 , n , grid , dp ) ) ; } }