"Bellman Ford Algorithm ( Simple Implementation ) | C # program for Bellman - Ford 's single source shortest path algorithm. ; The main function that finds shortest distances from src to all other vertices using Bellman - Ford algorithm . The function also detects negative weight cycle The row graph [ i ] represents i - th edge with three values u , v and w . ; Initialize distance of all vertices as infinite . ; initialize distance of source as 0 ; Relax all edges | V | - 1 times . A simple shortest path from src to any other vertex can have at - most | V | - 1 edges ; check for negative - weight cycles . The above step guarantees shortest distances if graph doesn 't contain  negative weight cycle. If we get a  shorter path, then there is a cycle. ; Driver code ; Every edge has three values ( u , v , w ) where the edge is from vertex u to v . And weight of the edge is w ."
Solution in C#:

using System ; class GFG { static void BellmanFord ( int [ , ] graph , int V , int E , int src ) { int [ ] dis = new int [ V ] ; for ( int i = 0 ; i < V ; i ++ ) dis [ i ] = int . MaxValue ; dis [ src ] = 0 ; for ( int i = 0 ; i < V - 1 ; i ++ ) { for ( int j = 0 ; j < E ; j ++ ) { if ( dis [ graph [ j , 0 ] ] = int . MaxValue && dis [ graph [ j , 0 ] ] + graph [ j , 2 ] < dis [ graph [ j , 1 ] ] ) dis [ graph [ j , 1 ] ] = dis [ graph [ j , 0 ] ] + graph [ j , 2 ] ; } } for ( int i = 0 ; i < E ; i ++ ) { int x = graph [ i , 0 ] ; int y = graph [ i , 1 ] ; int weight = graph [ i , 2 ] ; if ( dis [ x ] != int . MaxValue && dis [ x ] + weight < dis [ y ] ) Console . WriteLine ( " Graph ▁ contains ▁ negative " + " ▁ weight ▁ cycle " ) ; } Console . WriteLine ( " Vertex ▁ Distance ▁ from ▁ Source " ) ; for ( int i = 0 ; i < V ; i ++ ) Console . WriteLine ( i + " 	 	 " + dis [ i ] ) ; } public static void Main ( String [ ] args ) { int [ , ] graph = { { 0 , 1 , - 1 } , { 0 , 2 , 4 } , { 1 , 2 , 3 } , { 1 , 3 , 2 } , { 1 , 4 , 2 } , { 3 , 2 , 5 } , { 3 , 1 , 1 } , { 4 , 3 , - 3 } } ; BellmanFord ( graph , V , E , 0 ) ; } }


"Best meeting point in 2D binary array | C # program to find best meeting point in 2D array ; Find all members home 's position ; Sort positions so we can find most beneficial point ; middle position will always beneficial for all group members but it will be sorted which we have already done ; Now find total distance from best meeting point ( x , y ) using Manhattan Distance formula ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int ROW = 3 ; static int COL = 5 ; static int minTotalDistance ( int [ , ] grid ) { if ( ROW == 0 COL == 0 ) return 0 ; List < int > vertical = new List < int > ( ) ; List < int > horizontal = new List < int > ( ) ; for ( int i = 0 ; i < ROW ; i ++ ) { for ( int j = 0 ; j < COL ; j ++ ) { if ( grid [ i , j ] == 1 ) { vertical . Add ( i ) ; horizontal . Add ( j ) ; } } } vertical . Sort ( ) ; horizontal . Sort ( ) ; int size = vertical . Count / 2 ; int x = vertical [ size ] ; int y = horizontal [ size ] ; int distance = 0 ; for ( int i = 0 ; i < ROW ; i ++ ) for ( int j = 0 ; j < COL ; j ++ ) if ( grid [ i , j ] == 1 ) distance += Math . Abs ( x - i ) + Math . Abs ( y - j ) ; return distance ; } public static void Main ( String [ ] args ) { int [ , ] grid = { { 1 , 0 , 1 , 0 , 1 } , { 0 , 1 , 0 , 0 , 0 } , { 0 , 1 , 1 , 0 , 0 } } ; Console . WriteLine ( minTotalDistance ( grid ) ) ; } }


"Biggest Reuleaux Triangle inscirbed within a square inscribed in a semicircle | C # Program to find the biggest Reuleaux triangle inscribed within in a square which in turn is inscribed within a semicircle ; Function to find the biggest reuleaux triangle ; radius cannot be negative ; height of the reuleaux triangle ; area of the reuleaux triangle ; Driver code"
Solution in C#:

using System ; class GFG { static double Area ( double r ) { if ( r < 0 ) return - 1 ; double x = ( 2 * r ) / ( double ) ( Math . Sqrt ( 5 ) ) ; double A = 0.70477 * ( double ) ( Math . Pow ( x , 2 ) ) ; return A ; } public static void Main ( ) { double r = 5 ; Console . WriteLine ( Area ( r ) ) ; } }


"Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle | C # Program to find the biggest Reuleaux triangle inscribed within in a square which in turn is inscribed within an equilateral triangle ; Function to find the biggest reuleaux triangle ; side cannot be negative ; height of the reuleaux triangle ; area of the reuleaux triangle ; Driver code"
Solution in C#:

using System ; class GFG { static float Area ( float a ) { if ( a < 0 ) return - 1 ; float x = 0.464f * a ; float A = 0.70477f * ( float ) Math . Pow ( x , 2 ) ; return A ; } public static void Main ( ) { float a = 5 ; Console . WriteLine ( String . Format ( " { 0,0 : # .00000 } " , Area ( a ) ) ) ; } }


"Biggest Reuleaux Triangle inscribed within a square which is inscribed within a hexagon | C # Program to find the biggest Reuleaux triangle inscribed within in a square which in turn is inscribed within a hexagon ; Function to find the biggest reuleaux triangle ; side cannot be negative ; height of the reuleaux triangle ; area of the reuleaux triangle ; Driver code"
Solution in C#:

using System ; class GFG { static float Area ( float a ) { if ( a < 0 ) return - 1 ; float h = ( float ) 1.268 * a ; float A = ( float ) ( 0.70477 * Math . Pow ( h , 2 ) ) ; return A ; } public static void Main ( ) { float a = 5 ; Console . WriteLine ( Area ( a ) ) ; } }


"Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse | C # Program to find the biggest Reuleaux triangle inscribed within in a square which in turn is inscribed within an ellipse ; Function to find the biggest reuleaux triangle ; length of the axes cannot be negative ; height of the reuleaux triangle ; area of the reuleaux triangle ; Driver code"
Solution in C#:

using System ; class GFG { static double Area ( double a , double b ) { if ( a < 0 && b < 0 ) return - 1 ; double h = ( double ) Math . Sqrt ( ( ( Math . Pow ( a , 2 ) + Math . Pow ( b , 2 ) ) / ( Math . Pow ( a , 2 ) * Math . Pow ( b , 2 ) ) ) ) ; double A = ( double ) ( 0.70477 * Math . Pow ( h , 2 ) ) ; return A ; } static void Main ( ) { double a = 5 , b = 4 ; Console . WriteLine ( Math . Round ( Area ( a , b ) , 7 ) ) ; } }


"Biggest Reuleaux Triangle within A Square | C # program to find area of the biggest Reuleaux triangle that can be inscribed within a square ; Function to find the area of the reuleaux triangle ; Side cannot be negative ; Area of the reauleaux triangle ; Driver code"
Solution in C#:

using System ; class GFG { static double reuleauxArea ( double a ) { if ( a < 0 ) return - 1 ; double A = 0.70477 * Math . Pow ( a , 2 ) ; return A ; } static public void Main ( ) { double a = 6 ; Console . WriteLine ( reuleauxArea ( a ) ) ; } }


"Biggest Reuleaux Triangle within a Square which is inscribed within a Circle | C # Program to find the biggest Reuleaux triangle inscribed within in a square which in turn is inscribed within a circle ; Function to find the Area of the Reuleaux triangle ; radius cannot be negative ; Area of the Reuleaux triangle ; Driver code"
Solution in C#:

using System ; class GFG { static double ReuleauxArea ( double r ) { if ( r < 0 ) return - 1 ; double A = 0.70477 * 2 * Math . Pow ( r , 2 ) ; return A ; } public static void Main ( ) { double r = 6 ; Console . WriteLine ( ReuleauxArea ( r ) ) ; } }


"Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle | C # Program to find the biggest Reuleaux triangle inscribed within in a square which in turn is inscribed within a circle ; Function to find the biggest reuleaux triangle ; the height or base or hypotenuse cannot be negative ; height of the reuleaux triangle ; area of the reuleaux triangle ; Driver code"
Solution in C#:

using System ; class GFG { static double Area ( double l , double b , double h ) { if ( l < 0 b < 0 h < 0 ) return - 1 ; double x = ( l * b ) / ( l + b ) ; double A = 0.70477 * Math . Pow ( x , 2 ) ; return A ; } public static void Main ( ) { double l = 5 , b = 12 , h = 13 ; Console . WriteLine ( ( Area ( l , b , h ) ) ) ; } }


"Bin Packing Problem ( Minimize number of used Bins ) | C # program to find number of bins required using Best fit algorithm . ; Returns number of bins required using best fit online algorithm ; Initialize result ( Count of bins ) ; Create an array to store remaining space in bins there can be at most n bins ; Place items one by one ; Find the best bin that can accommodate weight [ i ] ; Initialize minimum space left and index of best bin ; If no bin could accommodate weight [ i ] , create a new bin ; Assign the item to best bin ; Driver code"
Solution in C#:

using System ; class GFG { static int bestFit ( int [ ] weight , int n , int c ) { int res = 0 ; int [ ] bin_rem = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int j ; int min = c + 1 , bi = 0 ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] && bin_rem [ j ] - weight [ i ] < min ) { bi = j ; min = bin_rem [ j ] - weight [ i ] ; } } if ( min == c + 1 ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; } else bin_rem [ bi ] -= weight [ i ] ; } return res ; } public static void Main ( String [ ] args ) { int [ ] weight = { 2 , 5 , 4 , 7 , 1 , 3 , 8 } ; int c = 10 ; int n = weight . Length ; Console . Write ( " Number ▁ of ▁ bins ▁ required ▁ in ▁ Best ▁ Fit ▁ : ▁ " + bestFit ( weight , n , c ) ) ; } }


"Bin Packing Problem ( Minimize number of used Bins ) | C # program to find number of bins required using First Fit algorithm . ; Returns number of bins required using first fit online algorithm ; Initialize result ( Count of bins ) ; Create an array to store remaining space in bins there can be at most n bins ; Place items one by one ; Find the first bin that can accommodate weight [ i ] ; If no bin could accommodate weight [ i ] ; Driver code"
Solution in C#:

using System ; class GFG { static int firstFit ( int [ ] weight , int n , int c ) { int res = 0 ; int [ ] bin_rem = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] ) { bin_rem [ j ] = bin_rem [ j ] - weight [ i ] ; break ; } } if ( j == res ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; } } return res ; } public static void Main ( String [ ] args ) { int [ ] weight = { 2 , 5 , 4 , 7 , 1 , 3 , 8 } ; int c = 10 ; int n = weight . Length ; Console . Write ( " Number ▁ of ▁ bins ▁ required ▁ in ▁ First ▁ Fit ▁ : ▁ " + firstFit ( weight , n , c ) ) ; } }


"Bin Packing Problem ( Minimize number of used Bins ) | C # program to find number of bins required using Worst fit algorithm . ; Returns number of bins required using worst fit online algorithm ; Initialize result ( Count of bins ) ; Create an array to store remaining space in bins there can be at most n bins ; Place items one by one ; Find the best bin that ca \ n accommodate weight [ i ] ; Initialize maximum space left and index of worst bin ; If no bin could accommodate weight [ i ] , create a new bin ; else Assign the item to best bin ; Driver program"
Solution in C#:

using System ; class GFG { static int worstFit ( int [ ] weight , int n , int c ) { int res = 0 ; int [ ] bin_rem = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int j ; int mx = - 1 , wi = 0 ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] && bin_rem [ j ] - weight [ i ] > mx ) { wi = j ; mx = bin_rem [ j ] - weight [ i ] ; } } if ( mx == - 1 ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; } bin_rem [ wi ] -= weight [ i ] ; } return res ; } public static void Main ( String [ ] args ) { int [ ] weight = { 2 , 5 , 4 , 7 , 1 , 3 , 8 } ; int c = 10 ; int n = weight . Length ; Console . Write ( " Number ▁ of ▁ bins ▁ required ▁ in ▁ Worst ▁ Fit ▁ : ▁ " + worstFit ( weight , n , c ) ) ; } }


"Binary Indexed Tree : Range Updates and Point Queries | C # code to demonstrate Range Update and * Point Queries on a Binary Index Tree . * This method only works when all array * values are initially 0. ; Max tree size ; Updates a node in Binary Index Tree ( BITree ) at given index in BITree . The given value ' val ' is added to BITree [ i ] and all of its ancestors in tree . ; index in BITree [ ] is 1 more than the index in arr [ ] ; Traverse all ancestors and add ' val ' ; Add ' val ' to current node of BITree ; Update index to that of parent in update View ; Constructs Binary Indexed Tree for given array of size n . ; Initialize BITree [ ] as 0 ; Store the actual values in BITree [ ] using update ( ) ; SERVES THE PURPOSE OF getElement ( ) Returns sum of arr [ 0. . index ] . This function assumes that the array is preprocessed and partial sums of array elements are stored in BITree [ ] ; Initialize result ; index in BITree [ ] is 1 more than the index in arr [ ] ; Traverse ancestors of BITree [ index ] ; Add current element of BITree to sum ; Move index to parent node in getSum View ; Return the sum ; Updates such that getElement ( ) gets an increased value when queried from l to r . ; Increase value at ' l ' by ' val ' ; Decrease value at ' r + 1' by ' val ' ; Driver Code ; Add 2 to all the element from [ 2 , 4 ] ; Find the element at Index 4 ; Add 2 to all the element from [ 0 , 3 ] ; Find the element at Index 3"
Solution in C#:

using System ; public class GFG { public const int MAX = 1000 ; public static int [ ] BITree = new int [ MAX ] ; public static void updateBIT ( int n , int index , int val ) { index = index + 1 ; while ( index <= n ) { BITree [ index ] += val ; index += index & ( - index ) ; } } public static void constructBITree ( int [ ] arr , int n ) { for ( int i = 1 ; i <= n ; i ++ ) { BITree [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { updateBIT ( n , i , arr [ i ] ) ; } } public static int getSum ( int index ) { int sum = 0 ; index = index + 1 ; while ( index > 0 ) { sum += BITree [ index ] ; index -= index & ( - index ) ; } return sum ; } public static void update ( int l , int r , int n , int val ) { updateBIT ( n , l , val ) ; updateBIT ( n , r + 1 , - val ) ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 0 , 0 , 0 , 0 , 0 } ; int n = arr . Length ; constructBITree ( arr , n ) ; int l = 2 , r = 4 , val = 2 ; update ( l , r , n , val ) ; int index = 4 ; Console . WriteLine ( " Element ▁ at ▁ index ▁ " + index + " ▁ is ▁ " + getSum ( index ) ) ; l = 0 ; r = 3 ; val = 4 ; update ( l , r , n , val ) ; index = 3 ; Console . WriteLine ( " Element ▁ at ▁ index ▁ " + index + " ▁ is ▁ " + getSum ( index ) ) ; } }


"Binary Indexed Tree : Range Updates and Point Queries | C # program to demonstrate Range Update and Point Queries Without using BIT ; Updates such that getElement ( ) gets an increased value when queried from l to r . ; Get the element indexed at i ; To get ith element sum of all the elements from 0 to i need to be computed ; Driver code ; Find the element at Index 4 ; Find the element at Index 3"
Solution in C#:

using System ; class GfG { static void update ( int [ ] arr , int l , int r , int val ) { arr [ l ] += val ; if ( r + 1 < arr . Length ) arr [ r + 1 ] -= val ; } static int getElement ( int [ ] arr , int i ) { int res = 0 ; for ( int j = 0 ; j <= i ; j ++ ) res += arr [ j ] ; return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 0 , 0 , 0 , 0 } ; int n = arr . Length ; int l = 2 , r = 4 , val = 2 ; update ( arr , l , r , val ) ; int index = 4 ; Console . WriteLine ( " Element ▁ at ▁ index ▁ " + index + " ▁ is ▁ " + getElement ( arr , index ) ) ; l = 0 ; r = 3 ; val = 4 ; update ( arr , l , r , val ) ; index = 3 ; Console . WriteLine ( " Element ▁ at ▁ index ▁ " + index + " ▁ is ▁ " + getElement ( arr , index ) ) ; } }


"Binary Matrix after flipping submatrices in given range for Q queries | C # program to implement the above approach ; Function to flip a submatrices ; Boundaries of the submatrix ; Iterate over the submatrix ; Check for 1 or 0 and flip accordingly ; Function to perform the queries ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static void manipulation ( int [ , ] matrix , int [ ] q ) { int x1 = q [ 0 ] , y1 = q [ 1 ] , x2 = q [ 2 ] , y2 = q [ 3 ] ; for ( int i = x1 - 1 ; i < x2 ; i ++ ) { for ( int j = y1 - 1 ; j < y2 ; j ++ ) { if ( matrix [ i , j ] == 1 ) matrix [ i , j ] = 0 ; else matrix [ i , j ] = 1 ; } } } public static int [ ] GetRow ( int [ , ] matrix , int row ) { var rowLength = matrix . GetLength ( 1 ) ; var rowVector = new int [ rowLength ] ; for ( var i = 0 ; i < rowLength ; i ++ ) rowVector [ i ] = matrix [ row , i ] ; return rowVector ; } static void queries_fxn ( int [ , ] matrix , int [ , ] queries ) { for ( int i = 0 ; i < queries . GetLength ( 0 ) ; i ++ ) manipulation ( matrix , GetRow ( queries , i ) ) ; } public static void Main ( String [ ] args ) { int [ , ] matrix = { { 0 , 1 , 0 } , { 1 , 1 , 0 } } ; int [ , ] queries = { { 1 , 1 , 2 , 3 } , { 1 , 1 , 1 , 1 } , { 1 , 2 , 2 , 3 } } ; queries_fxn ( matrix , queries ) ; Console . Write ( " [ " ) ; for ( int i = 0 ; i < matrix . GetLength ( 0 ) ; i ++ ) { Console . Write ( " [ " ) ; for ( int j = 0 ; j < matrix . GetLength ( 1 ) ; j ++ ) Console . Write ( matrix [ i , j ] + " , ▁ " ) ; if ( i == matrix . Length - 1 ) Console . Write ( " ] " ) ; else Console . Write ( " ] , ▁ " ) ; } Console . Write ( " ] " ) ; } }


"Binary Search Tree insert with Parent Pointer | C # program to demonstrate insert operation in binary search tree with parent pointer ; Node structure ; A utility function to create a new BST Node ; A utility function to do inorder traversal of BST ; A utility function to insert a new Node with given key in BST ; If the tree is empty , return a new Node ; Otherwise , recur down the tree ; Set parent of root of left subtree ; Set parent of root of right subtree ; return the ( unchanged ) Node pointer ; Driver code ; Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80 ; print iNoder traversal of the BST"
Solution in C#:

using System ; class GfG { class Node { public int key ; public Node left , right , parent ; } static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . key = item ; temp . left = null ; temp . right = null ; temp . parent = null ; return temp ; } static void inorder ( Node root ) { if ( root != null ) { inorder ( root . left ) ; Console . Write ( " Node ▁ : ▁ " + root . key + " ▁ , ▁ " ) ; if ( root . parent == null ) Console . WriteLine ( " Parent ▁ : ▁ NULL " ) ; else Console . WriteLine ( " Parent ▁ : ▁ " + root . parent . key ) ; inorder ( root . right ) ; } } static Node insert ( Node node , int key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) { Node lchild = insert ( node . left , key ) ; node . left = lchild ; lchild . parent = node ; } else if ( key > node . key ) { Node rchild = insert ( node . right , key ) ; node . right = rchild ; rchild . parent = node ; } return node ; } public static void Main ( String [ ] args ) { Node root = null ; root = insert ( root , 50 ) ; insert ( root , 30 ) ; insert ( root , 20 ) ; insert ( root , 40 ) ; insert ( root , 70 ) ; insert ( root , 60 ) ; insert ( root , 80 ) ; inorder ( root ) ; } }


"Binary Search Tree | Set 1 ( Search and Insertion ) | A utility function to search a given key in BST ; Base Cases : root is null or key is present at root ; Key is greater than root 's key ; Key is smaller than root 's key"
Solution in C#:

public Node search ( Node root , int key ) { if ( root == null root . key == key ) return root ; if ( root . key < key ) return search ( root . right , key ) ; return search ( root . left , key ) ; }


"Binary Search a String | C # program to implement Binary Search for strings ; Returns index of x if it is present in arr [ ] , else return - 1 ; Check if x is present at mid ; If x greater , ignore left half ; If x is smaller , ignore right half ; Driver method to test above"
Solution in C#:

using System ; class GFG { static int binarySearch ( String [ ] arr , String x ) { int l = 0 , r = arr . Length - 1 ; while ( l <= r ) { int m = l + ( r - l ) / 2 ; int res = x . CompareTo ( arr [ m ] ) ; if ( res == 0 ) return m ; if ( res > 0 ) l = m + 1 ; else r = m - 1 ; } return - 1 ; } public static void Main ( String [ ] args ) { String [ ] arr = { " contribute " , " geeks " , " ide " , " practice " } ; String x = " ide " ; int result = binarySearch ( arr , x ) ; if ( result == - 1 ) Console . WriteLine ( " Element ▁ not ▁ present " ) ; else Console . WriteLine ( " Element ▁ found ▁ at ▁ " + " index ▁ " + result ) ; } }


"Binary Search | C # implementation of iterative Binary Search ; Returns index of x if it is present in arr [ ] , else return - 1 ; Check if x is present at mid ; If x greater , ignore left half ; If x is smaller , ignore right half ; if we reach here , then element was not present ; Driver method to test above"
Solution in C#:

using System ; class GFG { static int binarySearch ( int [ ] arr , int x ) { int l = 0 , r = arr . Length - 1 ; while ( l <= r ) { int m = l + ( r - l ) / 2 ; if ( arr [ m ] == x ) return m ; if ( arr [ m ] < x ) l = m + 1 ; else r = m - 1 ; } return - 1 ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 10 , 40 } ; int n = arr . Length ; int x = 10 ; int result = binarySearch ( arr , x ) ; if ( result == - 1 ) Console . WriteLine ( " Element ▁ not ▁ present " ) ; else Console . WriteLine ( " Element ▁ found ▁ at ▁ " + " index ▁ " + result ) ; } }


"Binary Search | C # implementation of recursive Binary Search ; Returns index of x if it is present in arr [ l . . r ] , else return - 1 ; If the element is present at the middle itself ; If element is smaller than mid , then it can only be present in left subarray ; Else the element can only be present in right subarray ; We reach here when element is not present in array ; Driver method to test above"
Solution in C#:

using System ; class GFG { static int binarySearch ( int [ ] arr , int l , int r , int x ) { if ( r >= l ) { int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return - 1 ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 10 , 40 } ; int n = arr . Length ; int x = 10 ; int result = binarySearch ( arr , 0 , n - 1 , x ) ; if ( result == - 1 ) Console . WriteLine ( " Element ▁ not ▁ present " ) ; else Console . WriteLine ( " Element ▁ found ▁ at ▁ index ▁ " + result ) ; } }


"Binary String of given length that without a palindrome of size 3 | C # program find a binary String of given length that doesn 't contain a palindrome of size 3. ; Printing the character according to i ; Driver code"
Solution in C#:

using System ; class GFG { static void generateString ( int n ) { String s = " " ; for ( int i = 0 ; i < n ; i ++ ) s += ( ( i & 2 ) > 1 ? ' b ' : ' a ' ) ; Console . WriteLine ( s ) ; } public static void Main ( String [ ] args ) { int n = 5 ; generateString ( n ) ; n = 8 ; generateString ( n ) ; n = 10 ; generateString ( n ) ; } }


"Binary Tree ( Array implementation ) | C # implementation of tree using array numbering starting from 0 to n - 1. ; create root ; create left son of root ; create right son of root ; Print tree ; Driver Code"
Solution in C#:

 using System ; class Array_imp { static int root = 0 ; static String [ ] str = new String [ 10 ] ; public void Root ( String key ) { str [ 0 ] = key ; } public void set_Left ( String key , int root ) { int t = ( root * 2 ) + 1 ; if ( str [ root ] == null ) { Console . Write ( " Can ' t ▁ set ▁ child ▁ at ▁ { 0 } , ▁ no ▁ parent ▁ found
 " , t ) ; } else { str [ t ] = key ; } } public void set_Right ( String key , int root ) { int t = ( root * 2 ) + 2 ; if ( str [ root ] == null ) { Console . Write ( " Can ' t ▁ set ▁ child ▁ at ▁ { 0 } , ▁ no ▁ parent ▁ found
 " , t ) ; } else { str [ t ] = key ; } } public void print_Tree ( ) { for ( int i = 0 ; i < 10 ; i ++ ) { if ( str [ i ] != null ) Console . Write ( str [ i ] ) ; else Console . Write ( " - " ) ; } } } public class Tree { public static void Main ( String [ ] args ) { Array_imp obj = new Array_imp ( ) ; obj . Root ( " A " ) ; obj . set_Right ( " C " , 0 ) ; obj . set_Left ( " D " , 1 ) ; obj . set_Right ( " E " , 1 ) ; obj . set_Left ( " F " , 2 ) ; obj . print_Tree ( ) ; } }


"Binary Tree | Set 1 ( Introduction ) | A C # program to introduce Binary Tree ; Class containing left and right child of current node and key value ; Binary Tree ; Driver Code"
Solution in C#:

using System ; public class Node { public int key ; public Node left , right ; public Node ( int item ) { key = item ; left = right = null ; } } public class BinaryTree { Node root ; BinaryTree ( int key ) { root = new Node ( key ) ; } BinaryTree ( ) { root = null ; } public static void Main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; } }


"Binary Tree | Set 1 ( Introduction ) | Class containing left and right child of current node and key value"
Solution in C#:

class Node { int key ; Node left , right ; public Node ( int item ) { key = item ; left = right = null ; } }


"Binary array after M range toggle operations | C # program to find modified array after m range toggle operations . ; function for toggle ; function for final processing of array ; function for printing result ; Driver Code ; function call for toggle ; process array ; print result"
Solution in C#:

using System ; class GFG { static void command ( bool [ ] arr , int a , int b ) { arr [ a ] ^= true ; arr [ b + 1 ] ^= true ; } static void process ( bool [ ] arr , int n ) { for ( int k = 1 ; k <= n ; k ++ ) { arr [ k ] ^= arr [ k - 1 ] ; } } static void result ( bool [ ] arr , int n ) { for ( int k = 1 ; k <= n ; k ++ ) { if ( arr [ k ] == true ) Console . Write ( "1" + " ▁ " ) ; else Console . Write ( "0" + " ▁ " ) ; } } public static void Main ( ) { int n = 5 , m = 3 ; bool [ ] arr = new bool [ n + 2 ] ; command ( arr , 1 , 5 ) ; command ( arr , 2 , 5 ) ; command ( arr , 3 , 5 ) ; process ( arr , n ) ; result ( arr , n ) ; } }


"Binary representation of a given number |  ; bin function ; Driver code"
Solution in C#:

using System ; public class GFG { static void bin ( long n ) { long i ; Console . Write ( "0" ) ; for ( i = 1 << 30 ; i > 0 ; i = i / 2 ) { if ( ( n & i ) != 0 ) { Console . Write ( "1" ) ; } else { Console . Write ( "0" ) ; } } } static public void Main ( ) { bin ( 7 ) ; Console . WriteLine ( ) ; bin ( 4 ) ; } }


"Binary representation of a given number | C # implementation of above approach ; Function to convert decimal to binary number ; Driver code"
Solution in C#:

using System ; public class GFG { static void bin ( int n ) { if ( n > 1 ) bin ( n >> 1 ) ; Console . Write ( n & 1 ) ; } public static void Main ( ) { bin ( 131 ) ; Console . WriteLine ( ) ; bin ( 3 ) ; } }


"Binary representation of next greater number with same number of 1 ' s ▁ and ▁ 0' s | C # program to find next permutation in a binary string . ; Function to find the next greater number with same number of 1 ' s ▁ and ▁ 0' s ; locate first ' i ' from end such that bnum [ i ] == '0' and bnum [ i + 1 ] == '1' swap these value and break ; ; if no swapping performed ; Since we want the smallest next value , shift all 1 ' s ▁ at ▁ the ▁ end ▁ in ▁ the ▁ binary ▁ ▁ substring ▁ starting ▁ from ▁ index ▁ ' i + 2 ' ; special case while swapping if '0' occurs then break ; required next greater number ; Driver code"
Solution in C#:

using System ; class GFG { static String nextGreaterWithSameDigits ( char [ ] bnum ) { int l = bnum . Length ; int i ; for ( i = l - 2 ; i >= 1 ; i -- ) { if ( bnum [ i ] == '0' && bnum [ i + 1 ] == '1' ) { char ch = bnum [ i ] ; bnum [ i ] = bnum [ i + 1 ] ; bnum [ i + 1 ] = ch ; break ; } } if ( i == 0 ) Console . WriteLine ( " no ▁ greater ▁ number " ) ; int j = i + 2 , k = l - 1 ; while ( j < k ) { if ( bnum [ j ] == '1' && bnum [ k ] == '0' ) { char ch = bnum [ j ] ; bnum [ j ] = bnum [ k ] ; bnum [ k ] = ch ; j ++ ; k -- ; } else if ( bnum [ i ] == '0' ) break ; else j ++ ; } return String . Join ( " " , bnum ) ; } public static void Main ( String [ ] args ) { char [ ] bnum = "10010" . ToCharArray ( ) ; Console . WriteLine ( " Binary ▁ representation ▁ of ▁ next ▁ greater ▁ number ▁ = ▁ " + nextGreaterWithSameDigits ( bnum ) ) ; } }


"Binary string with given frequencies of sums of consecutive pairs of characters | C # program to generate a binary String with given frequencies of sums of consecutive pair of characters ; A Function that generates and returns the binary String ; P : Frequency of consecutive characters with sum 0 Q : Frequency of consecutive characters with sum 1 R : Frequency of consecutive characters with sum 2 ; If no consecutive character adds up to 1 ; Not possible if both P and Q are non - zero ; If P is not equal to 0 ; Append 0 P + 1 times ; Append 1 R + 1 times ; Append "01" to satisfy Q ; Append "0" P times ; Append "1" R times ; Driver Code"
Solution in C#:

using System ; class GFG { static String build_binary_str ( int p , int q , int r ) { String ans = " " ; if ( q == 0 ) { if ( p != 0 && r != 0 ) { return " - 1" ; } else { if ( p > 0 ) { ans = Strings ( p + 1 , '0' ) ; } else { ans = Strings ( r + 1 , '1' ) ; } } } else { for ( int i = 1 ; i <= q + 1 ; i ++ ) { if ( i % 2 == 0 ) { ans += '0' ; } else { ans += '1' ; } } ans = ans . Substring ( 0 , 1 ) + Strings ( p , '0' ) + ans . Substring ( 1 ) ; ans = Strings ( r , '1' ) + ans ; } return ans ; } static String Strings ( int p , char c ) { String ans = " " ; for ( int i = 0 ; i < p ; i ++ ) ans += c ; return ans ; } public static void Main ( String [ ] args ) { int p = 1 , q = 2 , r = 2 ; Console . Write ( build_binary_str ( p , q , r ) ) ; } }


"Binomial Random Variables | C # program to compute Binomial Probability . ; function to calculate nCr i . e . , number of ways to choose r out of n objects ; Since nCr is same as nC ( n - r ) To decrease number of iterations ; function to calculate binomial r . v . probability ; Driver code"
Solution in C#:

using System ; class GFG { static int nCr ( int n , int r ) { if ( r > n / 2 ) r = n - r ; int answer = 1 ; for ( int i = 1 ; i <= r ; i ++ ) { answer *= ( n - r + i ) ; answer /= i ; } return answer ; } static float binomialProbability ( int n , int k , float p ) { return nCr ( n , k ) * ( float ) Math . Pow ( p , k ) * ( float ) Math . Pow ( 1 - p , n - k ) ; } public static void Main ( ) { int n = 10 ; int k = 5 ; float p = ( float ) 1.0 / 3 ; float probability = binomialProbability ( n , k , p ) ; Console . Write ( " Probability ▁ of ▁ " + k ) ; Console . Write ( " ▁ heads ▁ when ▁ a ▁ coin ▁ " + " is ▁ tossed ▁ " + n ) ; Console . Write ( " ▁ times ▁ where ▁ " + " probability ▁ of ▁ each ▁ head ▁ is ▁ " + p ) ; Console . Write ( " ▁ is ▁ = ▁ " + probability ) ; } }


"Birthday Paradox | C # program to approximate number of people in Birthday Paradox problem . ; Returns approximate number of people for a given probability ; Driver code"
Solution in C#:

using System ; class GFG { static double find ( double p ) { return Math . Ceiling ( Math . Sqrt ( 2 * 365 * Math . Log ( 1 / ( 1 - p ) ) ) ) ; } public static void Main ( ) { Console . Write ( find ( 0.70 ) ) ; } }


"Bitonic point in the given linked list | C # implementation of the approach ; Node for linked list ; Function to insert a node at the head of the linked list ; Function to return the bitonic of the given linked list ; If list is empty ; If list contains only a single node ; Invalid bitonic sequence ; If current node is the bitonic point ; Get to the next node in the list ; Nodes must be in descending starting from here ; Out of order node ; Get to the next node in the list ; Driver code"
Solution in C#:

using System ; class GFG { public class Node { public int data ; public Node next ; } ; static Node push ( Node head_ref , int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } static int bitonic_point ( Node node ) { if ( node == null ) return - 1 ; if ( node . next == null ) return - 1 ; if ( node . data > node . next . data ) return - 1 ; while ( node . next != null ) { if ( node . data > node . next . data ) break ; node = node . next ; } int bitonicPoint = node . data ; while ( node . next != null ) { if ( node . data < node . next . data ) return - 1 ; node = node . next ; } return bitonicPoint ; } public static void Main ( String [ ] args ) { Node head = null ; head = push ( head , 100 ) ; head = push ( head , 201 ) ; head = push ( head , 399 ) ; head = push ( head , 490 ) ; head = push ( head , 377 ) ; head = push ( head , 291 ) ; head = push ( head , 100 ) ; Console . WriteLine ( bitonic_point ( head ) ) ; } }


"Bitonic string | C # program for the above approach ; Function to check if the given String is bitonic ; Check for increasing sequence ; If end of String has been reached ; Check for decreasing sequence ; If the end of String hasn 't  been reached ; Return true if bitonic ; Driver Code ; Given String ; Function call"
Solution in C#:

using System ; class GFG { static int checkBitonic ( char [ ] s ) { int i , j ; for ( i = 1 ; i < s . Length ; i ++ ) { if ( s [ i ] > s [ i - 1 ] ) continue ; if ( s [ i ] <= s [ i - 1 ] ) break ; } if ( i == s . Length - 1 ) return 1 ; for ( j = i + 1 ; j < s . Length ; j ++ ) { if ( s [ j ] < s [ j - 1 ] ) continue ; if ( s [ j ] >= s [ j - 1 ] ) break ; } i = j ; if ( i != s . Length ) return 0 ; return 1 ; } public static void Main ( String [ ] args ) { String s = " abcdfgcba " ; Console . Write ( ( checkBitonic ( s . ToCharArray ( ) ) == 1 ) ? " YES " : " NO " ) ; } }


"Bitwise AND of N binary strings | C # implementation of the above approach : ; Function to find the bitwise AND of all the binary strings ; To store the largest and the smallest string ' s ▁ size , ▁ We ▁ need ▁ this ▁ to ▁ add ▁ ▁ ' 0 's in the resultant string ; Reverse each string Since we need to perform AND operation on bits from Right to Left ; Update the respective length values ; Traverse bits from 0 to smallest string 's size ; If at this bit position , there is a 0 in any of the given strings then AND operation on current bit position will be 0 ; Add resultant bit to result ; Add 0 's to the string. ; Reverse the string Since we started from LEFT to RIGHT ; Return the resultant string ; Utility reversefunction ; Swap values of left and right ; Driver code"
Solution in C#:

using System ; class GfG { static String strBitwiseAND ( String [ ] arr , int n ) { String res = " " ; int smallest_size = int . MaxValue ; int largest_size = int . MinValue ; String temp = " " ; for ( int i = 0 ; i < n ; i ++ ) { temp += arr [ i ] ; arr [ i ] = reverse ( temp ) ; smallest_size = Math . Min ( smallest_size , arr [ i ] . Length ) ; largest_size = Math . Max ( largest_size , arr [ i ] . Length ) ; } for ( int i = 0 ; i < smallest_size ; i ++ ) { bool all_ones = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] [ i ] == '0' ) { all_ones = false ; break ; } } res += ( all_ones ? '1' : '0' ) ; } for ( int i = 0 ; i < largest_size - smallest_size ; i ++ ) res += '0' ; String temp1 = " " ; temp1 += res ; res = reverse ( temp1 ) ; return res ; } static String reverse ( String input ) { char [ ] temparray = input . ToCharArray ( ) ; int left , right = 0 ; right = temparray . Length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . Join ( " " , temparray ) ; } public static void Main ( String [ ] args ) { String [ ] arr = { "101" , "110110" , "111" } ; int n = arr . Length ; Console . WriteLine ( strBitwiseAND ( arr , n ) ) ; } }


"Bitwise AND of all the elements of array | C # program to find bitwise AND of all the elements in the array ; Function to calculate bitwise AND ; Initialise ans variable is arr [ 0 ] ; Traverse the array compute AND ; Return ans ; Driver Code ; Function Call to find AND"
Solution in C#:

using System ; class GFG { static int find_and ( int [ ] arr ) { int ans = arr [ 0 ] ; for ( int i = 0 ; i < arr . Length ; i ++ ) { ans = ( ans & arr [ i ] ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 9 , 11 } ; Console . Write ( find_and ( arr ) ) ; } }


"Bitwise AND of all unordered pairs from a given array | C # program to implement the above approach ; Function to calculate bitwise AND of all pairs from the given array ; Stores bitwise AND of all possible pairs ; Generate all possible pairs ; Calculate bitwise AND of each pair ; Driver Code"
Solution in C#:

using System ; class GFG { static int TotalAndPair ( int [ ] arr , int N ) { int totalAND = ( 1 << 30 ) - 1 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { totalAND &= arr [ i ] & arr [ j ] ; } } return totalAND ; } public static void Main ( ) { int [ ] arr = { 4 , 5 , 12 , 15 } ; int N = arr . Length ; Console . Write ( TotalAndPair ( arr , N ) ) ; } }


"Bitwise AND of sub | C # implementation of the approach ; Function to return the minimum possible value of | K - X | where X is the bitwise AND of the elements of some sub - array ; Check all possible sub - arrays ; Find the overall minimum ; Driver code"
Solution in C#:

using System ; class GFG { static int closetAND ( int [ ] arr , int n , int k ) { int ans = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { int X = arr [ i ] ; for ( int j = i ; j < n ; j ++ ) { X &= arr [ j ] ; ans = Math . Min ( ans , Math . Abs ( k - X ) ) ; } } return ans ; } public static void Main ( ) { int [ ] arr = { 4 , 7 , 10 } ; int n = arr . Length ; int k = 2 ; Console . WriteLine ( closetAND ( arr , n , k ) ) ; } }


"Bitwise AND of sub | C # implementation of the approach ; Function to return the minimum possible value of | K - X | where X is the bitwise AND of the elements of some sub - array ; Check all possible sub - arrays ; Find the overall minimum ; No need to perform more AND operations as | k - X | will increase ; Driver code"
Solution in C#:

using System ; class GFG { static int closetAND ( int [ ] arr , int n , int k ) { int ans = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { int X = arr [ i ] ; for ( int j = i ; j < n ; j ++ ) { X &= arr [ j ] ; ans = Math . Min ( ans , Math . Abs ( k - X ) ) ; if ( X <= k ) break ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 7 , 10 } ; int n = arr . Length ; int k = 2 ; Console . WriteLine ( closetAND ( arr , n , k ) ) ; } }


"Bitwise OR ( or | ) of a range | C # Program to find the bitwise OR of all the integers in range L - R ; Returns the Most Significant Bit Position ( MSB ) ; Returns the Bitwise OR of all integers between L and R ; Find the MSB position in L ; Find the MSB position in R ; Add this value until msb_p1 and msb_p2 are same ; ; Calculate msb_p1 and msb_p2 ; Find the max of msb_p1 and msb_p2 ; Set all the bits from msb_p1 upto 0 th bit in the result ; Driver Code"
Solution in C#:

using System ; class GFG { static int MSBPosition ( long N ) { int msb_p = - 1 ; while ( N > 0 ) { N = N >> 1 ; msb_p ++ ; } return msb_p ; } static long findBitwiseOR ( long L , long R ) { long res = 0 ; int msb_p1 = MSBPosition ( L ) ; int msb_p2 = MSBPosition ( R ) ; while ( msb_p1 == msb_p2 ) { long res_val = ( 1 << msb_p1 ) ; res += res_val ; L -= res_val ; R -= res_val ; msb_p1 = MSBPosition ( L ) ; msb_p2 = MSBPosition ( R ) ; } msb_p1 = Math . Max ( msb_p1 , msb_p2 ) ; for ( int i = msb_p1 ; i >= 0 ; i -- ) { long res_val = ( 1 << i ) ; res += res_val ; } return res ; } public static void Main ( ) { int L = 12 , R = 18 ; Console . WriteLine ( findBitwiseOR ( L , R ) ) ; } }


"Bitwise OR ( | ) of all even number from 1 to N | C # implementation of the above approach ; Function to return the bitwise OR of all even numbers upto N ; For value less than 2 ; Count total number of bits in bitwise or all bits will be set except last bit ; Compute 2 to the power bitCount and subtract 2 ; Driver code"
Solution in C#:

using System ; class GFG { static int bitwiseOrTillN ( int n ) { if ( n < 2 ) return 0 ; int bitCount = ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) + 1 ; return ( int ) Math . Pow ( 2 , bitCount ) - 2 ; } public static void Main ( ) { int n = 10 ; Console . WriteLine ( bitwiseOrTillN ( n ) ) ; } }


"Bitwise OR ( | ) of all even number from 1 to N | C # implementation of the above approach ; Function to return the bitwise OR of all the even numbers upto N ; Initialize result as 2 ; Driver code"
Solution in C#:

using System ; class GFG { static int bitwiseOrTillN ( int n ) { int result = 2 ; for ( int i = 4 ; i <= n ; i = i + 2 ) { result = result | i ; } return result ; } static public void Main ( ) { int n = 10 ; Console . WriteLine ( bitwiseOrTillN ( n ) ) ; } }


"Bitwise OR of Bitwise AND of all subsets of an Array for Q queries | C # program for the above approach ; Function to find the OR of AND of all subsets of the array for each query ; An array to store the bits ; Itearte for all the bits ; Iterate over all the numbers and store the bits in bits [ ] array ; Itearte over all the queries ; Replace the bits of the value at arr [ queries [ p ] [ 0 ] ] with the bits of queries [ p ] [ 1 ] ; Substitute the value in the array ; Find OR of the bits [ ] array ; Print the answer ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; class GFG { static void Or_of_Ands_for_each_query ( int [ ] arr , int n , int [ , ] queries , int q ) { int [ ] bits = new int [ 32 ] ; for ( int i = 0 ; i < 32 ; i ++ ) { bits [ i ] = 0 ; } for ( int i = 0 ; i < 32 ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( ( ( 1 << i ) & arr [ j ] ) != 0 ) { bits [ i ] ++ ; } } } for ( int p = 0 ; p < q ; p ++ ) { for ( int i = 0 ; i < 32 ; i ++ ) { if ( ( ( 1 << i ) & arr [ queries [ p , 0 ] ] ) != 0 ) { bits [ i ] -- ; } if ( ( queries [ p , 1 ] & ( 1 << i ) ) != 0 ) { bits [ i ] ++ ; } } arr [ queries [ p , 0 ] ] = queries [ p , 1 ] ; int ans = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { if ( bits [ i ] != 0 ) { ans |= ( 1 << i ) ; } } Console . WriteLine ( ans ) ; } } public static void Main ( String [ ] args ) { int n = 3 , q = 2 ; int [ ] arr = { 3 , 5 , 7 } ; int [ , ] queries = { { 1 , 2 } , { 2 , 1 } } ; Or_of_Ands_for_each_query ( arr , n , queries , q ) ; } }


"Bitwise OR of N binary strings | C # implementation of the approach ; Function to return the bitwise OR of all the binary strings ; Get max size and reverse each string Since we have to perform OR operation on bits from right to left Reversing the string will make it easier to perform operation from left to right ; Add 0 s to the end of strings if needed ; Perform OR operation on each bit ; Reverse the resultant string to get the final string ; Return the final string ; Utility reverse ; Driver code"
Solution in C#:

using System ; class GFG { static String strBitwiseOR ( String [ ] arr , int n ) { String res = " " ; int max_size = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { max_size = Math . Max ( max_size , ( int ) arr [ i ] . Length ) ; arr [ i ] = reverse ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { String s = " " ; for ( int j = 0 ; j < max_size - arr [ i ] . Length ; j ++ ) s += '0' ; arr [ i ] = arr [ i ] + s ; } for ( int i = 0 ; i < max_size ; i ++ ) { int curr_bit = 0 ; for ( int j = 0 ; j < n ; j ++ ) curr_bit = curr_bit | ( arr [ j ] [ i ] - '0' ) ; res += ( char ) ( curr_bit + '0' ) ; } res = reverse ( res ) ; return res ; } static String reverse ( String input ) { char [ ] temparray = input . ToCharArray ( ) ; int left , right = 0 ; right = temparray . Length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { public static void Main ( String [ ] args ) { String [ ] arr = { "10" , "11" , "1000001" } ; int n = arr . Length ; Console . WriteLine ( strBitwiseOR ( arr , n ) ) ; } }


"Bitwise OR of all unordered pairs from a given array | C # program to implement the above approach ; Function to find the bitwise OR of all possible pairs of the array ; Stores bitwise OR of all possible pairs of [ ] arr ; Traverse the array [ ] arr ; Update totalOR ; Return bitwise OR of all possible pairs of [ ] arr ; Driver Code"
Solution in C#:

using System ; class GFG { static int TotalBitwiseORPair ( int [ ] arr , int N ) { int totalOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { totalOR |= arr [ i ] ; } return totalOR ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 5 , 12 , 15 } ; int N = arr . Length ; Console . Write ( TotalBitwiseORPair ( arr , N ) ) ; } }


"Bitwise Operations on Digits of a Number | C # implementation of the approach ; Function to find the digits ; Function to Find OR of all digits of a number ; Find OR of all digits ; return OR of digits ; Function to Find AND of all digits of a number ; Find AND of all digits ; return AND of digits ; Function to Find XOR of all digits of a number ; Find XOR of all digits ; return XOR of digits ; Driver code ; Find and store all digits ; Find XOR of digits ; Find OR of digits ; Find AND of digits ; Driver code"
Solution in C#:

 using System ; class GFG { static int [ ] digit = new int [ 100000 ] ; static int findDigits ( int n ) { int count = 0 ; while ( n != 0 ) { digit [ count ] = n % 10 ; n = n / 10 ; ++ count ; } return count ; } static int OR_of_Digits ( int n , int count ) { int ans = 0 ; for ( int i = 0 ; i < count ; i ++ ) { ans = ans | digit [ i ] ; } return ans ; } static int AND_of_Digits ( int n , int count ) { int ans = 0 ; for ( int i = 0 ; i < count ; i ++ ) { ans = ans & digit [ i ] ; } return ans ; } static int XOR_of_Digits ( int n , int count ) { int ans = 0 ; for ( int i = 0 ; i < count ; i ++ ) { ans = ans ^ digit [ i ] ; } return ans ; } static void bitwise_operation ( int N ) { int countOfDigit = findDigits ( N ) ; Console . Write ( " XOR ▁ = ▁ " + XOR_of_Digits ( N , countOfDigit ) + "
 " ) ; Console . Write ( " OR ▁ = ▁ " + OR_of_Digits ( N , countOfDigit ) + "
 " ) ; Console . Write ( " AND ▁ = ▁ " + AND_of_Digits ( N , countOfDigit ) + "
 " ) ; } public static void Main ( String [ ] args ) { int N = 123456 ; bitwise_operation ( N ) ; } }


"Bitwise XOR of Bitwise AND of all pairs from two given arrays | C # program for the above approach ; Function to find the Bitwise XOR of Bitwise AND of all pairs from the arrays arr1 [ ] and arr2 [ ] ; Stores XOR of array arr1 [ ] ; Stores XOR of array arr2 [ ] ; Traverse the array arr1 [ ] ; Traverse the array arr2 [ ] ; Return the result ; Driver Code ; Input"
Solution in C#:

using System ; class GFG { static int findXORS ( int [ ] arr1 , int [ ] arr2 , int N , int M ) { int XORS1 = 0 ; int XORS2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { XORS1 ^= arr1 [ i ] ; } for ( int i = 0 ; i < M ; i ++ ) { XORS2 ^= arr2 [ i ] ; } return ( XORS1 & XORS2 ) ; } public static void Main ( String [ ] args ) { int [ ] arr1 = { 1 , 2 , 3 } ; int [ ] arr2 = { 6 , 5 } ; int N = arr1 . Length ; int M = arr2 . Length ; Console . WriteLine ( findXORS ( arr1 , arr2 , N , M ) ) ; } }


"Bitwise XOR of Bitwise AND of all pairs from two given arrays | C # program for the above approach ; Function to find the Bitwise XOR of Bitwise AND of all pairs from the arrays arr1 [ ] and arr2 [ ] ; Stores the result ; Iterate over the range [ 0 , N - 1 ] ; Iterate over the range [ 0 , M - 1 ] ; Stores Bitwise AND of the pair { arr1 [ i ] , arr2 [ j ] } ; Update res ; Return the res ; Driver Code ; Input"
Solution in C#:

using System ; class GFG { static int findXORS ( int [ ] arr1 , int [ ] arr2 , int N , int M ) { int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { int temp = arr1 [ i ] & arr2 [ j ] ; res ^= temp ; } } return res ; } public static void Main ( ) { int [ ] arr1 = { 1 , 2 , 3 } ; int [ ] arr2 = { 6 , 5 } ; int N = arr1 . Length ; int M = arr2 . Length ; Console . Write ( findXORS ( arr1 , arr2 , N , M ) ) ; } }


"Bitwise XOR of a Binary array | C # implementation of the approach ; Function to return the reverse string ; Function to return the bitwise XOR of all the binary strings ; Get max size and reverse each string Since we have to perform XOR operation on bits from right to left Reversing the string will make it easier to perform operation from left to right ; Add 0 s to the end of strings if needed ; Perform XOR operation on each bit ; Reverse the resultant string to get the final string ; Return the final string ; Driver code"
Solution in C#:

using System ; class GFG { static string reverse ( string str ) { string rev = " " ; for ( int i = str . Length - 1 ; i >= 0 ; i -- ) rev = rev + str [ i ] ; return rev ; } static string strBitwiseXOR ( string [ ] arr , int n ) { string result = " " ; int max_len = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { max_len = Math . Max ( max_len , ( int ) arr [ i ] . Length ) ; arr [ i ] = reverse ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { string s = " " ; for ( int j = 0 ; j < max_len - arr [ i ] . Length ; j ++ ) s += '0' ; arr [ i ] = arr [ i ] + s ; } for ( int i = 0 ; i < max_len ; i ++ ) { int pres_bit = 0 ; for ( int j = 0 ; j < n ; j ++ ) pres_bit = pres_bit ^ ( arr [ j ] [ i ] - '0' ) ; result += ( char ) ( pres_bit + '0' ) ; } result = reverse ( result ) ; return result ; } public static void Main ( ) { string [ ] arr = { "1000" , "10001" , "0011" } ; int n = arr . Length ; Console . Write ( strBitwiseXOR ( arr , n ) ) ; } }


"Bitwise XOR of all odd numbers from a given range | C # program for the above approach ; Function to calculate Bitwise XOR of odd numbers in the range [ 1 , N ] ; N & 3 is equivalent to n % 4 ; If n is multiple of 4 ; If n % 4 gives remainder 1 ; If n % 4 gives remainder 2 ; If n % 4 gives remainder 3 ; Function to find the XOR of odd numbers less than or equal to N ; If number is even ; Print the answer ; If number is odd ; Print the answer ; Driver Code ; Function Call"
Solution in C#:

using System ; public class GFG { static int findXOR ( int n ) { switch ( n & 3 ) { case 0 : return n ; case 1 : return 1 ; case 2 : return n + 1 ; } return 0 ; } static void findOddXOR ( int n ) { if ( n % 2 == 0 ) Console . Write ( ( ( findXOR ( n ) ) ^ ( 2 * findXOR ( n / 2 ) ) ) ) ; else Console . Write ( ( ( findXOR ( n ) ) ^ ( 2 * findXOR ( ( n - 1 ) / 2 ) ) ) ) ; } public static void Main ( String [ ] args ) { int N = 11 ; findOddXOR ( N ) ; } }


"Bitwise XOR of all unordered pairs from a given array | C # program to implement the above approach ; Function to get bitwise XOR of all possible pairs of the given array ; Stores bitwise XOR of all possible pairs ; Generate all possible pairs and calculate bitwise XOR of all possible pairs ; Calculate bitwise XOR of each pair ; Driver code"
Solution in C#:

using System ; class GFG { public static int TotalXorPair ( int [ ] arr , int N ) { int totalXOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { totalXOR ^= arr [ i ] ^ arr [ j ] ; } } return totalXOR ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; Console . Write ( TotalXorPair ( arr , N ) ) ; } }


"Bitwise and ( or & ) of a range | An efficient C # program to find bit - wise & of all numbers from x to y . ; Find position of MSB in n . For example if n = 17 , then position of MSB is 4. If n = 7 , value of MSB is 3 ; Function to find Bit - wise & of all numbers from x to y . ; Initialize result ; Find positions of MSB in x and y ; If positions are not same , return ; Add 2 ^ msb_p1 to result ; subtract 2 ^ msb_p1 from x and y . ; Driver code"
Solution in C#:

using System ; class GFG { static int msbPos ( long n ) { int msb_p = - 1 ; while ( n > 0 ) { n = n >> 1 ; msb_p ++ ; } return msb_p ; } static long andOperator ( long x , long y ) { long res = 0 ; while ( x > 0 && y > 0 ) { int msb_p1 = msbPos ( x ) ; int msb_p2 = msbPos ( y ) ; if ( msb_p1 != msb_p2 ) break ; long msb_val = ( 1 << msb_p1 ) ; res = res + msb_val ; x = x - msb_val ; y = y - msb_val ; } return res ; } public static void Main ( ) { long x = 10 , y = 15 ; Console . WriteLine ( andOperator ( x , y ) ) ; } }


"Bitwise operations on Subarrays of size K | C # program for maximum values of each bitwise OR operation on element of subarray of size K ; Function to convert bit array to decimal number ; Function to find maximum values of each bitwise OR operation on element of subarray of size K ; Maintain an integer array bit [ ] of size 32 all initialized to 0 ; Create a sliding window of size k ; Function call ; Perform operation for removed element ; Perform operation for added_element ; Taking maximum value ; Return the result ; Driver Code ; Given array [ ] arr ; Given subarray size K ; Function Call"
Solution in C#:

using System ; class GFG { static int build_num ( int [ ] bit ) { int ans = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) if ( bit [ i ] > 0 ) ans += ( 1 << i ) ; return ans ; } static int maximumOR ( int [ ] arr , int n , int k ) { int [ ] bit = new int [ 32 ] ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) bit [ j ] ++ ; } } int max_or = build_num ( bit ) ; for ( int i = k ; i < n ; i ++ ) { for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i - k ] & ( 1 << j ) ) > 0 ) bit [ j ] -- ; } for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) bit [ j ] ++ ; } max_or = Math . Max ( build_num ( bit ) , max_or ) ; } return max_or ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 5 , 3 , 6 , 11 , 13 } ; int k = 3 ; int n = arr . Length ; Console . Write ( maximumOR ( arr , n , k ) ) ; } }


"Bitwise operations on Subarrays of size K | C # program for minimum values of each bitwise AND operation on elements of subarray of size K ; Function to convert bit array to decimal number ; Function to find minimum values of each bitwise AND operation on element of subarray of size K ; Maintain an integer array bit [ ] of size 32 all initialized to 0 ; Create a sliding window of size k ; Function call ; Perform operation to removed element ; Perform operation to add element ; Taking minimum value ; Return the result ; Driver Code ; Given array [ ] arr ; Given subarray size K ; Function call"
Solution in C#:

using System ; class GFG { static int build_num ( int [ ] bit , int k ) { int ans = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) if ( bit [ i ] == k ) ans += ( 1 << i ) ; return ans ; } static int minimumAND ( int [ ] arr , int n , int k ) { int [ ] bit = new int [ 32 ] ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) bit [ j ] ++ ; } } int min_and = build_num ( bit , k ) ; for ( int i = k ; i < n ; i ++ ) { for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i - k ] & ( 1 << j ) ) > 0 ) bit [ j ] -- ; } for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) bit [ j ] ++ ; } min_and = Math . Min ( build_num ( bit , k ) , min_and ) ; } return min_and ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 5 , 3 , 6 , 11 , 13 } ; int k = 3 ; int n = arr . Length ; Console . Write ( minimumAND ( arr , n , k ) ) ; } }


"Bitwise operations on Subarrays of size K | C # program to find the subarray with minimum XOR ; Function to find the minimum XOR of the subarray of size K ; K must be smaller than or equal to n ; Initialize the beginning index of result ; Compute XOR sum of first subarray of size K ; Initialize minimum XOR sum as current xor ; Traverse from ( k + 1 ) ' th ▁ ▁ element ▁ to ▁ n ' th element ; XOR with current item and first item of previous subarray ; Update result if needed ; Print the minimum XOR ; Driver Code ; Given array [ ] arr ; Given subarray size K ; Function call"
Solution in C#:

using System ; class GFG { static void findMinXORSubarray ( int [ ] arr , int n , int k ) { if ( n < k ) return ; int res_index = 0 ; int curr_xor = 0 ; for ( int i = 0 ; i < k ; i ++ ) curr_xor ^= arr [ i ] ; int min_xor = curr_xor ; for ( int i = k ; i < n ; i ++ ) { curr_xor ^= ( arr [ i ] ^ arr [ i - k ] ) ; if ( curr_xor < min_xor ) { min_xor = curr_xor ; res_index = ( i - k + 1 ) ; } } Console . WriteLine ( min_xor ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 7 , 90 , 20 , 10 , 50 , 40 } ; int k = 3 ; int n = arr . Length ; findMinXORSubarray ( arr , n , k ) ; } }


"Bitwise recursive addition of two integers | C # program to do recursive addition of two integers ; If bitwise & is 0 , then there is not going to be any carry . Hence result of XOR is addition . ; Driver code"
Solution in C#:

using System ; class GFG { static int add ( int x , int y ) { int keep = ( x & y ) << 1 ; int res = x ^ y ; if ( keep == 0 ) return res ; return add ( keep , res ) ; } public static void Main ( ) { Console . Write ( add ( 15 , 38 ) ) ; } }


"Block swap algorithm for array rotation |  ; Wrapper over the recursive function leftRotateRec ( ) It left rotates [ ] arr by d . ; Return If number of elements to be rotated is zero or equal to array size ; If number of elements to be rotated is exactly half of array size ; If A is shorter ; If B is shorter ; Function to print an array ; This function swaps d elements starting at index fi with d elements starting at index si ; Driver Code"
Solution in C#:

using System ; class GFG { public static void leftRotate ( int [ ] arr , int d , int n ) { leftRotateRec ( arr , 0 , d , n ) ; } public static void leftRotateRec ( int [ ] arr , int i , int d , int n ) { if ( d == 0 d == n ) return ; if ( n - d == d ) { swap ( arr , i , n - d + i , d ) ; return ; } if ( d < n - d ) { swap ( arr , i , n - d + i , d ) ; leftRotateRec ( arr , i , d , n - d ) ; } else { swap ( arr , i , d , n - d ) ; leftRotateRec ( arr , n - d + i , 2 * d - n , d ) ; } } public static void printArray ( int [ ] arr , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } public static void swap ( int [ ] arr , int fi , int si , int d ) { int i , temp ; for ( i = 0 ; i < d ; i ++ ) { temp = arr [ fi + i ] ; arr [ fi + i ] = arr [ si + i ] ; arr [ si + i ] = temp ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; leftRotate ( arr , 2 , 7 ) ; printArray ( arr , 7 ) ; } }


"Block swap algorithm for array rotation | C # code for above implementation ; A is shorter ; B is shorter ; Finally , block swap A and B"
Solution in C#:

static void leftRotate ( int [ ] arr , int d , int n ) { int i , j ; if ( d == 0 d == n ) return ; i = d ; j = n - d ; while ( i != j ) { if ( i < j ) { swap ( arr , d - i , d + j - i , i ) ; j -= i ; } else { swap ( arr , d - i , d , j ) ; i -= j ; } } swap ( arr , d - i , d , i ) ; }


"Blum Integer | C # implementation to check If a number is a Blum integer ; Function to cheek if number is Blum Integer ; to store prime numbers from 2 to n ; If prime [ i ] is not changed , then it is a prime ; Update all multiples of p ; to check if the given odd integer is Blum Integer or not ; checking the factors are of 4 t + 3 form or not ; Driver code ; give odd integer greater than 20"
Solution in C#:

using System ; class GFG { public static bool isBlumInteger ( int n ) { bool [ ] prime = new bool [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) prime [ i ] = true ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( prime [ i ] == true ) { for ( int j = i * 2 ; j <= n ; j += i ) prime [ j ] = false ; } } for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] ) { if ( ( n % i == 0 ) && ( ( i - 3 ) % 4 ) == 0 ) { int q = n / i ; return ( q != i && prime [ q ] && ( q - 3 ) % 4 == 0 ) ; } } } return false ; } static public void Main ( ) { int n = 249 ; if ( isBlumInteger ( n ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Boggle ( Find all possible words in a board of characters ) | Set 1 | C # program for Boggle game ; Let the given dictionary be following ; A given function to check if a given string is present in dictionary . The implementation is naive for simplicity . As per the question dictionary is given to us . ; Linearly search all words ; A recursive function to print all words present on boggle ; Mark current cell as visited and append current character to str ; If str is present in dictionary , then print it ; Traverse 8 adjacent cells of boggle [ i , j ] ; Erase current character from string and mark visited of current cell as false ; Prints all words present in dictionary . ; Mark all characters as not visited ; Initialize current string ; Consider every character and look for all words starting with this character ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static readonly String [ ] dictionary = { " GEEKS " , " FOR " , " QUIZ " , " GUQ " , " EE " } ; static readonly int n = dictionary . Length ; static readonly int M = 3 , N = 3 ; static bool isWord ( String str ) { for ( int i = 0 ; i < n ; i ++ ) if ( str . Equals ( dictionary [ i ] ) ) return true ; return false ; } static void findWordsUtil ( char [ , ] boggle , bool [ , ] visited , int i , int j , String str ) { visited [ i , j ] = true ; str = str + boggle [ i , j ] ; if ( isWord ( str ) ) Console . WriteLine ( str ) ; for ( int row = i - 1 ; row <= i + 1 && row < M ; row ++ ) for ( int col = j - 1 ; col <= j + 1 && col < N ; col ++ ) if ( row >= 0 && col >= 0 && ! visited [ row , col ] ) findWordsUtil ( boggle , visited , row , col , str ) ; str = " " + str [ str . Length - 1 ] ; visited [ i , j ] = false ; } static void findWords ( char [ , ] boggle ) { bool [ , ] visited = new bool [ M , N ] ; String str = " " ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) findWordsUtil ( boggle , visited , i , j , str ) ; } public static void Main ( String [ ] args ) { char [ , ] boggle = { { ' G ' , ' I ' , ' Z ' } , { ' U ' , ' E ' , ' K ' } , { ' Q ' , ' S ' , ' E ' } } ; Console . WriteLine ( " Following ▁ words ▁ of ▁ " + " dictionary ▁ are ▁ present " ) ; findWords ( boggle ) ; } }


"Boolean Parenthesization Problem | DP |  ; Count number of True in left Partition ; Count number of False in left Partition ; Count number of True in right Partition ; Count number of False in right Partition ; Evaluate AND operation ; Evaluate OR operation ; Evaluate XOR operation ; Driver code ; We obtain the string T | T & F ^ T ; There are 4 ways ( ( T T ) & ( F ^ T ) ) , ( T | ( T & ( F ^ T ) ) ) , ( ( ( T T ) & F ) ^ T ) and ( T | ( ( T & F ) ^ T ) )"
Solution in C#:

using System ; class GFG { static int parenthesis_count ( string str , int i , int j , int isTrue , int [ , , ] dp ) { if ( i > j ) return 0 ; if ( i == j ) { if ( isTrue == 1 ) { return ( str [ i ] == ' T ' ) ? 1 : 0 ; } else { return ( str [ i ] == ' F ' ) ? 1 : 0 ; } } if ( dp [ i , j , isTrue ] != - 1 ) return dp [ i , j , isTrue ] ; int temp_ans = 0 ; int leftTrue , rightTrue , leftFalse , rightFalse ; for ( int k = i + 1 ; k <= j - 1 ; k = k + 2 ) { if ( dp [ i , k - 1 , 1 ] != - 1 ) leftTrue = dp [ i , k - 1 , 1 ] ; else { leftTrue = parenthesis_count ( str , i , k - 1 , 1 , dp ) ; } if ( dp [ i , k - 1 , 0 ] != - 1 ) leftFalse = dp [ i , k - 1 , 0 ] ; else { leftFalse = parenthesis_count ( str , i , k - 1 , 0 , dp ) ; } if ( dp [ k + 1 , j , 1 ] != - 1 ) rightTrue = dp [ k + 1 , j , 1 ] ; else { rightTrue = parenthesis_count ( str , k + 1 , j , 1 , dp ) ; } if ( dp [ k + 1 , j , 0 ] != - 1 ) rightFalse = dp [ k + 1 , j , 0 ] ; else { rightFalse = parenthesis_count ( str , k + 1 , j , 0 , dp ) ; } if ( str [ k ] == ' & ' ) { if ( isTrue == 1 ) { temp_ans = temp_ans + leftTrue * rightTrue ; } else { temp_ans = temp_ans + leftTrue * rightFalse + leftFalse * rightTrue + leftFalse * rightFalse ; } } else if ( str [ k ] == ' ▁ ' ) { if ( isTrue == 1 ) { temp_ans = temp_ans + leftTrue * rightTrue + leftTrue * rightFalse + leftFalse * rightTrue ; } else { temp_ans = temp_ans + leftFalse * rightFalse ; } } else if ( str [ k ] == ' ^ ' ) { if ( isTrue == 1 ) { temp_ans = temp_ans + leftTrue * rightFalse + leftFalse * rightTrue ; } else { temp_ans = temp_ans + leftTrue * rightTrue + leftFalse * rightFalse ; } } dp [ i , j , isTrue ] = temp_ans ; } return temp_ans ; } static int countWays ( int N , string S ) { int [ , , ] dp = new int [ N + 1 , N + 1 , 2 ] ; for ( int i = 0 ; i < ( N + 1 ) ; i ++ ) { for ( int j = 0 ; j < ( N + 1 ) ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) { dp [ i , j , k ] = - 1 ; } } } return parenthesis_count ( S , 0 , N - 1 , 1 , dp ) ; } static void Main ( ) { string symbols = " TTFT " ;...


"Boolean Parenthesization Problem | DP |  ; CountWays function ; Base Condition ; Count number of True in left Partition ; Count number of False in left Partition ; Count number of True in right Partition ; Count number of False in right Partition ; Evaluate AND operation ; Evaluate OR operation ; Evaluate XOR operation ; Driver code ; We obtain the string T | T & F ^ T ; There are 4 ways ( ( T T ) & ( F ^ T ) ) , ( T | ( T & ( F ^ T ) ) ) , ( ( ( T T ) & F ) ^ T ) and ( T | ( ( T & F ) ^ T ) )"
Solution in C#:

using System ; class GFG { static int countWays ( int N , string S ) { int [ , , ] dp = new int [ N + 1 , N + 1 , 2 ] ; for ( int i = 0 ; i < ( N + 1 ) ; i ++ ) { for ( int j = 0 ; j < ( N + 1 ) ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) { dp [ i , j , k ] = - 1 ; } } } static int parenthesis_count ( string str , int i , int j , int isTrue , int [ , , ] dp ) { if ( i > j ) return 0 ; if ( i == j ) { if ( isTrue == 1 ) { return ( str [ i ] == ' T ' ) ? 1 : 0 ; } else { return ( str [ i ] == ' F ' ) ? 1 : 0 ; } } if ( dp [ i , j , isTrue ] != - 1 ) return dp [ i , j , isTrue ] ; int temp_ans = 0 ; int leftTrue , rightTrue , leftFalse , rightFalse ; for ( int k = i + 1 ; k <= j - 1 ; k = k + 2 ) { if ( dp [ i , k - 1 , 1 ] != - 1 ) leftTrue = dp [ i , k - 1 , 1 ] ; else { leftTrue = parenthesis_count ( str , i , k - 1 , 1 , dp ) ; } if ( dp [ i , k - 1 , 0 ] != - 1 ) leftFalse = dp [ i , k - 1 , 0 ] ; else { leftFalse = parenthesis_count ( str , i , k - 1 , 0 , dp ) ; } if ( dp [ k + 1 , j , 1 ] != - 1 ) rightTrue = dp [ k + 1 , j , 1 ] ; else { rightTrue = parenthesis_count ( str , k + 1 , j , 1 , dp ) ; } if ( dp [ k + 1 , j , 0 ] != - 1 ) rightFalse = dp [ k + 1 , j , 0 ] ; else { rightFalse = parenthesis_count ( str , k + 1 , j , 0 , dp ) ; } if ( str [ k ] == ' & ' ) { if ( isTrue == 1 ) { temp_ans = temp_ans + leftTrue * rightTrue ; } else { temp_ans = temp_ans + leftTrue * rightFalse + leftFalse * rightTrue + leftFalse * rightFalse ; } } else if ( str [ k ] == ' ▁ ' ) { if ( isTrue == 1 ) { temp_ans = temp_ans + leftTrue * rightTrue + leftTrue * rightFalse + leftFalse * rightTrue ; } else { temp_ans = temp_ans + leftFalse * rightFalse ; } } else if ( str [ k ] == ' ^ ' ) { if ( isTrue == 1 ) { temp_ans = temp_ans + leftTrue * rightFalse + leftFalse * rightTrue ; } else { temp_ans = temp_ans + leftTrue * rightTrue + leftFalse * rightFalse ; } } dp [ i , j , isTrue ] = temp_ans ; } return temp_ans ; } return parenthesis_count ( S , 0 , N - 1 , 1 , dp ) ; } static void Main ( ) { string symbols = " TTFT " ;...


"Boolean Parenthesization Problem | DP | C # program of above approach ; Returns count of all possible parenthesizations that lead to result true for a boolean expression with symbols like true and false and operators like & , | and ^ filled between symbols ; Fill diaginal entries first All diagonal entries in T [ i , i ] are 1 if symbol [ i ] is T ( true ) . Similarly , all F [ i , i ] entries are 1 if symbol [ i ] is F ( False ) ; Now fill T [ i , i + 1 ] , T [ i , i + 2 ] , T [ i , i + 3 ] ... in order And F [ i , i + 1 ] , F [ i , i + 2 ] , F [ i , i + 3 ] ... in order ; Find place of parenthesization using current value of gap ; Store Total [ i , k ] and Total [ k + 1 , j ] ; Follow the recursive formulas according to the current operator ; Driver code ; There are 4 ways ( ( T T ) & ( F ^ T ) ) , ( T | ( T & ( F ^ T ) ) ) , ( ( ( T T ) & F ) ^ T ) and ( T | ( ( T & F ) ^ T ) )"
Solution in C#:

using System ; class GFG { static int countParenth ( char [ ] symb , char [ ] oper , int n ) { int [ , ] F = new int [ n , n ] ; int [ , ] T = new int [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) { F [ i , i ] = ( symb [ i ] == ' F ' ) ? 1 : 0 ; T [ i , i ] = ( symb [ i ] == ' T ' ) ? 1 : 0 ; } for ( int gap = 1 ; gap < n ; ++ gap ) { for ( int i = 0 , j = gap ; j < n ; ++ i , ++ j ) { T [ i , j ] = F [ i , j ] = 0 ; for ( int g = 0 ; g < gap ; g ++ ) { int k = i + g ; int tik = T [ i , k ] + F [ i , k ] ; int tkj = T [ k + 1 , j ] + F [ k + 1 , j ] ; if ( oper [ k ] == ' & ' ) { T [ i , j ] += T [ i , k ] * T [ k + 1 , j ] ; F [ i , j ] += ( tik * tkj - T [ i , k ] * T [ k + 1 , j ] ) ; } if ( oper [ k ] == ' ▁ ' ) { F [ i , j ] += F [ i , k ] * F [ k + 1 , j ] ; T [ i , j ] += ( tik * tkj - F [ i , k ] * F [ k + 1 , j ] ) ; } if ( oper [ k ] == ' ^ ' ) { T [ i , j ] += F [ i , k ] * T [ k + 1 , j ] + T [ i , k ] * F [ k + 1 , j ] ; F [ i , j ] += T [ i , k ] * T [ k + 1 , j ] + F [ i , k ] * F [ k + 1 , j ] ; } } } } return T [ 0 , n - 1 ] ; } public static void Main ( ) { char [ ] symbols = " TTFT " . ToCharArray ( ) ; char [ ] operators = " | & ^ " . ToCharArray ( ) ; int n = symbols . Length ; Console . WriteLine ( countParenth ( symbols , operators , n ) ) ; } }


"Booth ’ s Multiplication Algorithm | C # code to implement booth 's algorithm ; function to perform adding in the accumulator ; updating accumulator with A = A + BR ; function to find the number 's complement ; function to perform right shift ; function to display operations ; accumulator content ; multiplier content ; Function to implement booth 's algo ; SECOND CONDITION ; subtract BR from accumulator ; THIRD CONDITION ; add BR to accumulator ; FIRST CONDITION ; decrement counter ; Driver code ; Number of multiplicand bit ; multiplicand ; copy multiplier to temp array mt [ ] ; No . of multiplier bit ; sequence counter ; multiplier"
Solution in C#:

 using System ; class GFG { static void add ( int [ ] ac , int [ ] x , int qrn ) { int i , c = 0 ; for ( i = 0 ; i < qrn ; i ++ ) { ac [ i ] = ac [ i ] + x [ i ] + c ; if ( ac [ i ] > 1 ) { ac [ i ] = ac [ i ] % 2 ; c = 1 ; } else c = 0 ; } } static void complement ( int [ ] a , int n ) { int i ; int [ ] x = new int [ 8 ] ; Array . Clear ( x , 0 , 8 ) ; x [ 0 ] = 1 ; for ( i = 0 ; i < n ; i ++ ) { a [ i ] = ( a [ i ] + 1 ) % 2 ; } add ( a , x , n ) ; } static void rightShift ( int [ ] ac , int [ ] qr , ref int qn , int qrn ) { int temp , i ; temp = ac [ 0 ] ; qn = qr [ 0 ] ; Console . Write ( " 	 	 rightShift 	 " ) ; for ( i = 0 ; i < qrn - 1 ; i ++ ) { ac [ i ] = ac [ i + 1 ] ; qr [ i ] = qr [ i + 1 ] ; } qr [ qrn - 1 ] = temp ; } static void display ( int [ ] ac , int [ ] qr , int qrn ) { int i ; for ( i = qrn - 1 ; i >= 0 ; i -- ) Console . Write ( ac [ i ] ) ; Console . Write ( " 	 " ) ; for ( i = qrn - 1 ; i >= 0 ; i -- ) Console . Write ( qr [ i ] ) ; } static void boothAlgorithm ( int [ ] br , int [ ] qr , int [ ] mt , int qrn , int sc ) { int qn = 0 ; int [ ] ac = new int [ 10 ] ; Array . Clear ( ac , 0 , 10 ) ; int temp = 0 ; Console . Write ( " qn 	 q [ n ▁ + ▁ 1 ] 	 BR 	 " + " 	 AC 	 QR 	 	 sc
 " ) ; Console . Write ( " 	 	 	 initial 	 	 " ) ; display ( ac , qr , qrn ) ; Console . Write ( " 	 	 " + sc + "
 " ) ; while ( sc != 0 ) { Console . Write ( qr [ 0 ] + " 	 " + qn ) ; if ( ( qn + qr [ 0 ] ) == 1 ) { if ( temp == 0 ) { add ( ac , mt , qrn ) ; Console . Write ( " 	 	 A ▁ = ▁ A ▁ - ▁ BR 	 " ) ; for ( int i = qrn - 1 ; i >= 0 ; i -- ) Console . Write ( ac [ i ] ) ; temp = 1 ; } else if ( temp == 1 ) { add ( ac , br , qrn ) ; Console . Write ( " 	 	 A ▁ = ▁ A ▁ + ▁ BR 	 " ) ; for ( int i = qrn - 1 ; i >= 0 ; i -- ) Console . Write ( ac [ i ] ) ; temp = 0 ; } Console . Write ( "
 	 " ) ; rightShift ( ac , qr , ref qn , qrn ) ; } else if ( qn - qr [ 0 ] == 0 ) rightShift ( ac , qr , ref qn , qrn ) ; display ( ac , qr , qrn ) ; Console . Write ( " 	 " ) ; sc -- ; Console . Write ( " 	 " + sc + "
...


"Bottom View of a Binary Tree | C # program to print Bottom View of Binary Tree ; Tree node class ; Data of the node ; Horizontal distance of the node ; left and right references ; Constructor of tree node ; Tree class ; Root node of tree ; Default constructor ; Parameterized tree constructor ; Method that prints the bottom view . ; Initialize a variable ' hd ' with 0 for the root element . ; TreeMap which stores key value pair sorted on key value ; Queue to store tree nodes in level order traversal ; Assign initialized horizontal distance value to root node and add it to the queue . ; In STL , push ( ) is used enqueue an item ; Loop until the queue is empty ( standard level order loop ) ; Extract the horizontal distance value from the dequeued tree node . ; Put the dequeued tree node to TreeMap having key as horizontal distance . Every time we find a node having same horizontal distance we need to replace the data in the map . ; If the dequeued node has a left child add it to the queue with a horizontal distance hd - 1. ; If the dequeued node has a right child add it to the queue with a horizontal distance hd + 1. ; Traverse the map elements using the iterator . ; Driver code"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class Node { public int data ; public int hd ; public Node left , right ; public Node ( int key ) { data = key ; hd = 1000000 ; left = right = null ; } } class Tree { Node root ; public Tree ( ) { } public Tree ( Node node ) { root = node ; } public void bottomView ( ) { if ( root == null ) return ; int hd = 0 ; SortedDictionary < int , int > map = new SortedDictionary < int , int > ( ) ; Queue queue = new Queue ( ) ; root . hd = hd ; queue . Enqueue ( root ) ; while ( queue . Count != 0 ) { Node temp = ( Node ) queue . Dequeue ( ) ; hd = temp . hd ; map [ hd ] = temp . data ; if ( temp . left != null ) { temp . left . hd = hd - 1 ; queue . Enqueue ( temp . left ) ; } if ( temp . right != null ) { temp . right . hd = hd + 1 ; queue . Enqueue ( temp . right ) ; } } foreach ( int i in map . Values ) { Console . Write ( i + " ▁ " ) ; } } } public class BottomView { public static void Main ( string [ ] args ) { Node root = new Node ( 20 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 22 ) ; root . left . left = new Node ( 5 ) ; root . left . right = new Node ( 3 ) ; root . right . left = new Node ( 4 ) ; root . right . right = new Node ( 25 ) ; root . left . right . left = new Node ( 10 ) ; root . left . right . right = new Node ( 14 ) ; Tree tree = new Tree ( root ) ; Console . WriteLine ( " Bottom ▁ view ▁ of ▁ the ▁ " + " given ▁ binary ▁ tree : " ) ; tree . bottomView ( ) ; } }


"Boundary Traversal of binary tree | C # program to print boundary traversal of binary tree ; A binary tree node has data , pointer to left child and a pointer to right child ; A simple function to print leaf nodes of a binary tree ; Print it if it is a leaf node ; A function to print all left boundary nodes , except a leaf node . Print the nodes in TOP DOWN manner ; to ensure top down order , print the node before calling itself for left subtree ; do nothing if it is a leaf node , this way we avoid duplicates in output ; A function to print all right boundary nodes , except a leaf node . Print the nodes in BOTTOM UP manner ; to ensure bottom up order , first call for right subtree , then print this node ; do nothing if it is a leaf node , this way we avoid duplicates in output ; A function to do boundary traversal of a given binary tree ; Print the left boundary in top - down manner . ; Print all leaf nodes ; Print the right boundary in bottom - up manner ; Driver Code"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class GFG { public Node root ; public virtual void printLeaves ( Node node ) { if ( node == null ) return ; printLeaves ( node . left ) ; if ( node . left == null && node . right == null ) { Console . Write ( node . data + " ▁ " ) ; } printLeaves ( node . right ) ; } public virtual void printBoundaryLeft ( Node node ) { if ( node == null ) return ; if ( node . left != null ) { Console . Write ( node . data + " ▁ " ) ; printBoundaryLeft ( node . left ) ; } else if ( node . right != null ) { Console . Write ( node . data + " ▁ " ) ; printBoundaryLeft ( node . right ) ; } } public virtual void printBoundaryRight ( Node node ) { if ( node == null ) return ; if ( node . right != null ) { printBoundaryRight ( node . right ) ; Console . Write ( node . data + " ▁ " ) ; } else if ( node . left != null ) { printBoundaryRight ( node . left ) ; Console . Write ( node . data + " ▁ " ) ; } } public virtual void printBoundary ( Node node ) { if ( node == null ) return ; Console . Write ( node . data + " ▁ " ) ; printBoundaryLeft ( node . left ) ; printLeaves ( node . left ) ; printLeaves ( node . right ) ; printBoundaryRight ( node . right ) ; } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 20 ) ; tree . root . left = new Node ( 8 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . left . right . left = new Node ( 10 ) ; tree . root . left . right . right = new Node ( 14 ) ; tree . root . right = new Node ( 22 ) ; tree . root . right . right = new Node ( 25 ) ; tree . printBoundary ( tree . root ) ; } }


"Boundary Value Analysis : Nature of Roots of a Quadratic equation | C # program to check the nature of the roots ; BVA for nature of roots of a quadratic equation ; If a = 0 , D / 2 a will yield exception Hence it is not a valid Quadratic Equation ; If D > 0 , it will be Real Roots ; If D == 0 , it will be Equal Roots ; If D < 0 , it will be Imaginary Roots ; Function to check for all testcases ; Driver Code"
Solution in C#:

 using System ; class GFG { static void nature_of_roots ( int a , int b , int c ) { if ( a == 0 ) { Console . Write ( " Not ▁ a ▁ Quadratic ▁ Equation " + "
 " ) ; return ; } int D = b * b - 4 * a * c ; if ( D > 0 ) { Console . Write ( " Real ▁ Roots " + "
 " ) ; } else if ( D == 0 ) { Console . Write ( " Equal ▁ Roots " + "
 " ) ; } else { Console . Write ( " Imaginary ▁ Roots " + "
 " ) ; } } static void checkForAllTestCase ( ) { Console . Write ( " Testcase " + " 	 a 	 b 	 c 	 Actual ▁ Output " + "
 " ) ; Console . WriteLine ( ) ; int a , b , c ; a = b = c = 0 ; int testcase = 1 ; while ( testcase <= 13 ) { if ( testcase == 1 ) { a = 0 ; b = 50 ; c = 50 ; } else if ( testcase == 2 ) { a = 1 ; b = 50 ; c = 50 ; } else if ( testcase == 3 ) { a = 50 ; b = 50 ; c = 50 ; } else if ( testcase == 4 ) { a = 99 ; b = 50 ; c = 50 ; } else if ( testcase == 5 ) { a = 100 ; b = 50 ; c = 50 ; } else if ( testcase == 6 ) { a = 50 ; b = 0 ; c = 50 ; } else if ( testcase == 7 ) { a = 50 ; b = 1 ; c = 50 ; } else if ( testcase == 8 ) { a = 50 ; b = 99 ; c = 50 ; } else if ( testcase == 9 ) { a = 50 ; b = 100 ; c = 50 ; } else if ( testcase == 10 ) { a = 50 ; b = 50 ; c = 0 ; } else if ( testcase == 11 ) { a = 50 ; b = 50 ; c = 1 ; } else if ( testcase == 12 ) { a = 50 ; b = 50 ; c = 99 ; } else if ( testcase == 13 ) { a = 50 ; b = 50 ; c = 100 ; } Console . Write ( " 	 " + testcase + " 	 " + a + " 	 " + b + " 	 " + c + " 	 " ) ; nature_of_roots ( a , b , c ) ; Console . WriteLine ( ) ; testcase ++ ; } } public static void Main ( String [ ] args ) { checkForAllTestCase ( ) ; } }


"Boundary elements of a Matrix | C # Code for Boundary elements of a Matrix ; Driver Code"
Solution in C#:

using System ; class GFG { public static void printBoundary ( int [ , ] a , int m , int n ) { for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == 0 ) Console . Write ( a [ i , j ] + " ▁ " ) ; else if ( i == m - 1 ) Console . Write ( a [ i , j ] + " ▁ " ) ; else if ( j == 0 ) Console . Write ( a [ i , j ] + " ▁ " ) ; else if ( j == n - 1 ) Console . Write ( a [ i , j ] + " ▁ " ) ; else Console . Write ( " ▁ " ) ; } Console . WriteLine ( " ▁ " ) ; } } static public void Main ( ) { int [ , ] a = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } } ; printBoundary ( a , 4 , 4 ) ; } }


"Boundary elements of a Matrix | C # Code for Finding sum of boundary elements ; Driver Code"
Solution in C#:

using System ; class GFG { public static long getBoundarySum ( int [ , ] a , int m , int n ) { long sum = 0 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == 0 ) sum += a [ i , j ] ; else if ( i == m - 1 ) sum += a [ i , j ] ; else if ( j == 0 ) sum += a [ i , j ] ; else if ( j == n - 1 ) sum += a [ i , j ] ; } } return sum ; } static public void Main ( ) { int [ , ] a = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } } ; long sum = getBoundarySum ( a , 4 , 4 ) ; Console . WriteLine ( " Sum ▁ of ▁ boundary " + " ▁ elements ▁ is ▁ " + sum ) ; } }


"Boyer Moore Algorithm for Pattern Searching | C # Program for Bad Character Heuristic of Boyer Moore String Matching Algorithm ; A utility function to get maximum of two integers ; The preprocessing function for Boyer Moore 's  bad character heuristic ; Initialize all occurrences as - 1 ; Fill the actual value of last occurrence of a character ; A pattern searching function that uses Bad Character Heuristic of Boyer Moore Algorithm ; Fill the bad character array by calling the preprocessing function badCharHeuristic ( ) for given pattern ; s is shift of the pattern with respect to text ; Keep reducing index j of pattern while characters of pattern and text are matching at this shift s ; If the pattern is present at current shift , then index j will become - 1 after the above loop ; Shift the pattern so that the next character in text aligns with the last occurrence of it in pattern . The condition s + m < n is necessary for the case when pattern occurs at the end of text ; Shift the pattern so that the bad character in text aligns with the last occurrence of it in pattern . The max function is used to make sure that we get a positive shift . We may get a negative shift if the last occurrence of bad character in pattern is on the right side of the current character . ; Driver program to test above function"
Solution in C#:

using System ; public class AWQ { static int NO_OF_CHARS = 256 ; static int max ( int a , int b ) { return ( a > b ) ? a : b ; } static void badCharHeuristic ( char [ ] str , int size , int [ ] badchar ) { int i ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) badchar [ i ] = - 1 ; for ( i = 0 ; i < size ; i ++ ) badchar [ ( int ) str [ i ] ] = i ; } static void search ( char [ ] txt , char [ ] pat ) { int m = pat . Length ; int n = txt . Length ; int [ ] badchar = new int [ NO_OF_CHARS ] ; badCharHeuristic ( pat , m , badchar ) ; int s = 0 ; while ( s <= ( n - m ) ) { int j = m - 1 ; while ( j >= 0 && pat [ j ] == txt [ s + j ] ) j -- ; if ( j < 0 ) { Console . WriteLine ( " Patterns ▁ occur ▁ at ▁ shift ▁ = ▁ " + s ) ; s += ( s + m < n ) ? m - badchar [ txt [ s + m ] ] : 1 ; } else s += max ( 1 , j - badchar [ txt [ s + j ] ] ) ; } } public static void Main ( ) { char [ ] txt = " ABAAABCD " . ToCharArray ( ) ; char [ ] pat = " ABC " . ToCharArray ( ) ; search ( txt , pat ) ; } }


"Boyer Moore Algorithm for Pattern Searching | C # Program for Bad Character Heuristic of Boyer Moore String Matching Algorithm ; A utility function to get maximum of two integers ; The preprocessing function for Boyer Moore 's  bad character heuristic ; Initialize all occurrences as - 1 ; Fill the actual value of last occurrence of a character ; A pattern searching function that uses Bad Character Heuristic of Boyer Moore Algorithm ; Fill the bad character array by calling the preprocessing function badCharHeuristic ( ) for given pattern ; s is shift of the pattern with respect to text ; there are n - m + 1 potential allignments ; Keep reducing index j of pattern while characters of pattern and text are matching at this shift s ; If the pattern is present at current shift , then index j will become - 1 after the above loop ; Shift the pattern so that the next character in text aligns with the last occurrence of it in pattern . The condition s + m < n is necessary for the case when pattern occurs at the end of text ; Shift the pattern so that the bad character in text aligns with the last occurrence of it in pattern . The max function is used to make sure that we get a positive shift . We may get a negative shift if the last occurrence of bad character in pattern is on the right side of the current character . ; Driver program to test above function"
Solution in C#:


"Boyer Moore Algorithm | Good Suffix heuristic | C # program for Boyer Moore Algorithm with Good Suffix heuristic to find pattern in given text string ; preprocessing for strong good suffix rule ; m is the length of pattern ; if character at position i - 1 is not equivalent to character at j - 1 , then continue searching to right of the pattern for border ; the character preceding the occurrence of t in pattern P is different than the mismatching character in P , we stop skipping the occurrences and shift the pattern from i to j ; Update the position of next border ; p [ i - 1 ] matched with p [ j - 1 ] , border is found . store the beginning position of border ; Preprocessing for case 2 ; set the border position of the first character of the pattern to all indices in array shift having shift [ i ] = 0 ; suffix becomes shorter than bpos [ 0 ] , use the position of next widest border as value of j ; Search for a pattern in given text using Boyer Moore algorithm with Good suffix rule ; s is shift of the pattern with respect to text ; initialize all occurrence of shift to 0 ; do preprocessing ; Keep reducing index j of pattern while characters of pattern and text are matching at this shift s ; If the pattern is present at the current shift , then index j will become - 1 after the above loop ; pat [ i ] != pat [ s + j ] so shift the pattern shift [ j + 1 ] times ; Driver Code"
Solution in C#:

 using System ; class GFG { static void preprocess_strong_suffix ( int [ ] shift , int [ ] bpos , char [ ] pat , int m ) { int i = m , j = m + 1 ; bpos [ i ] = j ; while ( i > 0 ) { while ( j <= m && pat [ i - 1 ] != pat [ j - 1 ] ) { if ( shift [ j ] == 0 ) shift [ j ] = j - i ; j = bpos [ j ] ; } i -- ; j -- ; bpos [ i ] = j ; } } static void preprocess_case2 ( int [ ] shift , int [ ] bpos , char [ ] pat , int m ) { int i , j ; j = bpos [ 0 ] ; for ( i = 0 ; i <= m ; i ++ ) { if ( shift [ i ] == 0 ) shift [ i ] = j ; if ( i == j ) j = bpos [ j ] ; } } static void search ( char [ ] text , char [ ] pat ) { int s = 0 , j ; int m = pat . Length ; int n = text . Length ; int [ ] bpos = new int [ m + 1 ] ; int [ ] shift = new int [ m + 1 ] ; for ( int i = 0 ; i < m + 1 ; i ++ ) shift [ i ] = 0 ; preprocess_strong_suffix ( shift , bpos , pat , m ) ; preprocess_case2 ( shift , bpos , pat , m ) ; while ( s <= n - m ) { j = m - 1 ; while ( j >= 0 && pat [ j ] == text [ s + j ] ) j -- ; if ( j < 0 ) { Console . Write ( " pattern ▁ occurs ▁ at ▁ shift ▁ = ▁ { 0 }
 " , s ) ; s += shift [ 0 ] ; } else s += shift [ j + 1 ] ; } } public static void Main ( String [ ] args ) { char [ ] text = " ABAAAABAACD " . ToCharArray ( ) ; char [ ] pat = " ABA " . ToCharArray ( ) ; search ( text , pat ) ; } }


"Brahmagupta Fibonacci Identity | C # code to verify Brahmagupta Fibonacci identity ; represent the product as sum of 2 squares ; check identity criteria ; Driver code ; 1 ^ 2 + 2 ^ 2 ; 3 ^ 2 + 4 ^ 2 ; express product of sum of 2 squares as sum of ( sum of 2 squares )"
Solution in C#:

 using System ; class GFG { static void find_sum_of_two_squares ( int a , int b ) { int ab = a * b ; for ( int i = 0 ; i * i <= ab ; i ++ ) { for ( int j = i ; i * i + j * j <= ab ; j ++ ) { if ( i * i + j * j == ab ) Console . Write ( i + " ^ 2 ▁ + ▁ " + j + " ^ 2 ▁ = ▁ " + ab + "
 " ) ; } } } public static void Main ( ) { int a = 1 * 1 + 2 * 2 ; int b = 3 * 3 + 4 * 4 ; Console . Write ( " Representation ▁ of ▁ a ▁ * ▁ b ▁ " + " as ▁ sum ▁ of ▁ 2 ▁ squares :
 " ) ; find_sum_of_two_squares ( a , b ) ; } }


"Break a number such that sum of maximum divisors of all parts is minimum | C # Code for Break a number such that sum of maximum divisors of all parts is minimum ; Function to check if a number is prime or not . ; If n is an even number ( we can write it as sum of two primes ) ; If n is odd and n - 2 is prime . ; If n is odd , n - 3 must be even . ; Driver program"
Solution in C#:

using System ; class GFG { static bool isPrime ( int n ) { int i = 2 ; while ( i * i <= n ) { if ( n % i == 0 ) return false ; i ++ ; } return true ; } static int minimumSum ( int n ) { if ( isPrime ( n ) ) return 1 ; if ( n % 2 == 0 ) return 2 ; if ( isPrime ( n - 2 ) ) return 2 ; return 3 ; } public static void Main ( ) { int n = 27 ; Console . WriteLine ( minimumSum ( n ) ) ; } }


"Break the number into three parts | C # program to count number of ways to break a number in three parts ; Function to count number of ways to make the given number n ; driver program"
Solution in C#:

using System ; class GFG { static long count_of_ways ( long n ) { long count = 0 ; count = ( n + 1 ) * ( n + 2 ) / 2 ; return count ; } public static void Main ( ) { long n = 3 ; Console . WriteLine ( count_of_ways ( n ) ) ; } }


"Breaking a number such that first part is integral division of second by a power of 10 | C # program to count ways to divide a String in two parts a and b such that b / pow ( 10 , p ) == a ; subString representing int a ; no of digits in a ; consider only most significant l1 characters of remaining String for int b ; if any of a or b contains leading 0 s discard this combination ; if both are equal ; Driver Code"
Solution in C#:

using System ; class GFG { static int calculate ( String N ) { int len = N . Length ; int l = ( len ) / 2 ; int count = 0 ; for ( int i = 1 ; i <= l ; i ++ ) { String s = N . Substring ( 0 , i ) ; int l1 = s . Length ; String t = N . Substring ( i , l1 ) ; if ( s [ 0 ] == '0' t [ 0 ] == '0' ) continue ; if ( s . CompareTo ( t ) == 0 ) count ++ ; } return count ; } public static void Main ( String [ ] args ) { String N = "2202200" ; Console . Write ( calculate ( N ) ) ; } }


"Breaking an Integer to get Maximum Product |  ; The main function that returns the max possible product ; n equals to 2 or 3 must be handled explicitly ; Keep removing parts of size 3 while n is greater than 4 ; Keep multiplying 3 to res ; The last part multiplied by previous parts ; Driver code"
Solution in C#:

using System ; class GFG { static int maxProd ( int n ) { if ( n == 2 n == 3 ) return ( n - 1 ) ; int res = 1 ; while ( n > 4 ) { n -= 3 ; res *= 3 ; } return ( n * res ) ; } static void Main ( ) { Console . WriteLine ( " Maximum ▁ Product ▁ is ▁ " + maxProd ( 45 ) ) ; } }


"Breaking an Integer to get Maximum Product | C # program to find maximum product by breaking the Integer ; method return x ^ a in log ( a ) time ; Method returns maximum product obtained by breaking N ; base case 2 = 1 + 1 ; base case 3 = 2 + 1 ; breaking based on mod with 3 ; If divides evenly , then break into all 3 ; If division gives mod as 1 , then break as 4 + power of 3 for remaining part ; If division gives mod as 2 , then break as 2 + power of 3 for remaining part ; Driver code to test above methods"
Solution in C#:

class GFG { static int power ( int x , int a ) { int res = 1 ; while ( a > 0 ) { if ( ( a & 1 ) > 0 ) res = res * x ; x = x * x ; a >>= 1 ; } return res ; } static int breakInteger ( int N ) { if ( N == 2 ) return 1 ; if ( N == 3 ) return 2 ; int maxProduct = - 1 ; switch ( N % 3 ) { case 0 : maxProduct = power ( 3 , N / 3 ) ; break ; case 1 : maxProduct = 2 * 2 * power ( 3 , ( N / 3 ) - 1 ) ; break ; case 2 : maxProduct = 2 * power ( 3 , N / 3 ) ; break ; } return maxProduct ; } public static void Main ( ) { int maxProduct = breakInteger ( 10 ) ; System . Console . WriteLine ( maxProduct ) ; } }


"Brilliant Numbers | C # implementation for the above approach ; Function to generate all prime numbers less than n ; Initialize all entries of boolean array as true . A value in isPrime [ i ] will finally be false if i is Not a prime ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to return the number of digits in a number ; Function to check if N is a Brilliant number ; Generating primes using Sieve ; Traversing all numbers to find first pair ; Driver Code ; Given Number ; Function Call"
Solution in C#:

using System ; class GFG { static void SieveOfEratosthenes ( int n , bool [ ] isPrime ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) isPrime [ i ] = false ; } } } static int countDigit ( int n ) { int count = 0 ; while ( n != 0 ) { n = n / 10 ; ++ count ; } return count ; } static bool isBrilliant ( int n ) { bool [ ] isPrime = new bool [ n + 1 ] ; SieveOfEratosthenes ( n , isPrime ) ; for ( int i = 2 ; i < n ; i ++ ) { int x = n / i ; if ( isPrime [ i ] && isPrime [ x ] && ( x * i ) == n ) { if ( countDigit ( i ) == countDigit ( x ) ) return true ; } } return false ; } public static void Main ( ) { int n = 1711 ; if ( isBrilliant ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Bubble Sort On Doubly Linked List | C # program to sort a doubly linked list using bubble sort ; structure of a node ; Function to insert a node at the beginning of a linked list ; Function to print nodes in a given linked list ; Bubble sort the given linked list ; Checking for empty list ; Driver code ; start with empty linked list ; Create linked list from the array arr [ ] . Created linked list will be 1 -> 11 -> 2 -> 56 -> 12 ; print list before sorting ; sort the linked list ; print list after sorting"
Solution in C#:

 using System ; class GFG { public class Node { public int data ; public Node prev ; public Node next ; } ; static Node insertAtTheBegin ( Node start_ref , int data ) { Node ptr1 = new Node ( ) ; ptr1 . data = data ; ptr1 . next = start_ref ; if ( start_ref != null ) ( start_ref ) . prev = ptr1 ; start_ref = ptr1 ; return start_ref ; } static void printList ( Node start ) { Node temp = start ; Console . WriteLine ( ) ; while ( temp != null ) { Console . Write ( temp . data + " ▁ " ) ; temp = temp . next ; } } static Node bubbleSort ( Node start ) { int swapped ; Node ptr1 ; Node lptr = null ; if ( start == null ) return null ; do { swapped = 0 ; ptr1 = start ; while ( ptr1 . next != lptr ) { if ( ptr1 . data > ptr1 . next . data ) { int t = ptr1 . data ; ptr1 . data = ptr1 . next . data ; ptr1 . next . data = t ; swapped = 1 ; } ptr1 = ptr1 . next ; } lptr = ptr1 ; } while ( swapped != 0 ) ; return start ; } public static void Main ( String [ ] args ) { int [ ] arr = { 12 , 56 , 2 , 11 , 1 , 90 } ; int i ; Node start = null ; for ( i = 0 ; i < 6 ; i ++ ) start = insertAtTheBegin ( start , arr [ i ] ) ; Console . Write ( "
 ▁ Linked ▁ list ▁ before ▁ sorting ▁ " ) ; printList ( start ) ; start = bubbleSort ( start ) ; Console . Write ( "
 ▁ Linked ▁ list ▁ after ▁ sorting ▁ " ) ; printList ( start ) ; } }


"Bucket Sort To Sort an Array with Negative Numbers | C # program to sort an array of positive and negative numbers using bucket sort ; Function to sort [ ] arr of size n using bucket sort ; 1 ) Create n empty buckets ; 2 ) Put array elements in different buckets ; int bi = ( int ) ( n * arr [ i ] ) ; Index in bucket ; 3 ) Sort individual buckets ; 4 ) Concatenate all buckets into [ ] arr ; This function mainly splits array into two and then calls bucketSort ( ) for two arrays . ; traverse array elements ; store - Ve elements by converting into + ve element ; store + ve elements ; First store elements of Neg [ ] array by converting into - ve ; store + ve element ; Driver program to test above function"
Solution in C#:

 using System ; using System . Collections . Generic ; public class GFG { static void bucketSort ( List < Double > arr , int n ) { List < Double > [ ] b = new List < Double > [ n ] ; for ( int i = 0 ; i < b . Length ; i ++ ) b [ i ] = new List < Double > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { b [ bi ] . Add ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) b [ i ] . Sort ( ) ; int index = 0 ; arr . Clear ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < b [ i ] . Count ; j ++ ) arr . Add ( b [ i ] [ j ] ) ; } static void sortMixed ( double [ ] arr , int n ) { List < Double > Neg = new List < Double > ( ) ; List < Double > Pos = new List < Double > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) Neg . Add ( - 1 * arr [ i ] ) ; else Pos . Add ( arr [ i ] ) ; } bucketSort ( Neg , ( int ) Neg . Count ) ; bucketSort ( Pos , ( int ) Pos . Count ) ; for ( int i = 0 ; i < Neg . Count ; i ++ ) arr [ i ] = - 1 * Neg [ Neg . Count - 1 - i ] ; for ( int j = Neg . Count ; j < n ; j ++ ) arr [ j ] = Pos [ j - Neg . Count ] ; } public static void Main ( String [ ] args ) { double [ ] arr = { - 0.897 , 0.565 , 0.656 , - 0.1234 , 0 , 0.3434 } ; int n = arr . Length ; sortMixed ( arr , n ) ; Console . Write ( " Sorted ▁ array ▁ is ▁
 " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }


"Bucket Sort | C # program to sort an array using bucket sort ; Function to sort arr [ ] of size n using bucket sort ; 1 ) Create n empty buckets ; 2 ) Put array elements in different buckets ; 3 ) Sort individual buckets ; 4 ) Concatenate all buckets into arr [ ] ; Driver code"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static void bucketSort ( float [ ] arr , int n ) { if ( n <= 0 ) return ; List < float > [ ] buckets = new List < float > [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { buckets [ i ] = new List < float > ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { float idx = arr [ i ] * n ; buckets [ ( int ) idx ] . Add ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { buckets [ i ] . Sort ( ) ; } int index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < buckets [ i ] . Count ; j ++ ) { arr [ index ++ ] = buckets [ i ] [ j ] ; } } } public static void Main ( ) { float [ ] arr = { ( float ) 0.897 , ( float ) 0.565 , ( float ) 0.656 , ( float ) 0.1234 , ( float ) 0.665 , ( float ) 0.3434 } ; int n = arr . Length ; bucketSort ( arr , n ) ; Console . WriteLine ( " Sorted ▁ array ▁ is ▁ " ) ; foreach ( float el in arr ) { Console . Write ( el + " ▁ " ) ; } } }