"Convert given string to another by minimum replacements of subsequences by its smallest character | C # program for the above approach ; Function to return the minimum number of operation ; Storing data ; Initialize both arrays ; Stores the index of character ; Filling str1array , convChar and hashmap convertMap . ; Not possible to convert ; Calculate result Initializing return values ; Iterating the character from the end ; Increment the number of operations ; Not possible to convert ; To check whether the final element has been added in set S or not . ; Check if v1 [ j ] is present in hashmap or not ; Already converted then then continue ; Not possible to convert ; Print the result ; Driver code ; Given strings ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void transformString ( string str1 , string str2 ) { int N = str1 . Length ; List < List < int > > convChar = new List < List < int > > ( ) ; List < List < int > > str1array = new List < List < int > > ( ) ; for ( int i = 0 ; i < 26 ; i ++ ) { convChar . Add ( new List < int > ( ) ) ; str1array . Add ( new List < int > ( ) ) ; } Dictionary < int , char > convertMap = new Dictionary < int , char > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { str1array [ str1 [ i ] - ' a ' ] . Add ( i ) ; } for ( int i = 0 ; i < N ; i ++ ) { if ( str1 [ i ] < str2 [ i ] ) { Console . WriteLine ( - 1 ) ; return ; } else if ( str1 [ i ] == str2 [ i ] ) continue ; else { convChar [ str2 [ i ] - ' a ' ] . Add ( i ) ; convertMap [ i ] = str2 [ i ] ; } } int ret = 0 ; List < List < int > > retv = new List < List < int > > ( ) ; for ( int i = 25 ; i >= 0 ; i -- ) { List < int > v = convChar [ i ] ; if ( v . Count == 0 ) continue ; ret ++ ; List < int > v1 = str1array [ i ] ; if ( v1 . Count == 0 ) { Console . WriteLine ( - 1 ) ; return ; } bool isScompleted = false ; for ( int j = 0 ; j < v1 . Count ; j ++ ) { if ( convertMap . ContainsKey ( v1 [ j ] ) ) { char a = convertMap [ v1 [ j ] ] ; if ( a > i + ' a ' ) continue ; else { v . Add ( v1 [ j ] ) ; isScompleted = true ; retv . Add ( v ) ; break ; } } else { v . Add ( v1 [ j ] ) ; isScompleted = true ; retv . Add ( v ) ; break ; } } if ( ! isScompleted ) { Console . WriteLine ( - 1 ) ; return ; } } Console . WriteLine ( ret ) ; } static void Main ( ) { string A = " abcab " ; string B = " aabab " ; transformString ( A , B ) ; } }


"Convert left | C # program to convert left - right to down - right representation of binary tree ; A Binary Tree Node ; An Iterative level order traversal based function to convert left - right to down - right representation . ; Base Case ; Recursively convert left an right subtrees ; If left child is NULL , make right child as left as it is the first child . ; If left child is NOT NULL , then make right child as right of left child ; Set root 's right as NULL  ; A utility function to traverse a tree stored in down - right form . ; Utility function to create a new tree node ; Driver Code ; 1 / \ 2 3 / \ 4 5 / / \ 6 7 8"
Solution in C#:

using System ; class GFG { public class node { public int key ; public node left , right ; public node ( int key ) { this . key = key ; this . left = null ; this . right = null ; } } public static void convert ( node root ) { if ( root == null ) { return ; } convert ( root . left ) ; convert ( root . right ) ; if ( root . left == null ) { root . left = root . right ; } else { root . left . right = root . right ; } root . right = null ; } public static void downRightTraversal ( node root ) { if ( root != null ) { Console . Write ( root . key + " ▁ " ) ; downRightTraversal ( root . right ) ; downRightTraversal ( root . left ) ; } } public static node newNode ( int key ) { node temp = new node ( 0 ) ; temp . key = key ; temp . left = null ; temp . right = null ; return temp ; } public static void Main ( string [ ] args ) { node root = new node ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . right . left = newNode ( 4 ) ; root . right . right = newNode ( 5 ) ; root . right . left . left = newNode ( 6 ) ; root . right . right . left = newNode ( 7 ) ; root . right . right . right = newNode ( 8 ) ; convert ( root ) ; Console . WriteLine ( " Traversal ▁ of ▁ the ▁ tree ▁ " + " converted ▁ to ▁ down - right ▁ form " ) ; downRightTraversal ( root ) ; } }


"Convert numbers into binary representation and add them without carry | C # program to add two binary number without carry ; Function returns sum of both the binary number without carry ; XOR of N and M ; Driver code"
Solution in C#:

using System ; class GFG { static int NoCarrySum ( int N , int M ) { return N ^ M ; } static public void Main ( String [ ] args ) { int N = 37 ; int M = 12 ; Console . Write ( NoCarrySum ( N , M ) ) ; } }


"Convert string X to an anagram of string Y with minimum replacements | C # program to convert string X to string Y which minimum number of changes . ; Function that converts string X into lexicographically smallest anagram of string Y with minimal changes ; Counting frequency of characters in each string . ; We maintain two more counter arrays ctrx [ ] and ctry [ ] Ctrx [ ] maintains the count of extra elements present in string X than string Y Ctry [ ] maintains the count of characters missing from string X which should be present in string Y . ; This means that we cannot edit the current character as it 's frequency  in string X is equal to or less  than the frequency in string Y.  Thus, we go to the next position ; Here , we try to find that character , which has more frequency in string Y and less in string X . We try to find this character in lexicographical order so that we get lexicographically smaller string ; This portion deals with the lexicographical property . Now , we put a character in string X when either this character has smaller value than the character present there right now or if this is the last position for it to exchange , else we fix the character already present here in this position . ; Driver code"
Solution in C#:

using System ; class GFG { static readonly int MAX = 26 ; static void printAnagramAndChanges ( char [ ] X , char [ ] Y ) { int [ ] countx = new int [ MAX ] ; int [ ] county = new int [ MAX ] ; int [ ] ctrx = new int [ MAX ] ; int [ ] ctry = new int [ MAX ] ; int change = 0 ; int l = X . Length ; for ( int i = 0 ; i < l ; i ++ ) { countx [ X [ i ] - ' A ' ] ++ ; county [ Y [ i ] - ' A ' ] ++ ; } for ( int i = 0 ; i < MAX ; i ++ ) { if ( countx [ i ] > county [ i ] ) { ctrx [ i ] += ( countx [ i ] - county [ i ] ) ; } else if ( countx [ i ] < county [ i ] ) { ctry [ i ] += ( county [ i ] - countx [ i ] ) ; } change += Math . Abs ( county [ i ] - countx [ i ] ) ; } for ( int i = 0 ; i < l ; i ++ ) { if ( ctrx [ X [ i ] - ' A ' ] == 0 ) { continue ; } int j ; for ( j = 0 ; j < MAX ; j ++ ) { if ( ( ctry [ j ] ) > 0 ) { break ; } } if ( countx [ X [ i ] - ' A ' ] == ctrx [ X [ i ] - ' A ' ] X [ i ] - ' A ' > j ) { countx [ X [ i ] - ' A ' ] -- ; ctrx [ X [ i ] - ' A ' ] -- ; ctry [ j ] -- ; X [ i ] = ( char ) ( ' A ' + j ) ; } else { countx [ X [ i ] - ' A ' ] -- ; } } Console . WriteLine ( " Anagram ▁ : ▁ " + String . Join ( " " , X ) ) ; Console . WriteLine ( " Number ▁ of ▁ changes ▁ made ▁ : ▁ " + change / 2 ) ; } public static void Main ( String [ ] args ) { String x = " CDBABC " , y = " ADCABD " ; printAnagramAndChanges ( x . ToCharArray ( ) , y . ToCharArray ( ) ) ; } }


"Convert string to integer without using any in | C # program for the above approach ; Function to convert string to integer without using functions ; Initialize a variable ; Iterate till length of the string ; Subtract 48 from the current digit ; Print the answer ; Driver code ; Given string of number ; Function call"
Solution in C#:

using System ; class GFG { public static void convert ( string s ) { int num = 0 ; int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) num = num * 10 + ( ( int ) s [ i ] - 48 ) ; Console . Write ( num ) ; } public static void Main ( string [ ] args ) { string s = "123" ; convert ( s ) ; } }


"Convert the ASCII value sentence to its equivalent string | C # implementation of the approach ; Function to print the character sequence for the given ASCII sentence ; Append the current digit ; If num is within the required range ; Convert num to char ; Reset num to 0 ; Driver code"
Solution in C#:

using System ; class GFG { static void asciiToSentence ( String str , int len ) { int num = 0 ; for ( int i = 0 ; i < len ; i ++ ) { num = num * 10 + ( str [ i ] - '0' ) ; if ( num >= 32 && num <= 122 ) { char ch = ( char ) num ; Console . Write ( ch ) ; num = 0 ; } } } public static void Main ( ) { String str = "7110110110711510211111471101101107115" ; int len = str . Length ; asciiToSentence ( str , len ) ; } }


"Convert the given RGB color code to Hex color code | C # code to convert the given RGB color code to Hex color code ; Function to convert decimal to hexadecimal ; char array to store hexadecimal number ; Counter for hexadecimal number array ; Temporary variable to store remainder ; Storing remainder in temp variable . ; Check if temp < 10 ; Return the equivalent hexadecimal color code ; Function to convert the RGB code to Hex color code ; The hex color code doesn 't exist ; Driver code"
Solution in C#:

 using System ; class GFG { static string decToHexa ( int n ) { char [ ] hexaDeciNum = new char [ 2 ] ; int i = 0 ; while ( n != 0 ) { int temp = 0 ; temp = n % 16 ; if ( temp < 10 ) { hexaDeciNum [ i ] = ( char ) ( temp + 48 ) ; i ++ ; } else { hexaDeciNum [ i ] = ( char ) ( temp + 55 ) ; i ++ ; } n = n / 16 ; } string hexCode = " " ; if ( i == 2 ) { hexCode += hexaDeciNum [ 0 ] ; hexCode += hexaDeciNum [ 1 ] ; } else if ( i == 1 ) { hexCode = "0" ; hexCode += hexaDeciNum [ 0 ] ; } else if ( i == 0 ) hexCode = "00" ; return hexCode ; } static string convertRGBtoHex ( int R , int G , int B ) { if ( ( R >= 0 && R <= 255 ) && ( G >= 0 && G <= 255 ) && ( B >= 0 && B <= 255 ) ) { string hexCode = " # " ; hexCode += decToHexa ( R ) ; hexCode += decToHexa ( G ) ; hexCode += decToHexa ( B ) ; return hexCode ; } else return " - 1" ; } public static void Main ( string [ ] args ) { int R = 0 , G = 0 , B = 0 ; Console . Write ( convertRGBtoHex ( R , G , B ) + "
 " ) ; R = 255 ; G = 255 ; B = 255 ; Console . Write ( convertRGBtoHex ( R , G , B ) + "
 " ) ; R = 25 ; G = 56 ; B = 123 ; Console . Write ( convertRGBtoHex ( R , G , B ) + "
 " ) ; R = 2 ; G = 3 ; B = 4 ; Console . Write ( convertRGBtoHex ( R , G , B ) + "
 " ) ; R = 255 ; G = 255 ; B = 256 ; Console . Write ( convertRGBtoHex ( R , G , B ) + "
 " ) ; } }


"Convert the string into palindrome string by changing only one character | C # program to Check if it is possible to convert the string into palindrome string by changing only one character . ; Function to check if it is possible to convert the string into palindrome ; Counting number of characters that should be changed . ; If count of changes is less than or equal to 1 ; Driver Function"
Solution in C#:

using System ; class GFG { static bool checkPalindrome ( string str ) { int n = str . Length ; int count = 0 ; for ( int i = 0 ; i < n / 2 ; ++ i ) if ( str [ i ] != str [ n - i - 1 ] ) ++ count ; return ( count <= 1 ) ; } public static void Main ( ) { string str = " abccaa " ; if ( checkPalindrome ( str ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Convert to Strictly increasing integer array with minimum changes | C # program to find min elements to change so array is strictly increasing ; To find min elements to remove from array to make it strictly increasing ; Mark all elements of LIS as 1 ; Find LIS of array ; Return min changes for array to strictly increasing ; Driver Code"
Solution in C#:

using System ; class GFG { static int minRemove ( int [ ] arr , int n ) { int [ ] LIS = new int [ n ] ; int len = 0 ; for ( int i = 0 ; i < n ; i ++ ) LIS [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ i ] > arr [ j ] && ( i - j ) <= ( arr [ i ] - arr [ j ] ) ) LIS [ i ] = Math . Max ( LIS [ i ] , LIS [ j ] + 1 ) ; } len = Math . Max ( len , LIS [ i ] ) ; } return n - len ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 6 , 5 , 4 } ; int n = arr . Length ; Console . WriteLine ( minRemove ( arr , n ) ) ; } }


"Convert to a string that is repetition of a substring of k length | C # program to check if a string can be converted to a string that has repeated substrings of length k . ; Returns true if str can be converted to a string with k repeated substrings after replacing k characters . ; Length of string must be a multiple of k ; Map to store strings of length k and their counts ; If string is already a repetition of k substrings , return true . ; If number of distinct substrings is not 2 , then not possible to replace a string . ; One of the two distinct must appear exactly once . Either the first entry appears once , or it appears n / k - 1 times to make other substring appear once . ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool checkString ( String str , int k ) { int n = str . Length ; if ( n % k != 0 ) return false ; Dictionary < String , int > mp = new Dictionary < String , int > ( ) ; for ( int i = 0 ; i < n ; i += k ) { if ( ! mp . ContainsKey ( str . Substring ( i , k ) ) ) mp . Add ( str . Substring ( i , k ) , 1 ) ; else mp [ str . Substring ( i , k ) ] = mp [ str . Substring ( i , k ) ] + 1 ; } if ( mp . Count == 1 ) return true ; if ( mp . Count != 2 ) return false ; foreach ( KeyValuePair < String , int > entry in mp ) { if ( entry . Value == ( n / k - 1 ) entry . Value == 1 ) return true ; } return false ; } public static void Main ( String [ ] args ) { if ( checkString ( " abababcd " , 2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Convert undirected connected graph to strongly connected directed graph | C # program for the above approach ; To store the assigned Edges ; Flag variable to check Bridges ; Function to implement DFS Traversal ; Mark the current node as visited ; Update the order of node v ; Update the bridge_detect for node v ; Traverse the adjacency list of Node v ; Ignores if same edge is traversed ; Ignores the edge u -- > v as v -- > u is already processed ; Finds a back Edges , cycle present ; Update the bridge_detect [ v ] ; Else DFS traversal for current node in the adjacency list ; Update the bridge_detect [ v ] ; Store the current directed Edge ; Condition for Bridges ; Return flag ; Function to print the direction of edges to make graph SCCs ; Arrays to store the visited , bridge_detect and order of Nodes ; DFS Traversal from vertex 1 ; If flag is zero , then Bridge is present in the graph ; Else print the direction of Edges assigned ; Function to create graph ; Traverse the Edges ; Push the edges in an adjacency list ; Driver code ; N vertices and M Edges ; To create Adjacency List ; Create an undirected graph ; Function Call"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static ArrayList ans ; static int flag = 1 ; static int dfs ( ArrayList adj , int [ ] order , int [ ] bridge_detect , bool [ ] mark , int v , int l ) { mark [ v ] = true ; order [ v ] = order [ l ] + 1 ; bridge_detect [ v ] = order [ v ] ; for ( int i = 0 ; i < ( ( ArrayList ) adj [ v ] ) . Count ; i ++ ) { int u = ( int ) ( ( ArrayList ) adj [ v ] ) [ i ] ; if ( u == l ) { continue ; } if ( order [ v ] < order [ u ] ) { continue ; } if ( mark [ u ] ) { bridge_detect [ v ] = Math . Min ( order [ u ] , bridge_detect [ v ] ) ; } else { dfs ( adj , order , bridge_detect , mark , u , v ) ; } bridge_detect [ v ] = Math . Min ( bridge_detect [ u ] , bridge_detect [ v ] ) ; ans . Add ( new int [ ] { v , u } ) ; } if ( bridge_detect [ v ] == order [ v ] && l != 0 ) { flag = 0 ; } return flag ; } static void convert ( ArrayList adj , int n ) { int [ ] order = new int [ n ] ; int [ ] bridge_detect = new int [ n ] ; bool [ ] mark = new bool [ n ] ; int flag = dfs ( adj , order , bridge_detect , mark , 1 , 0 ) ; if ( flag == 0 ) { Console . Write ( " - 1" ) ; } else { foreach ( int [ ] it in ans ) { Console . WriteLine ( it [ 0 ] + " - > " + it [ 1 ] ) ; } } } static void createGraph ( int [ , ] Edges , ArrayList adj , int M ) { for ( int i = 0 ; i < M ; i ++ ) { int u = Edges [ i , 0 ] ; int v = Edges [ i , 1 ] ; ( ( ArrayList ) adj [ u ] ) . Add ( v ) ; ( ( ArrayList ) adj [ v ] ) . Add ( u ) ; } } public static void Main ( string [ ] args ) { int N = 5 , M = 6 ; int [ , ] Edges = { { 0 , 1 } , { 0 , 2 } , { 1 , 2 } , { 1 , 4 } , { 2 , 3 } , { 3 , 4 } } ; ArrayList adj = new ArrayList ( ) ; ans = new ArrayList ( ) ; for ( int i = 0 ; i < N ; i ++ ) adj . Add ( new ArrayList ( ) ) ; createGraph ( Edges , adj , M ) ; convert ( adj , N ) ; } }


"Converting Decimal Number lying between 1 to 3999 to Roman Numerals | C # Program for above approach ; Function to calculate roman equivalent ; storing roman values of digits from 0 - 9 when placed at different places ; Converting to roman ; Driver program to test above function"
Solution in C#:

using System ; class GFG { static String intToRoman ( int num ) { String [ ] m = { " " , " M " , " MM " , " MMM " } ; String [ ] c = { " " , " C " , " CC " , " CCC " , " CD " , " D " , " DC " , " DCC " , " DCCC " , " CM " } ; String [ ] x = { " " , " X " , " XX " , " XXX " , " XL " , " L " , " LX " , " LXX " , " LXXX " , " XC " } ; String [ ] i = { " " , " I " , " II " , " III " , " IV " , " V " , " VI " , " VII " , " VIII " , " IX " } ; String thousands = m [ num / 1000 ] ; String hundereds = c [ ( num % 1000 ) / 100 ] ; String tens = x [ ( num % 100 ) / 10 ] ; String ones = i [ num % 10 ] ; String ans = thousands + hundereds + tens + ones ; return ans ; } public static void Main ( ) { int number = 3549 ; Console . WriteLine ( intToRoman ( number ) ) ; } }


"Converting a Real Number ( between 0 and 1 ) to Binary String | C # program to Binary real number to String . ; Main function to convert Binary real number to String ; Check Number is Between 0 to 1 or Not ; Setting a limit on length : 32 characters , If the number cannot be represented accurately in binary with at most 32 characters ; It compare the number to . 5. ; Now it become 0.25 ; Driver Code ; double num1 = 0.625 ; Input value in Decimal"
Solution in C#:

using System ; class BinaryToString { static string printBinary ( double num ) { if ( num >= 1 num <= 0 ) return " ERROR " ; string binary = " " ; double frac = 0.5 ; binary += " . " ; while ( num > 0 ) { if ( binary . Length >= 32 ) return " ERROR " ; if ( num >= frac ) { binary += "1" ; num -= frac ; } else binary += "0" ; frac /= 2 ; } return binary ; } public static void Main ( ) { String output = printBinary ( num1 ) ; Console . WriteLine ( " ( 0" + output + " ) ▁ in ▁ base ▁ 2" ) ; double num2 = 0.72 ; output = printBinary ( num2 ) ; Console . WriteLine ( " ( " + output + " ) ▁ " ) ; } }


"Converting one string to other using append and delete last operations | C # Program to convert str1 to str2 in exactly k operations ; Returns true if it is possible to convert str1 to str2 using k operations . ; Case A ( i ) ; finding common length of both string ; Case A ( ii ) - ; Case B ; Driver program"
Solution in C#:

using System ; class GFG { static bool isConvertible ( string str1 , string str2 , int k ) { if ( ( str1 . Length + str2 . Length ) < k ) return true ; int commonLength = 0 ; for ( int i = 0 ; i < Math . Min ( str1 . Length , str2 . Length ) ; i ++ ) { if ( str1 == str2 ) commonLength ++ ; else break ; } if ( ( k - str1 . Length - str2 . Length + 2 * commonLength ) % 2 == 0 ) return true ; return false ; } public static void Main ( ) { string str1 = " geek " ; string str2 = " geek " ; int k = 7 ; if ( isConvertible ( str1 , str2 , k ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; str1 = " geeks " ; str2 = " geek " ; k = 5 ; if ( isConvertible ( str1 , str2 , k ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Coordinates of rectangle with given points lie inside | Program to find smallest rectangle to conquer all points ; function to print coordinate of smallest rectangle ; find Xmax and Xmin ; find Ymax and Ymin ; print all four coordinates ; Driver code"
Solution in C#:

using System . Linq ; using System ; public class GFG { static void printRect ( int [ ] X , int [ ] Y , int n ) { int Xmax = X . Max ( ) ; int Xmin = X . Min ( ) ; int Ymax = Y . Max ( ) ; int Ymin = Y . Min ( ) ; Console . WriteLine ( " { " + Xmin + " , ▁ " + Ymin + " } " ) ; Console . WriteLine ( " { " + Xmin + " , ▁ " + Ymax + " } " ) ; Console . WriteLine ( " { " + Xmax + " , ▁ " + Ymax + " } " ) ; Console . WriteLine ( " { " + Xmax + " , ▁ " + Ymin + " } " ) ; } static public void Main ( ) { int [ ] X = { 4 , 3 , 6 , 1 , - 1 , 12 } ; int [ ] Y = { 4 , 1 , 10 , 3 , 7 , - 1 } ; int n = X . Length ; printRect ( X , Y , n ) ; } }


"Coordinates of the last cell in a Matrix on which performing given operations exits from the Matrix | C # program for the above approach ; Function to check if the indices ( i , j ) are valid indices in a Matrix or not ; Cases for invalid cells ; Return true if valid ; Function to find indices of cells of a matrix from which traversal leads to out of the matrix ; Starting from cell ( 0 , 0 ) , traverse in right direction ; Stores direction changes ; Iterate until the current cell exceeds beyond the matrix ; Current index ; If the current cell is 1 ; Update arr [ i ] [ j ] = 0 ; Update indices according to the direction ; Otherwise ; Update indices according to the direction ; The exit cooridnates ; Driver Code ; Number of rdows ; Number of columns ; Given matrix arr [ ] [ ]"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static bool issafe ( int m , int n , int i , int j ) { if ( i < 0 ) return false ; if ( j < 0 ) return false ; if ( i >= m ) return false ; if ( j >= n ) return false ; return true ; } static int [ ] endpoints ( int [ , ] arr , int m , int n ) { int i = 0 ; int j = 0 ; int current_i = 0 ; int current_j = 0 ; char current_d = ' r ' ; Dictionary < char , char > rcd = new Dictionary < char , char > ( ) ; rcd [ ' l ' ] = ' u ' ; rcd [ ' u ' ] = ' r ' ; rcd [ ' r ' ] = ' d ' ; rcd [ ' d ' ] = ' l ' ; while ( issafe ( m , n , i , j ) ) { current_i = i ; current_j = j ; if ( arr [ i , j ] == 1 ) { char move_in = rcd [ current_d ] ; arr [ i , j ] = 0 ; if ( move_in == ' u ' ) i -= 1 ; else if ( move_in == ' d ' ) i += 1 ; else if ( move_in == ' l ' ) j -= 1 ; else if ( move_in == ' r ' ) j += 1 ; current_d = move_in ; } else { if ( current_d == ' u ' ) i -= 1 ; else if ( current_d == ' d ' ) i += 1 ; else if ( current_d == ' l ' ) j -= 1 ; else if ( current_d == ' r ' ) j += 1 ; } } return new int [ ] { current_i , current_j } ; } public static void Main ( string [ ] args ) { int M = 3 ; int N = 5 ; int [ , ] arr = { { 0 , 1 , 1 , 1 , 0 } , { 1 , 0 , 1 , 0 , 1 } , { 1 , 1 , 1 , 0 , 0 } } ; int [ ] p = endpoints ( arr , M , N ) ; Console . WriteLine ( " ( " + p [ 0 ] + " , ▁ " + p [ 1 ] + " ) " + "
 " ) ; } }


"Coplanarity of Two Lines in 3D Geometry | C # program implement the above approach ; Function to generate determinant ; Return the sum ; Driver Code ; Position vector of first line ; Direction ratios of line to which first line is parallel ; Position vectors of second line ; Direction ratios of line to which second line is parallel ; Determinant to check coplanarity ; If determinant is zero ; Otherwise"
Solution in C#:


"Cost Based Tower of Hanoi | C # implementation of the approach ; Function to initialize the dp table ; Initialize with maximum value ; Function to return the minimum cost ; Base case ; If problem is already solved , return the pre - calculated answer ; Number of the auxiliary disk ; Initialize the minimum cost as Infinity ; Calculationg the cost for first case ; Calculating the cost for second case ; Minimum of both the above cases ; Store it in the dp table ; Return the minimum cost ; Driver code"
Solution in C#:

using System ; class GFG { static int RODS = 3 ; static int N = 3 ; static int [ , , ] dp = new int [ N + 1 , RODS + 1 , RODS + 1 ] ; static void initialize ( ) { for ( int i = 0 ; i <= N ; i += 1 ) { for ( int j = 1 ; j <= RODS ; j ++ ) { for ( int k = 1 ; k <= RODS ; k += 1 ) { dp [ i , j , k ] = int . MaxValue ; } } } } static int mincost ( int idx , int src , int dest , int [ , ] costs ) { if ( idx > N ) return 0 ; if ( dp [ idx , src , dest ] != int . MaxValue ) return dp [ idx , src , dest ] ; int rem = 6 - ( src + dest ) ; int ans = int . MaxValue ; int case1 = costs [ src - 1 , dest - 1 ] + mincost ( idx + 1 , src , rem , costs ) + mincost ( idx + 1 , rem , dest , costs ) ; int case2 = costs [ src - 1 , rem - 1 ] + mincost ( idx + 1 , src , dest , costs ) + mincost ( idx + 1 , dest , src , costs ) + costs [ rem - 1 , dest - 1 ] + mincost ( idx + 1 , src , dest , costs ) ; ans = Math . Min ( case1 , case2 ) ; dp [ idx , src , dest ] = ans ; return ans ; } public static void Main ( String [ ] args ) { int [ , ] costs = { { 0 , 1 , 2 } , { 2 , 0 , 1 } , { 3 , 2 , 0 } } ; initialize ( ) ; Console . WriteLine ( mincost ( 1 , 1 , 3 , costs ) ) ; } }


"Cost of creating smallest subsequence with sum of difference between adjacent elements maximum |  ; Initializing cost = 0 ; To store the removed element ; This will store the sum of the subsequence ; Checking all the element of the vector ; Storing the value of arr [ i ] in temp variable ; If the situation like arr [ i - 1 ] < arr [ i ] < arr [ i + 1 ] or arr [ i - 1 ] > arr [ i ] > arr [ i + 1 ] occur remove arr [ i ] i . e , temp from sequence ; Insert the element in the set removedElements ; Storing the value of arr [ i ] in temp ; Taking the element not in removedElements ; Adding the value of elements of subsequence ; If we have to remove the element then we need to add the cost associated with the element ; Printing the sum of the subsequence with minimum length possible ; Printing the cost incurred in creating subsequence ; Driver code ; Calling the function"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static void costOfSubsequence ( int N , int [ ] arr , int [ ] costArray ) { int i , temp ; int cost = 0 ; HashSet < int > removedElements = new HashSet < int > ( ) ; int ans = 0 ; for ( i = 1 ; i < ( N - 1 ) ; i ++ ) { temp = arr [ i ] ; if ( ( ( arr [ i - 1 ] < temp ) && ( temp < arr [ i + 1 ] ) ) || ( ( arr [ i - 1 ] > temp ) && ( temp > arr [ i + 1 ] ) ) ) { removedElements . Add ( temp ) ; } } for ( i = 0 ; i < ( N ) ; i ++ ) { temp = arr [ i ] ; if ( ! ( removedElements . Contains ( temp ) ) ) { ans += arr [ i ] ; } else { cost += costArray [ i ] ; } } Console . Write ( ans + " , ▁ " ) ; Console . Write ( cost ) ; } public static void Main ( String [ ] args ) { int N ; N = 4 ; int [ ] arr = { 1 , 3 , 4 , 2 } ; int [ ] costArray = { 0 , 1 , 0 , 0 } ; costOfSubsequence ( N , arr , costArray ) ; } }


"Cost of painting n * m grid | C # implementation of the approach ; Function to return the minimum cost ; Driver code"
Solution in C#:

using System ; class GFG { static int getMinCost ( int n , int m ) { int cost = ( n - 1 ) * m + ( m - 1 ) * n ; return cost ; } public static void Main ( ) { int n = 4 , m = 5 ; Console . WriteLine ( getMinCost ( n , m ) ) ; } }


"Cost of rearranging the array such that no element exceeds the sum of its adjacent elements | C # program to implement the above approach ; Function to check if given elements can be arranged such that sum of its neighbors is strictly greater ; Initialize the total cost ; Storing the original index of elements in a hashmap ; Sort the given array ; Check if a given condition is satisfies or not ; First number ; Add the cost to overall cost ; Last number ; Add the cost to overall cost ; Add the cost to overall cost ; Printing the cost ; Driver Code ; Given array ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void Arrange ( int [ ] arr , int n ) { int cost = 0 ; Dictionary < int , int > index = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { index . Add ( arr [ i ] , i ) ; } Array . Sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { if ( arr [ i ] > arr [ i + 1 ] + arr [ n - 1 ] ) { Console . Write ( " - 1" ) ; return ; } else { cost += Math . Abs ( index [ arr [ i ] ] - i ) ; } } else if ( i == n - 1 ) { if ( arr [ i ] > arr [ i - 1 ] + arr [ 0 ] ) { Console . Write ( " - 1" ) ; return ; } else { cost += Math . Abs ( index [ arr [ i ] ] - i ) ; } } else { if ( arr [ i ] > arr [ i - 1 ] + arr [ i + 1 ] ) { Console . Write ( " - 1" ) ; return ; } else { cost += Math . Abs ( index [ arr [ i ] ] - i ) ; } } } Console . Write ( cost ) ; return ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 5 , 1 , 3 } ; int N = arr . Length ; Arrange ( arr , N ) ; } }


"Cost required to empty a given array by repeated removal of maximum obtained by given operations | C # program for the above approach ; Function to find the total cost of removing all array elements ; Sort the array in descending order ; Stores the total cost ; Contribution of i - th greatest element to the cost ; Remove the element ; If negative ; Add to the readonly cost ; Return the cost ; Driver Code ; Given array [ ] arr ; Function Call"
Solution in C#:

using System ; class GFG { static int findCost ( int [ ] a , int n ) { Array . Sort ( a ) ; Array . Reverse ( a ) ; int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { int p = a [ j ] - j ; a [ j ] = 0 ; if ( p < 0 ) { p = 0 ; continue ; } count += p ; } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 6 , 7 , 4 , 2 , 5 , 3 } ; int N = arr . Length ; Console . Write ( findCost ( arr , N ) ) ; } }


"Cost to make a string Panagram | C # program to find the cost to make a string Panagram ; Function to return the total cost required to make the string Pangram ; Mark all the alphabets that occurred in the string ; Calculate the total cost for the missing alphabets ; Driver Code"
Solution in C#:

using System ; class GFG { static int pangramCost ( int [ ] arr , string str ) { int cost = 0 ; bool [ ] occurred = new bool [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) occurred [ i ] = false ; for ( int i = 0 ; i < str . Length ; i ++ ) occurred [ str [ i ] - ' a ' ] = true ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( occurred [ i ] == false ) cost += arr [ i ] ; } return cost ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 } ; string str = " abcdefghijklmopqrstuvwz " ; Console . WriteLine ( pangramCost ( arr , str ) ) ; } }


"Cost to make a string Panagram | Set 2 | C # implementation of the approach ; Function to return the cost to make str a Panagram ; Count the occurrences of each lowercase character ; To store the total gain ; If some character is missing , it has to be added at twice the cost ; If some character appears more than once all of its occurrences except 1 can be traded for some gain ; If gain is more than the cost ; Return the total cost if gain < 0 ; Driver code"
Solution in C#:

using System ; class GFG { static int costToPanagram ( string str , int [ ] cost ) { int i , n = str . Length ; int [ ] occurrences = new int [ 26 ] ; for ( i = 0 ; i < n ; i ++ ) occurrences [ str [ i ] - ' a ' ] ++ ; int gain = 0 ; for ( i = 0 ; i < 26 ; i ++ ) { if ( occurrences [ i ] == 0 ) gain -= ( 2 * cost [ i ] ) ; else if ( occurrences [ i ] > 1 ) gain += ( cost [ i ] * ( occurrences [ i ] - 1 ) ) ; } if ( gain >= 0 ) return 0 ; return ( gain * - 1 ) ; } public static void Main ( ) { int [ ] cost = { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; string str = " geeksforgeeks " ; Console . WriteLine ( costToPanagram ( str , cost ) ) ; } }


"Count ' d ' digit positive integers with 0 as a digit | C # program to find the count of positive integer of a given number of digits that contain atleast one zero . ; Returns count of ' d ' digit integers have 0 as a digit ; Driver Code"
Solution in C#:

using System ; class GFG { static int findCount ( int d ) { return 9 * ( ( int ) ( Math . Pow ( 10 , d - 1 ) ) - ( int ) ( Math . Pow ( 9 , d - 1 ) ) ) ; } public static void Main ( ) { int d = 1 ; Console . WriteLine ( findCount ( d ) ) ; d = 2 ; Console . WriteLine ( findCount ( d ) ) ; d = 4 ; Console . WriteLine ( findCount ( d ) ) ; } }


"Count 1 's in a sorted binary array | C # program to count 1 's in a sorted array ; Returns counts of 1 's in arr[low..high].  The array is assumed to be sorted in  non-increasing order  ; get the middle index ; check if the element at middle index is last 1 ; If element is not last 1 , recur for right side ; else recur for left side ; Driver code"
Solution in C#:

using System ; class GFG { static int countOnes ( int [ ] arr , int low , int high ) { if ( high >= low ) { int mid = low + ( high - low ) / 2 ; if ( ( mid == high arr [ mid + 1 ] == 0 ) && ( arr [ mid ] == 1 ) ) return mid + 1 ; if ( arr [ mid ] == 1 ) return countOnes ( arr , ( mid + 1 ) , high ) ; return countOnes ( arr , low , ( mid - 1 ) ) ; } return 0 ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 1 , 1 , 0 , 0 , 0 } ; int n = arr . Length ; Console . WriteLine ( " Count ▁ of ▁ 1 ' s ▁ in ▁ given ▁ " + " array ▁ is ▁ " + countOnes ( arr , 0 , n - 1 ) ) ; } }


"Count 1 s in binary matrix having remaining indices of its row and column filled with 0 s | C # program for the above approach ; Function to count required 1 s from the given matrix ; Stores the dimensions of the [ , ] mat ; Calculate sum of rows ; Calculate sum of columns ; Stores required count of 1 s ; If current cell is 1 and sum of row and column is 1 ; Increment count of 1 s ; Return the readonly count ; Driver Code ; Given matrix ; Function call"
Solution in C#:

 using System ; class GFG { static int numSpecial ( int [ , ] mat ) { int m = mat . GetLength ( 0 ) ; int n = mat . GetLength ( 1 ) ; int [ ] rows = new int [ m ] ; int [ ] cols = new int [ n ] ; int i , j ; for ( i = 0 ; i < m ; i ++ ) { rows [ i ] = 0 ; for ( j = 0 ; j < n ; j ++ ) rows [ i ] += mat [ i , j ] ; } for ( i = 0 ; i < n ; i ++ ) { cols [ i ] = 0 ; for ( j = 0 ; j < m ; j ++ ) cols [ i ] += mat [ j , i ] ; } int cnt = 0 ; for ( i = 0 ; i < m ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( mat [ i , j ] == 1 && rows [ i ] == 1 && cols [ j ] == 1 ) cnt ++ ; } } return cnt ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 0 , 0 } , { 0 , 0 , 1 } , { 0 , 0 , 0 } } ; Console . Write ( numSpecial ( mat ) + "
 " ) ; } }


"Count All Palindrome Sub | C # Program to count palindrome substring in a string ; Method which return count palindrome substring ; Iterate the loop twice ; Get each substring ; If length is greater than or equal to two Check for palindrome ; Use StringBuffer class to reverse the string ; Compare substring wih reverse of substring ; return the count ; Swap values of l and r ; Driver Code ; Declare and initialize the string ; Call the method"
Solution in C#:

using System ; class GFG { static int countPS ( String str ) { String temp = " " ; String stf ; int count = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { for ( int j = i + 1 ; j <= str . Length ; j ++ ) { temp = str . Substring ( i , j - i ) ; if ( temp . Length >= 2 ) { stf = temp ; stf = reverse ( temp ) ; if ( stf . ToString ( ) . CompareTo ( temp ) == 0 ) count ++ ; } } } return count ; } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = 0 ; r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { String str = " abbaeae " ; Console . WriteLine ( countPS ( str ) ) ; } }


"Count All Palindrome Sub | C # program to find palindromic substrings of a string ; Returns total number of palindrome substring of length greater then equal to 2 ; create empty 2 - D matrix that counts all palindrome substring . dp [ i ] [ j ] stores counts of palindromic substrings in st [ i . . j ] ; P [ i ] [ j ] = true if substring str [ i . . j ] is palindrome , else false ; palindrome of single length ; palindrome of length 2 ; Palindromes of length more then 2. This loop is similar to Matrix Chain Multiplication . We start with a gap of length 2 and fill DP table in a way that gap between starting and ending indexes increases one by one by outer loop . ; Pick starting point for current gap ; Set ending point ; If current string is palindrome ; Add current palindrome substring ( + 1 ) and rest palindrome substring ( dp [ i ] [ j - 1 ] + dp [ i + 1 ] [ j ] ) remove common palindrome substrings ( - dp [ i + 1 ] [ j - 1 ] ) ; return total palindromic substrings ; Driver Code"
Solution in C#:

using System ; class GFG { public static int CountPS ( char [ ] str , int n ) { int [ ] [ ] dp = RectangularArrays . ReturnRectangularIntArray ( n , n ) ; bool [ ] [ ] P = RectangularArrays . ReturnRectangularBoolArray ( n , n ) ; for ( int i = 0 ; i < n ; i ++ ) { P [ i ] [ i ] = true ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( str [ i ] == str [ i + 1 ] ) { P [ i ] [ i + 1 ] = true ; dp [ i ] [ i + 1 ] = 1 ; } } for ( int gap = 2 ; gap < n ; gap ++ ) { for ( int i = 0 ; i < n - gap ; i ++ ) { int j = gap + i ; if ( str [ i ] == str [ j ] && P [ i + 1 ] [ j - 1 ] ) { P [ i ] [ j ] = true ; } if ( P [ i ] [ j ] == true ) { dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [ i + 1 ] [ j ] + 1 - dp [ i + 1 ] [ j - 1 ] ; } else { dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [ i + 1 ] [ j ] - dp [ i + 1 ] [ j - 1 ] ; } } } return dp [ 0 ] [ n - 1 ] ; } public static class RectangularArrays { public static int [ ] [ ] ReturnRectangularIntArray ( int size1 , int size2 ) { int [ ] [ ] newArray = new int [ size1 ] [ ] ; for ( int array1 = 0 ; array1 < size1 ; array1 ++ ) { newArray [ array1 ] = new int [ size2 ] ; } return newArray ; } public static bool [ ] [ ] ReturnRectangularBoolArray ( int size1 , int size2 ) { bool [ ] [ ] newArray = new bool [ size1 ] [ ] ; for ( int array1 = 0 ; array1 < size1 ; array1 ++ ) { newArray [ array1 ] = new bool [ size2 ] ; } return newArray ; } } public static void Main ( string [ ] args ) { string str = " abaab " ; Console . WriteLine ( CountPS ( str . ToCharArray ( ) , str . Length ) ) ; } }


"Count All Palindromic Subsequence in a given String | C # code to Count Palindromic Subsequence Subsequence in a given String ; Function return the total palindromic subsequence ; create a 2D array to store the count of palindromic subsequence ; palindromic subsequence of length 1 ; check subsequence of length L is palindrome or not ; return total palindromic subsequence ; Driver Code"
Solution in C#:

using System ; class GFG { static int countPS ( string str ) { int N = str . Length ; int [ , ] cps = new int [ N + 1 , N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) cps [ i , i ] = 1 ; for ( int L = 2 ; L <= N ; L ++ ) { for ( int i = 0 ; i <= N - L ; i ++ ) { int k = L + i - 1 ; if ( k < N ) { if ( str [ i ] == str [ k ] ) cps [ i , k ] = cps [ i , k - 1 ] + cps [ i + 1 , k ] + 1 ; else cps [ i , k ] = cps [ i , k - 1 ] + cps [ i + 1 , k ] - cps [ i + 1 , k - 1 ] ; } } } return cps [ 0 , N - 1 ] ; } public static void Main ( ) { string str = " abcb " ; Console . Write ( " Total ▁ palindromic ▁ " + " subsequence ▁ are ▁ : ▁ " + countPS ( str ) ) ; } }


"Count All Palindromic Subsequence in a given String | C # program to counts Palindromic Subsequence in a given String using recursion ; Function return the total palindromic subsequence ; Driver code"
Solution in C#:

using System ; class GFG { static int n ; static int [ , ] dp = new int [ 1000 , 1000 ] ; static string str = " abcb " ; static int countPS ( int i , int j ) { if ( i > j ) return 0 ; if ( dp [ i , j ] != - 1 ) return dp [ i , j ] ; if ( i == j ) return dp [ i , j ] = 1 ; else if ( str [ i ] == str [ j ] ) return dp [ i , j ] = countPS ( i + 1 , j ) + countPS ( i , j - 1 ) + 1 ; else return dp [ i , j ] = countPS ( i + 1 , j ) + countPS ( i , j - 1 ) - countPS ( i + 1 , j - 1 ) ; } static void Main ( ) { for ( int i = 0 ; i < 1000 ; i ++ ) for ( int j = 0 ; j < 1000 ; j ++ ) dp [ i , j ] = - 1 ; n = str . Length ; Console . Write ( " Total ▁ palindromic ▁ subsequence " + " are ▁ : ▁ " + countPS ( 0 , n - 1 ) ) ; } }


"Count Arithmetic Progressions having sum N and common difference equal to 1 | C # program for above approach ; Function to count all possible AP series with common difference 1 and sum of elements equal to N ; Stores the count of AP series ; Traverse through all factors of 2 * N ; Check for the given conditions ; Increment count ; Print count - 1 ; Driver code ; Given value of N ; Function call to count required number of AP series"
Solution in C#:

using System ; public class GFG { static void countAPs ( long N ) { long count = 0 ; for ( long i = 1 ; i * i <= 2 * N ; i ++ ) { long res = 2 * N ; if ( res % i == 0 ) { long op = res / i - i + 1 ; if ( op % 2 == 0 ) { count ++ ; } if ( i * i != res && ( i - res / i + 1 ) % 2 == 0 ) { count ++ ; } } } Console . WriteLine ( count - 1 ) ; } public static void Main ( String [ ] args ) { long N = 963761198400L ; countAPs ( N ) ; } }


"Count Arithmetic Progressions having sum S and common difference equal to D | C # program for the above approach ; Function to count the number of APs with sum S and common difference D ; Multiply S by 2 ; Stores the count of APs ; Iterate over the factors of 2 * S ; Check if i is the factor or not ; Conditions to check if AP can be formed using factor F ; Return the total count of APs ; Driver code"
Solution in C#:

using System ; class GFG { static int countAPs ( int S , int D ) { S = S * 2 ; int answer = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( S ) ; i ++ ) { if ( S % i == 0 ) { if ( ( ( S / i ) - D * i + D ) % 2 == 0 ) answer ++ ; if ( ( D * i - ( S / i ) + D ) % 2 == 0 ) answer ++ ; } } return answer ; } static void Main ( ) { int S = 12 , D = 1 ; Console . Write ( countAPs ( S , D ) ) ; } }


"Count BST subtrees that lie in given range | C # program to count subtrees that lie in a given range ; A BST node ; int class ; A utility function to check if data of root is in range from low to high ; A recursive function to get count of nodes whose subtree is in range from low to hgih . This function returns true if nodes in subtree rooted under ' root ' are in range . ; Base case ; Recur for left and right subtrees ; If both left and right subtrees are in range and current node is also in range , then increment count and return true ; A wrapper over getCountUtil ( ) . This function initializes count as 0 and calls getCountUtil ( ) ; Utility function to create new node ; Driver code ; Let us con the BST shown in the above figure ; Let us construct BST shown in above example 10 / \ 5 50 / / \ 1 40 100"
Solution in C#:

using System ; class GfG { public class node { public int data ; public node left , right ; } ; public class INT { public int a ; } static bool inRange ( node root , int low , int high ) { return root . data >= low && root . data <= high ; } static bool getCountUtil ( node root , int low , int high , INT count ) { if ( root == null ) return true ; bool l = getCountUtil ( root . left , low , high , count ) ; bool r = getCountUtil ( root . right , low , high , count ) ; if ( l && r && inRange ( root , low , high ) ) { ++ count . a ; return true ; } return false ; } static INT getCount ( node root , int low , int high ) { INT count = new INT ( ) ; count . a = 0 ; getCountUtil ( root , low , high , count ) ; return count ; } static node newNode ( int data ) { node temp = new node ( ) ; temp . data = data ; temp . left = temp . right = null ; return ( temp ) ; } public static void Main ( String [ ] args ) { node root = newNode ( 10 ) ; root . left = newNode ( 5 ) ; root . right = newNode ( 50 ) ; root . left . left = newNode ( 1 ) ; root . right . left = newNode ( 40 ) ; root . right . right = newNode ( 100 ) ; int l = 5 ; int h = 45 ; Console . WriteLine ( " Count ▁ of ▁ subtrees ▁ in ▁ [ " + l + " , ▁ " + h + " ] ▁ is ▁ " + getCount ( root , l , h ) . a ) ; } }


"Count Balanced Binary Trees of Height h | C # program to count number of balanced binary trees of height h . ; base cases ; Driver program"
Solution in C#:

using System ; class GFG { static int MOD = 1000000007 ; public static long countBT ( int h ) { long [ ] dp = new long [ h + 1 ] ; dp [ 0 ] = 1 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i <= h ; ++ i ) dp [ i ] = ( dp [ i - 1 ] * ( ( 2 * dp [ i - 2 ] ) % MOD + dp [ i - 1 ] ) % MOD ) % MOD ; return dp [ h ] ; } static void Main ( ) { int h = 3 ; Console . WriteLine ( " No . ▁ of ▁ balanced ▁ binary ▁ trees ▁ of ▁ height ▁ " + h + " ▁ is : ▁ " + countBT ( h ) ) ; } }


"Count Derangements ( Permutation such that no element appears in its original position ) | A Dynamic programming based C # program to count derangements ; Function to count derangements ; Create an array to store counts for subproblems ; Base cases ; Fill der [ 0. . n ] in bottom up manner using above recursive formula ; Return result for n ; Driver code"
Solution in C#:

using System ; class GFG { static int countDer ( int n ) { int [ ] der = new int [ n + 1 ] ; der [ 1 ] = 0 ; der [ 2 ] = 1 ; for ( int i = 3 ; i <= n ; ++ i ) der [ i ] = ( i - 1 ) * ( der [ i - 1 ] + der [ i - 2 ] ) ; return der [ n ] ; } public static void Main ( ) { int n = 4 ; Console . Write ( " Count ▁ of ▁ Derangements ▁ is ▁ " + countDer ( n ) ) ; } }


"Count Derangements ( Permutation such that no element appears in its original position ) | A Naive Recursive C # program to count derangements ; Function to count derangements ; Base cases ; countDer ( n ) = ( n - 1 ) [ countDer ( n - 1 ) + der ( n - 2 ) ] ; Driver Code"
Solution in C#:

using System ; class GFG { static int countDer ( int n ) { if ( n == 1 ) return 0 ; if ( n == 2 ) return 1 ; return ( n - 1 ) * ( countDer ( n - 1 ) + countDer ( n - 2 ) ) ; } public static void Main ( ) { int n = 4 ; Console . Write ( " Count ▁ of ▁ Derangements ▁ is ▁ " + countDer ( n ) ) ; } }


"Count Derangements ( Permutation such that no element appears in its original position ) | C # implementation of the above approach ; Function to count derangements ; Base case ; Variable for just storing previous values ; Using above recursive formula ; Return result for n ; Driver code"
Solution in C#:

using System ; class GFG { static int countDer ( int n ) { if ( n == 1 n == 2 ) { return n - 1 ; } int a = 0 ; int b = 1 ; for ( int i = 3 ; i <= n ; ++ i ) { int cur = ( i - 1 ) * ( a + b ) ; a = b ; b = cur ; } return b ; } public static void Main ( ) { Console . Write ( " Count ▁ of ▁ Dearrangements ▁ is ▁ " + countDer ( 4 ) ) ; } }


"Count Distinct Non | An efficient C # program to find different ( x , y ) pairs that satisfy x * x + y * y < n . ; This function counts number of pairs ( x , y ) that satisfy the inequality x * x + y * y < n . ; Find the count of different y values for x = 0. ; One by one increase value of x , and find yCount forcurrent x . If yCount becomes 0 , then we have reached maximum possible value of x . ; Add yCount ( count of different possible values of y for current x ) to result ; Increment x ; Update yCount for current x . Keep reducing yCount while the inequality is not satisfied . ; Driver program"
Solution in C#:

using System ; class GFG { static int countSolutions ( int n ) { int x = 0 , yCount , res = 0 ; for ( yCount = 0 ; yCount * yCount < n ; yCount ++ ) ; while ( yCount != 0 ) { res += yCount ; x ++ ; while ( yCount != 0 && ( x * x + ( yCount - 1 ) * ( yCount - 1 ) >= n ) ) yCount -- ; } return res ; } public static void Main ( ) { Console . WriteLine ( " Total ▁ Number ▁ of ▁ " + " distinct ▁ Non - Negative ▁ pairs ▁ is ▁ " + countSolutions ( 6 ) ) ; } }


"Count Distinct Non | C # code to Count Distinct Non - Negative Integer Pairs ( x , y ) that Satisfy the inequality x * x + y * y < n ; This function counts number of pairs ( x , y ) that satisfy the inequality x * x + y * y < n . ; Driver program"
Solution in C#:

using System ; class GFG { static int countSolutions ( int n ) { int res = 0 ; for ( int x = 0 ; x * x < n ; x ++ ) for ( int y = 0 ; x * x + y * y < n ; y ++ ) res ++ ; return res ; } public static void Main ( ) { Console . WriteLine ( " Total ▁ Number ▁ of ▁ " + " distinct ▁ Non - Negative ▁ pairs ▁ is ▁ " + countSolutions ( 6 ) ) ; } }


"Count Distinct Strings present in an array using Polynomial rolling hash function | C # program to implement the above approach ; Function to find the hash value of a string ; Traverse the string ; Update hash_val ; Update mul ; Return hash_val of str ; Function to find the count of distinct strings present in the given array ; Store the hash values of the strings ; Traverse the array ; Stores hash value of arr [ i ] ; Sort hash [ ] array ; Stores count of distinct strings in the array ; Traverse hash [ ] array ; Update cntElem ; Driver Code"
Solution in C#:

using System ; class GFG { static int compute_hash ( string str ) { int p = 31 ; int MOD = ( int ) 1e9 + 7 ; int hash_val = 0 ; int mul = 1 ; for ( int i = 0 ; i < str . Length ; i ++ ) { char ch = str [ i ] ; hash_val = ( hash_val + ( ch - ' a ' + 1 ) * mul ) % MOD ; mul = ( mul * p ) % MOD ; } return hash_val ; } static int distinct_str ( string [ ] arr , int n ) { int [ ] hash = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { hash [ i ] = compute_hash ( arr [ i ] ) ; } Array . Sort ( hash ) ; int cntElem = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( hash [ i ] != hash [ i - 1 ] ) { cntElem ++ ; } } return cntElem ; } public static void Main ( String [ ] args ) { string [ ] arr = { " abcde " , " abcce " , " abcdf " , " abcde " } ; int N = arr . Length ; Console . WriteLine ( distinct_str ( arr , N ) ) ; } }


"Count Distinct Subsequences | C # Program for above approach ; Returns count of distinct subsequences of str . ; Iterate from 0 to s . length ( ) ; Iterate from 0 to s . length ( ) ; Check if i equal to 0 ; Replace levelCount withe allCount + 1 ; If map is less than 0 ; Return answer ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int countSub ( String s ) { Dictionary < char , int > map = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( ! map . ContainsKey ( s [ i ] ) ) { map . Add ( s [ i ] , - 1 ) ; } } int allCount = 0 ; int levelCount = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { char c = s [ i ] ; if ( i == 0 ) { allCount = 1 ; if ( ! map . ContainsKey ( c ) ) { map . Add ( c , 1 ) ; } else { map = 1 ; } levelCount = 1 ; continue ; } levelCount = allCount + 1 ; if ( map . ContainsKey ( c ) ) { if ( map < 0 ) { allCount = ( allCount + levelCount ) ; } else { allCount = ( allCount + levelCount - map ) ; } } if ( ! map . ContainsKey ( c ) ) { map . Add ( c , levelCount ) ; } else { map = levelCount ; } } return allCount ; } static void Main ( ) { List < string > list = new List < string > ( ) ; list . Add ( " abab " ) ; list . Add ( " gfg " ) ; foreach ( string s in list ) { int cnt = countSub ( s ) ; int withEmptyString = cnt + 1 ; Console . WriteLine ( " With ▁ empty ▁ string ▁ count ▁ for ▁ " + s + " ▁ is ▁ " + withEmptyString ) ; Console . WriteLine ( " Without ▁ empty ▁ string ▁ count ▁ for ▁ " + s + " ▁ is ▁ " + cnt ) ; } } }


"Count Divisors of Factorial | C # program to find count of divisors in n ! ; allPrimes [ ] stores all prime numbers less than or equal to n . ; Fills above vector allPrimes [ ] for a given n ; 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 . ; Loop to update prime [ ] ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store primes in the vector allPrimes ; Function to find all result of factorial number ; Initialize result ; find exponents of all primes which divides n and less than n ; Current divisor ; Find the highest power ( stored in exp ) ' ▁ ▁ of ▁ allPrimes [ i ] ▁ that ▁ divides ▁ n ▁ using ▁ ▁ Legendre ' s formula . ; Multiply exponents of all primes less than n ; return total divisors ; Driver code"
Solution in C#:

using System ; using System . Collections ; class GFG { static ArrayList allPrimes = new ArrayList ( ) ; static void sieve ( int n ) { bool [ ] prime = new bool [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int p = 2 ; p <= n ; p ++ ) if ( prime [ p ] ) allPrimes . Add ( p ) ; } static int factorialDivisors ( int n ) { int result = 1 ; for ( int i = 0 ; i < allPrimes . Count ; i ++ ) { int p = ( int ) allPrimes [ i ] ; int exp = 0 ; while ( p <= n ) { exp = exp + ( n / p ) ; p = p * ( int ) allPrimes [ i ] ; } result = result * ( exp + 1 ) ; } return result ; } public static void Main ( ) { Console . WriteLine ( factorialDivisors ( 6 ) ) ; } }


"Count Divisors of n in O ( n ^ 1 / 3 ) | C # implementation of Naive method to count all divisors ; function to count the divisors ; If divisors are equal , count only one ; Otherwise count both ; Driver program"
Solution in C#:

using System ; class GFG { static int countDivisors ( int n ) { int cnt = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) cnt ++ ; else cnt = cnt + 2 ; } } return cnt ; } public static void Main ( ) { Console . WriteLine ( " Total ▁ distinct " + " ▁ divisors ▁ of ▁ 100 ▁ are ▁ : ▁ " + countDivisors ( 100 ) ) ; } }


"Count Full Prime numbers in a given range | C # program to implement the above approach ; Function to check if a number is prime or not ; If a divisor of n exists ; Function to check if a number is Full Prime or not ; If n is not a prime ; Otherwise ; Extract digit ; If any digit of n is non - prime ; Function to print count of Full Primes in a range [ L , R ] ; Stores count of full primes ; Check if i is full prime ; Driver Code ; Stores count of full primes"
Solution in C#:

using System ; class GFG { static bool isPrime ( int num ) { if ( num <= 1 ) return false ; for ( int i = 2 ; i * i <= num ; i ++ ) if ( num % i == 0 ) return false ; return true ; } static bool isFulPrime ( int n ) { if ( ! isPrime ( n ) ) return false ; else { while ( n > 0 ) { int rem = n % 10 ; if ( ! ( rem == 2 rem == 3 rem == 5 rem == 7 ) ) return false ; n = n / 10 ; } } return true ; } static int countFulPrime ( int L , int R ) { int cnt = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( ( i % 2 ) != 0 && isFulPrime ( i ) ) { cnt ++ ; } } return cnt ; } public static void Main ( String [ ] args ) { int L = 1 , R = 100 ; int ans = 0 ; if ( L < 3 ) ans ++ ; Console . WriteLine ( ans + countFulPrime ( L , R ) ) ; } }


"Count Inversions in an array | Set 1 ( Using Merge Sort ) | C # program to count inversions in an array ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int [ ] arr = new int [ ] { 1 , 20 , 6 , 4 , 5 } ; static int getInvCount ( int n ) { int inv_count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] > arr [ j ] ) inv_count ++ ; return inv_count ; } public static void Main ( ) { Console . WriteLine ( " Number ▁ of ▁ " + " inversions ▁ are ▁ " + getInvCount ( arr . Length ) ) ; } }


"Count Inversions of size three in a given array | A O ( n ^ 2 ) Java program to count inversions of size 3 ; returns count of inversion of size 3 ; initialize result ; count all smaller elements on right of arr [ i ] ; count all greater elements on left of arr [ i ] ; update inversion count by adding all inversions that have arr [ i ] as middle of three elements ; driver program to test above function"
Solution in C#:

using System ; public class Inversion { static int getInvCount ( int [ ] arr , int n ) { int invcount = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int small = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] > arr [ j ] ) small ++ ; int great = 0 ; for ( int j = i - 1 ; j >= 0 ; j -- ) if ( arr [ i ] < arr [ j ] ) great ++ ; invcount += great * small ; } return invcount ; } public static void Main ( ) { int [ ] arr = new int [ ] { 8 , 4 , 2 , 1 } ; int n = arr . Length ; Console . WriteLine ( " Inversion ▁ count ▁ : ▁ " + getInvCount ( arr , n ) ) ; } }


"Count Knights that can attack a given pawn in an N * N board | C # program to implement the above approach ; Function to count the knights that are attacking the pawn in an M * M board ; Stores count of knights that are attacking the pawn ; Traverse the knights [ ] [ ] array ; Stores absolute difference of X co - ordinate of i - th knight and pawn ; Stores absolute difference of Y co - ordinate of i - th knight and pawn ; If X is 1 and Y is 2 or X is 2 and Y is 1 ; Update cntKnights ; Driver code ; Stores total count of knights"
Solution in C#:

using System ; class GFG { static int cntKnightsAttackPawn ( int [ , ] knights , int [ ] pawn , int M ) { int cntKnights = 0 ; for ( int i = 0 ; i < M ; i ++ ) { int X = Math . Abs ( knights [ i , 0 ] - pawn [ 0 ] ) ; int Y = Math . Abs ( knights [ i , 1 ] - pawn [ 1 ] ) ; if ( ( X == 1 && Y == 2 ) || ( X == 2 && Y == 1 ) ) { cntKnights ++ ; } } return cntKnights ; } static void Main ( ) { int [ , ] knights = { { 0 , 4 } , { 4 , 5 } , { 1 , 4 } , { 3 , 1 } } ; int [ ] pawn = { 2 , 3 } ; int M = knights . GetLength ( 0 ) ; Console . WriteLine ( cntKnightsAttackPawn ( knights , pawn , M ) ) ; } }


"Count L | C # program for the above approach ; Fumction to find the number of arrays of length L such that each element divides the next element ; Stores the number of sequences of length i that ends with j ; Initialize 2D array dp [ ] [ ] as 0 ; Base Case ; Iterate over the range [ 0 , l ] ; Iterate for all multiples of j ; Incrementing dp [ i + 1 ] [ k ] by dp [ i ] [ j ] as the next element is multiple of j ; Stores the number of arrays ; Add all array of length L that ends with i ; Return the resultant count ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int numberOfArrays ( int n , int l ) { int [ , ] dp = new int [ l + 1 , n + 1 ] ; for ( int i = 0 ; i < l + 1 ; i ++ ) { for ( int j = 0 ; j < n + 1 ; j ++ ) dp [ i , j ] = 0 ; } dp [ 0 , 1 ] = 1 ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { for ( int k = j ; k <= n ; k += j ) { dp [ i + 1 , k ] += dp [ i , j ] ; } } } int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { ans += dp [ l , i ] ; } return ans ; } public static void Main ( ) { int N = 2 , L = 4 ; Console . Write ( numberOfArrays ( N , L ) ) ; } }


"Count N digits numbers with sum divisible by K | C # program to implement the above approach ; Function to count the N digit numbers whose sum is divisible by K ; Base case ; If already computed subproblem occurred ; Store the count of N digit numbers whoe sum is divisible by K ; Check if the number does not contain any leading 0. ; Recurrence relation ; Driver code ; Stores the values of overlapping subproblems"
Solution in C#:

using System ; class GFG { static int M = 1000 ; static int countNum ( int N , int sum , int K , int st , int [ , , ] dp ) { if ( N == 0 && sum == 0 ) { return 1 ; } if ( N < 0 ) { return 0 ; } if ( dp [ N , sum , st ] != - 1 ) { return dp [ N , sum , st ] ; } int res = 0 ; int start = ( st == 1 ? 0 : 1 ) ; for ( int i = start ; i <= 9 ; i ++ ) { res += countNum ( N - 1 , ( sum + i ) % K , K , ( ( st i ) > 0 ) ? 1 : 0 , dp ) ; } return dp [ N , sum , st ] = res ; } static public void Main ( ) { int N = 2 , K = 7 ; int [ , , ] dp = new int [ M , M , 2 ] ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) for ( int k = 0 ; k < 2 ; k ++ ) dp [ i , j , k ] = - 1 ; Console . WriteLine ( countNum ( N , 0 , K , 0 , dp ) ) ; } }


"Count N |  ; Stores the dp states ; Check if a number is a prime or not ; Function to generate all prime numbers that are less than or equal to n ; Base cases . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of as non - prime ; Function to find the count of N - digit numbers such that the sum of digits is a prime number ; If end of array is reached ; If the sum is equal to a prime number ; Otherwise ; If the dp - states are already computed ; If index = 1 , any digit from [ 1 - 9 ] can be placed . If N = 1 , 0 also can be placed . ; Otherwise , any digit from [ 0 - 9 ] can be placed . ; Return the answer . ; Driver Code ; Initializing dp array with - 1 ; Initializing prime array to true ; Find all primes less than or equal to 1000 , which is sufficient for N upto 100 ; Given Input ; Function call"
Solution in C#:

using System ; public class GFG { public static int [ , ] dp = new int [ 100 , 1000 ] ; public static bool [ ] prime = new bool [ 1005 ] ; public static void SieveOfEratosthenes ( int n ) { prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } } public static int countOfNumbers ( int index , int sum , int N ) { if ( index == N + 1 ) { if ( prime [ sum ] == true ) { return 1 ; } return 0 ; } int val = dp [ index , sum ] ; if ( val != - 1 ) { return val ; } val = 0 ; if ( index == 1 ) { for ( int digit = ( N == 1 ? 0 : 1 ) ; digit <= 9 ; ++ digit ) { val += countOfNumbers ( index + 1 , sum + digit , N ) ; } } else { for ( int digit = 0 ; digit <= 9 ; ++ digit ) { val += countOfNumbers ( index + 1 , sum + digit , N ) ; } } return val ; } public static void Main ( String [ ] args ) { for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < 1000 ; j ++ ) { dp [ i , j ] = - 1 ; } } for ( int i = 0 ; i < prime . Length ; i ++ ) prime [ i ] = true ; SieveOfEratosthenes ( 1000 ) ; int N = 6 ; Console . WriteLine ( countOfNumbers ( 1 , 0 , N ) ) ; } }


"Count N | C # program for the above approach ; Function for calculate ( x ^ y ) % mod in O ( log y ) ; Base Condition ; Transition state of power Function ; Function for counting total numbers that can be formed such that digits X , Y are present in each number ; Calculate the given expression ; Return the readonly answer ; Driver Code"
Solution in C#:

 using System ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static long power ( int x , int y ) { if ( y == 0 ) return 1 ; int p = ( int ) ( power ( x , y / 2 ) % mod ) ; p = ( p * p ) % mod ; if ( y % 2 == 1 ) { p = ( x * p ) % mod ; } return p ; } static int TotalNumber ( int N ) { int ans = ( int ) ( ( power ( 10 , N ) - 2 * power ( 9 , N ) + power ( 8 , N ) + 2 * mod ) % mod ) ; return ans ; } public static void Main ( String [ ] args ) { int N = 10 ; Console . Write ( TotalNumber ( N ) + "
 " ) ; } }


"Count N | C # program for the above approach ; Function to calculate the power of n ^ k % p ; Update x if it is more than or equal to p ; In case x is divisible by p ; ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Function to count the number of arrays satisfying required conditions ; Calculating N ^ K ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static int countArrays ( int n , int k ) { int mod = 1000000007 ; int ans = power ( n , k , mod ) ; return ans ; } public static void Main ( ) { int n = 3 , k = 5 ; int ans = countArrays ( n , k ) ; Console . Write ( ans ) ; } }


"Count N | C # program for the above approach ; Function to count N - digit numbers such that each position is divisible by the digit occurring at that position ; Stores the answer . ; Iterate from indices 1 to N ; Stores count of digits that can be placed at the current index ; Iterate from digit 1 to 9 ; If index is divisible by digit ; Multiply answer with possible choices ; Driver Code ; Given Input ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int mod = 1000000007 ; static void countOfNumbers ( int N ) { int ans = 1 ; for ( int index = 1 ; index <= N ; ++ index ) { int choices = 0 ; for ( int digit = 1 ; digit <= 9 ; ++ digit ) { if ( index % digit == 0 ) { ++ choices ; } } ans = ( ans * choices ) % mod ; } Console . Write ( ans ) ; } public static void Main ( ) { int N = 5 ; countOfNumbers ( N ) ; } }


"Count N | C # program for the above approach ; Function to count N digit numbers whose digits are less than or equal to the absolute difference of previous two digits ; If all digits are traversed ; If the state has already been computed ; If the current digit is 1 , any digit from [ 1 - 9 ] can be placed . If N == 1 , 0 can also be placed . ; If the current digit is 2 , any digit from [ 0 - 9 ] can be placed ; For other digits , any digit from 0 to abs ( prev1 - prev2 ) can be placed ; Return the answer ; Driver code ; Input ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int [ , , ] dp = new int [ 50 , 10 , 10 ] ; static void initialize ( ) { for ( int i = 0 ; i < 50 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { for ( int k = 0 ; k < 10 ; k ++ ) { dp [ i , j , k ] = - 1 ; } } } } static int countOfNumbers ( int digit , int prev1 , int prev2 , int N ) { if ( digit == N + 1 ) return 1 ; if ( dp [ digit , prev1 , prev2 ] != - 1 ) return dp [ digit , prev1 , prev2 ] ; dp [ digit , prev1 , prev2 ] = 0 ; if ( digit == 1 ) { for ( int j = ( N == 1 ? 0 : 1 ) ; j <= 9 ; ++ j ) { dp [ digit , prev1 , prev2 ] += countOfNumbers ( digit + 1 , j , prev1 , N ) ; } } else if ( digit == 2 ) { for ( int j = 0 ; j <= 9 ; ++ j ) { dp [ digit , prev1 , prev2 ] += countOfNumbers ( digit + 1 , j , prev1 , N ) ; } } else { for ( int j = 0 ; j <= Math . Abs ( prev1 - prev2 ) ; ++ j ) { dp [ digit , prev1 , prev2 ] += countOfNumbers ( digit + 1 , j , prev1 , N ) ; } } return dp [ digit , prev1 , prev2 ] ; } public static void Main ( ) { initialize ( ) ; int N = 3 ; Console . Write ( countOfNumbers ( 1 , 0 , 0 , N ) ) ; } }


"Count N | C # program for the above approach ; Function to count binary strings of length N having substring "11" ; Initialize dp [ ] of size N + 1 ; Base Cases ; Stores the first N powers of 2 ; Generate ; Iterate over the range [ 2 , N ] ; Print total count of substrings ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void binaryStrings ( int N ) { int [ ] dp = new int [ N + 1 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 0 ; int [ ] power = new int [ N + 1 ] ; power [ 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { power [ i ] = 2 * power [ i - 1 ] ; } for ( int i = 2 ; i <= N ; i ++ ) { dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + power [ i - 2 ] ; } Console . WriteLine ( dp [ N ] ) ; } public static void Main ( ) { int N = 12 ; binaryStrings ( N ) ; } }


"Count N | C # program for the above approach ; Function to find the value of x ^ y ; Stores the value of x ^ y ; Iterate until y is positive ; If y is odd ; Divide y by 2 ; Return the value of x ^ y ; Function to find the number of N - digit integers satisfying the given criteria ; Count of even positions ; Count of odd positions ; Return the resultant count ; Driver Code"
Solution in C#:

using System ; class GFG { static int m = 1000000007 ; static int power ( int x , int y ) { int res = 1 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % m ; y = y >> 1 ; x = ( x * x ) % m ; } return res ; } static int countNDigitNumber ( int N ) { int ne = N / 2 + N % 2 ; int no = ( int ) Math . Floor ( ( double ) N / 2 ) ; return power ( 4 , ne ) * power ( 5 , no ) ; } public static void Main ( ) { int N = 5 ; Console . Write ( countNDigitNumber ( N ) % m ) ; } }


"Count N | C # program for the above approach ; Stores the value of overlapping states ; Function to find the count of numbers that are formed by digits X and Y and whose sum of digit also have digit X or Y ; Initialize dp array ; Base Case ; Check if sum of digits formed by only X or Y ; Return the already computed ; Place the digit X at the current position ; Place the digit Y at the current position ; Update current state result ; Function to check whether a number have only digits X or Y or not ; Until sum is positive ; If any digit is not X or Y then return 0 ; Return 1 ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int [ , ] dp = new int [ 100 + 5 , 900 + 5 ] ; static int mod = 10000007 ; public static int countNumbers ( int n , int x , int y , int sum ) { for ( int i = 0 ; i < dp . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < dp . GetLength ( 1 ) ; j ++ ) { dp [ i , j ] = - 1 ; } } if ( n == 0 ) { return check ( sum , x , y ) ; } if ( dp [ n , sum ] != - 1 ) { return dp [ n , sum ] % mod ; } int option1 = countNumbers ( n - 1 , x , y , sum + x ) % mod ; int option2 = countNumbers ( n - 1 , x , y , sum + y ) % mod ; return dp [ n , sum ] = ( option1 + option2 ) % mod ; } public static int check ( int sum , int x , int y ) { while ( sum > 0 ) { int ln = sum % 10 ; if ( ln != x && ln != y ) { return 0 ; } sum /= 10 ; } return 1 ; } public static void Main ( String [ ] args ) { int N = 3 , X = 1 , Y = 5 ; Console . WriteLine ( countNumbers ( N , X , Y , 0 ) % mod ) ; } }


"Count N | C # program to illustrate Count of N - length strings consisting only of vowels sorted lexicographically ; to keep the string in lexicographically sorted order use start index to add the vowels starting the from that index ; base case : if string length is 0 add to the count ; if last character in string is ' e ' add vowels starting from ' e ' i . e ' e ' , ' i ' , ' o ' , ' u ' ; decrease the length of string ; char arr [ 5 ] = { ' a ' , ' e ' , ' i ' , ' o ' , ' u ' } ; starting from index 0 add the vowels to strings"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countstrings ( int n , int start ) { if ( n == 0 ) { return 1 ; } int cnt = 0 ; for ( int i = start ; i < 5 ; i ++ ) { cnt += countstrings ( n - 1 , i ) ; } return cnt ; } static int countVowelStrings ( int n ) { return countstrings ( n , 0 ) ; } static void Main ( ) { int n = 2 ; Console . Write ( countVowelStrings ( n ) ) ; } }


"Count N | C # program to implement the above approach ; Function to count N - length strings consisting of vowels only sorted lexicographically ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int findNumberOfStrings ( int n ) { return ( n + 1 ) * ( n + 2 ) * ( n + 3 ) * ( n + 4 ) / 24 ; } public static void Main ( string [ ] args ) { int N = 2 ; Console . Write ( findNumberOfStrings ( N ) ) ; } }


"Count N | C # program to implement the above approach ; Function to count N - length strings consisting of vowels only sorted lexicographically ; Stores count of strings consisting of vowels sorted lexicographically of all possible lengths ; Initialize DP [ 1 ] [ 1 ] ; Traverse the matrix row - wise ; Base Case ; Return the result ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int findNumberOfStrings ( int n ) { int [ , ] DP = new int [ n + 1 , 6 ] ; DP [ 1 , 1 ] = 1 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { for ( int j = 1 ; j < 6 ; j ++ ) { if ( i == 1 ) { DP [ i , j ] = DP [ i , j - 1 ] + 1 ; } else { DP [ i , j ] = DP [ i , j - 1 ] + DP [ i - 1 , j ] ; } } } return DP [ n , 5 ] ; } public static void Main ( string [ ] args ) { int N = 2 ; Console . Write ( findNumberOfStrings ( N ) ) ; } }


"Count N | C # program to implement the above approach ; Function to find the number of arrays following the given condition ; Initialize answer ; Calculate nPm ; Print ans ; Driver Code ; Given N and M ; Function Call"
Solution in C#:

using System ; class GFG { static void noOfArraysPossible ( int N , int M ) { int ans = 1 ; for ( int i = 0 ; i < N ; ++ i ) { ans = ans * ( M - i ) ; } Console . Write ( ans ) ; } public static void Main ( ) { int N = 2 , M = 3 ; noOfArraysPossible ( N , M ) ; } }


"Count Non | C # program of the above approach ; Function to find the gcd of the two numbers ; Function to find distinct elements in the array by repeatidely inserting the absolute difference of all possible pairs ; Stores largest element of the array ; Traverse the array , arr [ ] ; Update max_value ; Stores GCD of array ; Traverse the array , arr [ ] ; Update GCDArr ; Stores distinct elements in the array by repeatidely inserting absolute difference of all possible pairs ; Driver code ; Given array arr [ ]"
Solution in C#:

using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int DistinctValues ( int [ ] arr , int N ) { int max_value = Int32 . MinValue ; for ( int i = 0 ; i < N ; ++ i ) { max_value = Math . Max ( max_value , arr [ i ] ) ; } int GCDArr = arr [ 0 ] ; for ( int i = 1 ; i < N ; ++ i ) { GCDArr = gcd ( GCDArr , arr [ i ] ) ; } int answer = ( max_value / GCDArr ) + 1 ; return answer ; } static void Main ( ) { int [ ] arr = { 4 , 12 , 16 , 24 } ; int N = arr . Length ; Console . WriteLine ( DistinctValues ( arr , N ) ) ; } }


"Count Non | C # program to count total number of non - leaf nodes in a binary tree ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Computes the number of non - leaf nodes in a tree . ; Base cases . ; If root is Not NULL and its one of its child is also not NULL ; Driver code"
Solution in C#:

using System ; class GfG { class Node { public int data ; public Node left ; public Node right ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static int countNonleaf ( Node root ) { if ( root == null || ( root . left == null && root . right == null ) ) return 0 ; return 1 + countNonleaf ( root . left ) + countNonleaf ( root . right ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; Console . WriteLine ( countNonleaf ( root ) ) ; } }


"Count Number of animals in a zoo from given number of head and legs | C # implementation of above approach ; Function that calculates Rabbits ; Driver code"
Solution in C#:

using System ; class GFG { static int countRabbits ( int Heads , int Legs ) { int count = 0 ; count = ( Legs ) - 2 * ( Heads ) ; count = count / 2 ; return count ; } public static void Main ( ) { int Heads = 100 , Legs = 300 ; int Rabbits = countRabbits ( Heads , Legs ) ; Console . WriteLine ( " Rabbits ▁ = ▁ " + Rabbits ) ; Console . WriteLine ( " Pigeons ▁ = ▁ " + ( Heads - Rabbits ) ) ; } }


"Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime | C # implementation of the above approach ; Prime numbers upto 100 ; Function to return the count of required numbers from 0 to num ; Base Case ; check if the difference is equal to any prime number ; If this result is already computed simply return it ; Maximum limit upto which we can place digit . If tight is 1 , means number has already become smaller so we can place any digit , otherwise num [ pos ] ; If the current position is odd add it to currOdd , otherwise to currEven ; Function to convert x into its digit vector and uses count ( ) function to return the required count ; Initialize dp ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int M = 18 ; static int a , b ; static int [ , , , ] dp = new int [ M , 90 , 90 , 2 ] ; static int [ ] prime = { 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 43 , 47 , 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 , 89 , 97 } ; static int count ( int pos , int even , int odd , int tight , List < int > num ) { if ( pos == num . Count ) { if ( ( num . Count & 1 ) != 0 ) { int t = odd ; odd = even ; even = t ; } int d = even - odd ; for ( int i = 0 ; i < 24 ; i ++ ) if ( d == prime [ i ] ) return 1 ; return 0 ; } if ( dp [ pos , even , odd , tight ] != - 1 ) return dp [ pos , even , odd , tight ] ; int ans = 0 ; int limit = ( tight != 0 ? 9 : num [ pos ] ) ; for ( int d = 0 ; d <= limit ; d ++ ) { int currF = tight , currEven = even ; int currOdd = odd ; if ( d < num [ pos ] ) currF = 1 ; if ( ( pos & 1 ) != 0 ) currOdd += d ; else currEven += d ; ans += count ( pos + 1 , currEven , currOdd , currF , num ) ; } return dp [ pos , even , odd , tight ] = ans ; } static int solve ( int x ) { List < int > num = new List < int > ( ) ; while ( x != 0 ) { num . Add ( x % 10 ) ; x /= 10 ; } num . Reverse ( ) ; for ( int i = 0 ; i < dp . GetLength ( 0 ) ; i ++ ) for ( int j = 0 ; j < dp . GetLength ( 1 ) ; j ++ ) for ( int k = 0 ; k < dp . GetLength ( 2 ) ; k ++ ) for ( int k1 = 0 ; k1 < dp . GetLength ( 3 ) ; k1 ++ ) dp [ i , j , k , k1 ] = - 1 ; return count ( 0 , 0 , 0 , 0 , num ) ; } public static void Main ( String [ ] args ) { int L = 1 , R = 50 ; Console . WriteLine ( solve ( R ) - solve ( L - 1 ) ) ; L = 50 ; R = 100 ; Console . WriteLine ( solve ( R ) - solve ( L - 1 ) ) ; } }


"Count Numbers with N digits which consists of even number of 0 ’ s | C # program to count numbers with N digits which consists of odd number of 0 's ; Function to count Numbers with N digits which consists of odd number of 0 's ; Driver code"
Solution in C#:

using System ; class GFG { static double countNumbers ( int N ) { return ( Math . Pow ( 10 , N ) - 1 ) - ( Math . Pow ( 10 , N ) - Math . Pow ( 8 , N ) ) / 2 ; } static public void Main ( ) { int n = 2 ; Console . WriteLine ( countNumbers ( n ) ) ; } }


"Count Numbers with N digits which consists of odd number of 0 's | C # program to count numbers with N digits which consists of odd number of 0 's ; Function to count Numbers with N digits which consists of odd number of 0 's ; Driver code"
Solution in C#:

using System ; class GFG { static int countNumbers ( int N ) { return ( int ) ( Math . Pow ( 10 , N ) - Math . Pow ( 8 , N ) ) / 2 ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( countNumbers ( n ) ) ; } }


"Count Odd and Even numbers in a range from L to R | C # implementation of the above approach ; Return the number of odd numbers in the range [ L , R ] ; if either R or L is odd ; Driver code"
Solution in C#:

using System ; class GFG { static int countOdd ( int L , int R ) { int N = ( R - L ) / 2 ; if ( R % 2 != 0 L % 2 != 0 ) N ++ ; return N ; } public static void Main ( ) { int L = 3 , R = 7 ; int odds = countOdd ( L , R ) ; int evens = ( R - L + 1 ) - odds ; Console . WriteLine ( " Count ▁ of ▁ odd ▁ numbers ▁ is ▁ " + odds ) ; Console . WriteLine ( " Count ▁ of ▁ even ▁ numbers ▁ is ▁ " + evens ) ; } }


"Count Pairs from two arrays with even sum | C # implementation of the approach ; Function to return count of required pairs ; Count of odd and even numbers from both the arrays ; Find the count of odd and even elements in a [ ] ; Find the count of odd and even elements in b [ ] ; Count the number of pairs ; Return the number of pairs ; Driver code"
Solution in C#:

using System ; class GFG { static int count_pairs ( int [ ] a , int [ ] b , int n , int m ) { int odd1 = 0 , even1 = 0 ; int odd2 = 0 , even2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) odd1 ++ ; else even1 ++ ; } for ( int i = 0 ; i < m ; i ++ ) { if ( b [ i ] % 2 == 1 ) odd2 ++ ; else even2 ++ ; } int pairs = Math . Min ( odd1 , odd2 ) + Math . Min ( even1 , even2 ) ; return pairs ; } public static void Main ( ) { int [ ] a = { 9 , 14 , 6 , 2 , 11 } ; int [ ] b = { 8 , 4 , 7 , 20 } ; int n = a . Length ; int m = b . Length ; Console . WriteLine ( count_pairs ( a , b , n , m ) ) ; } }


"Count Possible Decodings of a given Digit Sequence in O ( N ) time and Constant Auxiliary space | C # implementation to count decodings ; A Dynamic programming based function to count decodings in digit sequence ; For base condition "01123" should return 0 ; Using last two calculated values , calculate for ith index ; Change bool to int ; Return the required answer ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static int countDecodingDP ( String digits , int n ) { if ( digits [ 0 ] == '0' ) { return 0 ; } int count0 = 1 , count1 = 1 , count2 ; for ( int i = 2 ; i <= n ; i ++ ) { int dig1 = 0 , dig2 , dig3 = 0 ; if ( digits [ i - 1 ] != '0' ) { dig1 = 1 ; } if ( digits [ i - 2 ] == '1' ) { dig2 = 1 ; } else dig2 = 0 ; if ( digits [ i - 2 ] == '2' && digits [ i - 1 ] < '7' ) { dig3 = 1 ; } count2 = dig1 * count1 + dig2 + dig3 * count0 ; count0 = count1 ; count1 = count2 ; } return count1 ; } public static void Main ( String [ ] args ) { String digits = "1234" ; int n = digits . Length ; Console . Write ( countDecodingDP ( digits , n ) ) ; } }


"Count Primes in Ranges | C # program to answer queries for count of primes in given range . ; prefix [ i ] is going to store count of primes till i ( including i ) . ; Create a boolean array " prime [ 0 . . n ] " . 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 ; Build prefix array ; Returns count of primes in range from L to R ( both inclusive ) . ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 10000 ; static int [ ] prefix = new int [ MAX + 1 ] ; static void buildPrefix ( ) { bool [ ] prime = new bool [ MAX + 1 ] ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == false ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = true ; } } prefix [ 0 ] = prefix [ 1 ] = 0 ; for ( int p = 2 ; p <= MAX ; p ++ ) { prefix [ p ] = prefix [ p - 1 ] ; if ( prime [ p ] == false ) prefix [ p ] ++ ; } } static int query ( int L , int R ) { return prefix [ R ] - prefix [ L - 1 ] ; } public static void Main ( ) { buildPrefix ( ) ; int L = 5 , R = 10 ; Console . WriteLine ( query ( L , R ) ) ; L = 1 ; R = 10 ; Console . WriteLine ( query ( L , R ) ) ; } }


"Count Pronic numbers from a given range | C # program for the above approach ; Function to check if x is a Pronic Number or not ; Check for Pronic Number by multiplying consecutive numbers ; Function to count pronic numbers in the range [ A , B ] ; Initialise count ; Iterate from A to B ; If i is pronic ; Increment count ; Print count ; Driver Code ; Function call to count pronic numbers in the range [ A , B ]"
Solution in C#:

using System ; public class GFG { static bool checkPronic ( int x ) { for ( int i = 0 ; i <= ( int ) ( Math . Sqrt ( x ) ) ; i ++ ) { if ( ( x == i * ( i + 1 ) ) != false ) { return true ; } } return false ; } static void countPronic ( int A , int B ) { int count = 0 ; for ( int i = A ; i <= B ; i ++ ) { if ( checkPronic ( i ) != false ) { count ++ ; } } Console . Write ( count ) ; } public static void Main ( String [ ] args ) { int A = 3 , B = 20 ; countPronic ( A , B ) ; } }


"Count Pronic numbers from a given range | C # program for the above approach ; Function to count pronic numbers in the range [ A , B ] ; Check upto sqrt N ; If product of consecutive numbers are less than equal to num ; Return N - 1 ; Function to count pronic numbers in the range [ A , B ] ; Subtract the count of pronic numbers which are <= ( A - 1 ) from the count f pronic numbers which are <= B ; Driver Code ; Function call to count pronic numbers in the range [ A , B ]"
Solution in C#:

using System ; public class GFG { static int pronic ( int num ) { int N = ( int ) Math . Sqrt ( num ) ; if ( N * ( N + 1 ) <= num ) { return N ; } return N - 1 ; } static int countPronic ( int A , int B ) { return pronic ( B ) - pronic ( A - 1 ) ; } public static void Main ( String [ ] args ) { int A = 3 ; int B = 20 ; Console . Write ( countPronic ( A , B ) ) ; } }


"Count Set | C # program to find number of set bist in a number ; Recursive function to find number of set bist in a number ; Base condition ; If Least significant bit is set ; If Least significant bit is not set ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int CountSetBits ( int n ) { if ( n == 0 ) return 0 ; if ( ( n & 1 ) == 1 ) return 1 + CountSetBits ( n >> 1 ) ; else return CountSetBits ( n >> 1 ) ; } public static void Main ( ) { int n = 21 ; Console . WriteLine ( CountSetBits ( n ) ) ; } }


"Count Sexy Prime Pairs in the given array | C # program to count Sexy Prime pairs in array ; A utility function to check if the number n is prime or not ; Base Cases ; Check to skip middle five numbers in below loop ; If n is divisible by i and i + 2 then it is not prime ; A utility function that check if n1 and n2 are SPP ( Sexy Prime Pair ) or not ; Function to find SPP ( Sexy Prime Pair ) pairs from the given array ; Iterate through all pairs ; Increment count if SPP ( Sexy Prime Pair ) pair ; Driver code ; Function call to find SPP ( Sexy Prime Pair ) pair"
Solution in C#:

using System ; class GFG { static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i += 6 ) { if ( n % i == 0 || n % ( i + 6 ) == 0 ) { return false ; } } return true ; } static bool SexyPrime ( int n1 , int n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && Math . Abs ( n1 - n2 ) == 6 ) ; } static int countSexyPairs ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( SexyPrime ( arr [ i ] , arr [ j ] ) ) { count ++ ; } } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 7 , 5 , 11 , 13 } ; int n = arr . Length ; Console . Write ( countSexyPairs ( arr , n ) ) ; } }


"Count Sexy Prime Pairs in the given array | C # program to count Sexy Prime pairs in array ; To store check the prime number ; A utility function that find the Prime Numbers till N ; Resize the Prime Number ; Loop till Math . Sqrt ( N ) to find prime numbers and make their multiple false in the bool array Prime ; Function that returns the count of SPP ( Sexy Prime Pair ) Pairs ; Find the maximum element in the given array [ ] arr ; Function to calculate the prime numbers till N ; To store the count of pairs ; To store the frequency of element in the array [ ] arr ; Sort before traversing the array ; Traverse the array and find the pairs with SPP ( Sexy Prime Pair ) s ; If current element is Prime , then check for ( current element + 6 ) ; Return the count of pairs ; Driver code ; Function call to find SPP ( Sexy Prime Pair ) s pair"
Solution in C#:

using System ; using System . Linq ; class GFG { static bool [ ] Prime ; static void computePrime ( int N ) { Prime = new bool [ N + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) { Prime [ i ] = true ; } Prime [ 0 ] = Prime [ 1 ] = false ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( Prime [ i ] ) { for ( int j = i * i ; j < N ; j += i ) { Prime [ j ] = false ; } } } } static int countSexyPairs ( int [ ] arr , int n ) { int maxE = arr . Max ( ) ; computePrime ( maxE ) ; int count = 0 ; int [ ] freq = new int [ maxE + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } Array . Sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( Prime [ arr [ i ] ] ) { if ( arr [ i ] + 6 < freq . Length && freq [ arr [ i ] + 6 ] > 0 && Prime [ arr [ i ] + 6 ] ) { count ++ ; } } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 7 , 5 , 11 , 13 } ; int n = arr . Length ; Console . Write ( countSexyPairs ( arr , n ) ) ; } }


"Count Strictly Increasing Subarrays | C # program to count number of strictly increasing subarrays ; Initialize count of subarrays as 0 ; Pick starting point ; Pick ending point ; If subarray arr [ i . . j ] is not strictly increasing , then subarrays after it , i . e . , arr [ i . . j + 1 ] , arr [ i . . j + 2 ] , ... . cannot be strictly increasing ; Driver Code"
Solution in C#:

using System ; class Test { static int [ ] arr = new int [ ] { 1 , 2 , 2 , 4 } ; static int countIncreasing ( int n ) { int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] > arr [ j - 1 ] ) cnt ++ ; else break ; } } return cnt ; } public static void Main ( String [ ] args ) { Console . Write ( " Count ▁ of ▁ strictly ▁ increasing " + " subarrays ▁ is ▁ " + countIncreasing ( arr . Length ) ) ; } }


"Count Strictly Increasing Subarrays | C # program to count number of strictly increasing subarrays ; Initialize result ; Initialize length of current increasing subarray ; Traverse through the array ; If arr [ i + 1 ] is greater than arr [ i ] , then increment length ; Else Update count and reset length ; If last length is more than 1 ; Driver method to test the above function"
Solution in C#:

using System ; class GFG { static int [ ] arr = new int [ ] { 1 , 2 , 2 , 4 } ; static int countIncreasing ( int n ) { int cnt = 0 ; int len = 1 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { if ( arr [ i + 1 ] > arr [ i ] ) len ++ ; else { cnt += ( ( ( len - 1 ) * len ) / 2 ) ; len = 1 ; } } if ( len > 1 ) cnt += ( ( ( len - 1 ) * len ) / 2 ) ; return cnt ; } public static void Main ( ) { Console . WriteLine ( " Count ▁ of ▁ strictly ▁ " + " increasing ▁ subarrays ▁ is ▁ " + countIncreasing ( arr . Length ) ) ; } }


"Count Triplets such that one of the numbers can be written as sum of the other two | C # program to count Triplets such that at least one of the numbers can be written as sum of the other two ; Function to count the number of ways to choose the triples ; compute the max value in the array and create frequency array of size max_val + 1. We can also use HashMap to store frequencies . We have used an array to keep remaining code simple . ; Case 1 : 0 , 0 , 0 ; Case 2 : 0 , x , x ; Case 3 : x , x , 2 * x ; Case 4 : x , y , x + y iterate through all pairs ( x , y ) ; Driver code"
Solution in C#:

using System ; class GFG { static int countWays ( int [ ] arr , int n ) { int max_val = 0 ; for ( int i = 0 ; i < n ; i ++ ) max_val = Math . Max ( max_val , arr [ i ] ) ; int [ ] freq = new int [ max_val + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] ++ ; ans += freq [ 0 ] * ( freq [ 0 ] - 1 ) * ( freq [ 0 ] - 2 ) / 6 ; for ( int i = 1 ; i <= max_val ; i ++ ) ans += freq [ 0 ] * freq [ i ] * ( freq [ i ] - 1 ) / 2 ; for ( int i = 1 ; 2 * i <= max_val ; i ++ ) ans += freq [ i ] * ( freq [ i ] - 1 ) / 2 * freq [ 2 * i ] ; for ( int i = 1 ; i <= max_val ; i ++ ) { for ( int j = i + 1 ; i + j <= max_val ; j ++ ) ans += freq [ i ] * freq [ j ] * freq [ i + j ] ; } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; Console . WriteLine ( countWays ( arr , n ) ) ; } }


"Count all N digit numbers whose digits are multiple of X | C # program for the above approach ; Function to calculate x ^ n using binary - exponentiation ; Stores the resultant power ; Stores the value of x ^ ( n / 2 ) ; Function to count aint N - digit numbers whose digits are multiples of x ; Count aint digits which are multiples of x ; Check if current number is a multiple of X ; Increase count of multiples ; Check if it 's a 1 digit number ; Count the total numbers ; Return the total numbers ; Driver Code ; Given N and X ; Function call"
Solution in C#:

using System ; class GFG { static int power ( int x , int n ) { int temp ; if ( n == 0 ) return 1 ; temp = power ( x , n / 2 ) ; if ( n % 2 == 0 ) return temp * temp ; else return x * temp * temp ; } static int count_Total_Numbers ( int n , int x ) { int total , multiples = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) { if ( i % x == 0 ) multiples ++ ; } if ( n == 1 ) return multiples ; total = ( multiples - 1 ) * power ( multiples , n - 1 ) ; return total ; } public static void Main ( ) { int N = 1 , X = 3 ; Console . Write ( count_Total_Numbers ( N , X ) ) ; } }


"Count all N | C # program for the above approach ; Function to print the count of arrays satisfying given condition ; First element of array is set as 1 ; Since the first element of arr [ ] is 1 , the second element can 't be 1 ; Traverse the remaining indices ; If arr [ i ] = 1 ; If arr [ i ] a 1 ; Since last element needs to be 1 ; Driver code ; Stores the count of arrays where arr [ 0 ] = arr [ N - 1 ] = 1 ; Since arr [ 0 ] and arr [ N - 1 ] can be any number from 1 to M ; Print answer"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int totalArrays ( int N , int M ) { int [ ] end_with_one = new int [ N + 1 ] ; int [ ] end_not_with_one = new int [ N + 1 ] ; end_with_one [ 0 ] = 1 ; end_not_with_one [ 0 ] = 0 ; end_with_one [ 1 ] = 0 ; end_not_with_one [ 1 ] = M - 1 ; for ( int i = 2 ; i < N ; i ++ ) { end_with_one [ i ] = end_not_with_one [ i - 1 ] ; end_not_with_one [ i ] = end_with_one [ i - 1 ] * ( M - 1 ) + end_not_with_one [ i - 1 ] * ( M - 2 ) ; } return end_with_one [ N - 1 ] ; } static void Main ( ) { int N = 3 , M = 3 ; int temp = totalArrays ( N , M ) ; int ans = M * temp ; Console . WriteLine ( ans ) ; } }


"Count all Prime Length Palindromic Substrings | C # implementation of the approach ; Function that returns true if sub - string starting at i and ending at j in str is a palindrome ; Function to count all palindromic substring whose lwngth is a prime number ; 0 and 1 are non - primes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; To store the required number of sub - strings ; Starting from the smallest prime till the largest length of the sub - string possible ; If j is prime ; Check all the sub - strings of length j ; If current sub - string is a palindrome ; Driver code"
Solution in C#:

using System ; class GfG { static bool isPalindrome ( string str , int i , int j ) { while ( i < j ) { if ( str [ i ] != str [ j ] ) return false ; i ++ ; j -- ; } return true ; } static int countPrimePalindrome ( string str , int len ) { bool [ ] prime = new bool [ len + 1 ] ; Array . Fill ( prime , true ) ; prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p * p <= len ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * p ; i <= len ; i += p ) prime [ i ] = false ; } } int count = 0 ; for ( int j = 2 ; j <= len ; j ++ ) { if ( prime [ j ] ) { for ( int i = 0 ; i + j - 1 < len ; i ++ ) { if ( isPalindrome ( str , i , i + j - 1 ) ) count ++ ; } } } return count ; } public static void Main ( ) { string s = " geeksforgeeks " ; int len = s . Length ; Console . WriteLine ( countPrimePalindrome ( s , len ) ) ; } }


"Count all disjoint pairs having absolute difference at least K from a given array | C # program for the above approach ; Function to check if it is possible to form M pairs with abs diff at least K ; Traverse the array over [ 0 , M ] ; If valid index ; Return 1 ; Function to count distinct pairs with absolute difference atleast K ; Stores the count of all possible pairs ; Initialize left and right ; Sort the array ; Perform Binary Search ; Find the value of mid ; Check valid index ; Update ans ; Print the answer ; Driver Code ; Given array arr [ ] ; Given difference K ; Size of the array ; Function call"
Solution in C#:

using System ; class GFG { static int isValid ( int [ ] arr , int n , int m , int d ) { for ( int i = 0 ; i < m ; i ++ ) { if ( Math . Abs ( arr [ n - m + i ] - arr [ i ] ) < d ) { return 0 ; } } return 1 ; } static void countPairs ( int [ ] arr , int N , int K ) { int ans = 0 ; int left = 0 , right = N / 2 + 1 ; Array . Sort ( arr ) ; while ( left < right ) { int mid = ( left + right ) / 2 ; if ( isValid ( arr , N , mid , K ) == 1 ) { ans = mid ; left = mid + 1 ; } else right = mid - 1 ; } Console . WriteLine ( ans ) ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 3 , 5 } ; int K = 2 ; int N = arr . Length ; countPairs ( arr , N , K ) ; } }


"Count all disjoint pairs having absolute difference at least K from a given array | C # program for the above approach ; Function to count distinct pairs with absolute difference atleast K ; Track the element that have been paired ; Stores count of distinct pairs ; Pick all elements one by one ; If already visited ; If already visited ; If difference is at least K ; Mark element as visited and increment the count ; Print the final count ; Driver Code ; Given arr [ ] ; Size of array ; Given difference K ; Function Call"
Solution in C#:

using System ; class GFG { static void countPairsWithDiffK ( int [ ] arr , int N , int K ) { int [ ] vis = new int [ N ] ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( vis [ i ] == 1 ) continue ; for ( int j = i + 1 ; j < N ; j ++ ) { if ( vis [ j ] == 1 ) continue ; if ( Math . Abs ( arr [ i ] - arr [ j ] ) >= K ) { count ++ ; vis [ i ] = 1 ; vis [ j ] = 1 ; break ; } } } Console . Write ( count ) ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 3 , 5 } ; int N = arr . Length ; int K = 2 ; countPairsWithDiffK ( arr , N , K ) ; } }


"Count all distinct pairs of repeating elements from the array for every array element | C # program for the above approach ; Function to print the required count of pairs excluding the current element ; Store the frequency ; Find all the count ; Delete the contribution of each element for equal pairs ; Print the answer ; Driver Code ; Given array [ ] arr ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void solve ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } } int cnt = 0 ; foreach ( KeyValuePair < int , int > x in mp ) { cnt += ( ( x . Value ) * ( x . Value - 1 ) / 2 ) ; } int [ ] ans = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { ans [ i ] = cnt - ( mp [ arr [ i ] ] - 1 ) ; } for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 1 , 2 } ; int N = arr . Length ; solve ( arr , N ) ; } }