"Round the given number to nearest multiple of 10 | C # Code for Round the given number to nearest multiple of 10 ; function to round the number ; Smaller multiple ; Larger multiple ; Return of closest of two ; Driver program"
Solution in C#:

using System ; class GFG { static int round ( int n ) { int a = ( n / 10 ) * 10 ; int b = a + 10 ; return ( n - a > b - n ) ? b : a ; } public static void Main ( ) { int n = 4722 ; Console . WriteLine ( round ( n ) ) ; } }


"Round the given number to nearest multiple of 10 | C # code for above approach ; Program to round the number to the nearest number having one 's digit 0 ; last character is 0 then return the original string ; if last character is 1 or 2 or 3 or 4 or 5 make it 0 ; process carry ; return final string ; Driver code ; Function Call"
Solution in C#:

using System ; class GFG { static string round ( string s , int n ) { char [ ] c = s . ToCharArray ( ) ; if ( c [ n - 1 ] == '0' ) return s ; else if ( c [ n - 1 ] == '1' c [ n - 1 ] == '2' c [ n - 1 ] == '3' c [ n - 1 ] == '4' c [ n - 1 ] == '5' ) { c [ n - 1 ] = '0' ; return new string ( c ) ; } else { c [ n - 1 ] = '0' ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( c [ i ] == '9' ) c [ i ] = '0' ; else { int t = c [ i ] - '0' + 1 ; c [ i ] = ( char ) ( 48 + t ) ; break ; } } } string s1 = new string ( c ) ; if ( s1 [ 0 ] == '0' ) s1 = "1" + s1 ; return s1 ; } static void Main ( ) { string s = "5748965412485599999874589965999" ; int n = s . Length ; Console . WriteLine ( round ( s , n ) ) ; } }


"Round the given number to nearest multiple of 10 | Set | C # implementation of the approach ; Function to round the given number to the nearest multiple of 10 ; If string is empty ; If the last digit is less then or equal to 5 then it can be rounded to the nearest ( previous ) multiple of 10 by just replacing the last digit with 0 ; Set the last digit to 0 ; Print the updated number ; The number hast to be rounded to the next multiple of 10 ; To store the carry ; Replace the last digit with 0 ; Starting from the second last digit , add 1 to digits while there is carry ; While there are digits to consider and there is carry to add ; Get the current digit ; Add the carry ; If the digit exceeds 9 then the carry will be generated ; Else there will be no carry ; Update the current digit ; Get to the previous digit ; If the carry is still 1 then it must be inserted at the beginning of the string ; Prin the rest of the number ; Driver code"
Solution in C#:

using System ; using System . Text ; class GFG { static void roundToNearest ( StringBuilder str , int n ) { if ( str . ToString ( ) == " " ) return ; if ( str [ n - 1 ] - '0' <= 5 ) { str [ n - 1 ] = '0' ; Console . Write ( str . ToString ( ) . Substring ( 0 , n ) ) ; } else { int carry = 0 ; str [ n - 1 ] = '0' ; int i = n - 2 ; carry = 1 ; while ( i >= 0 && carry == 1 ) { int currentDigit = str [ i ] - '0' ; currentDigit += carry ; if ( currentDigit > 9 ) { carry = 1 ; currentDigit = 0 ; } else carry = 0 ; str [ i ] = ( char ) ( currentDigit + '0' ) ; i -- ; } if ( carry == 1 ) Console . Write ( carry ) ; Console . Write ( str . ToString ( ) . Substring ( 0 , n ) ) ; } } public static void Main ( String [ ] args ) { StringBuilder str = new StringBuilder ( "99999999999999993" ) ; int n = str . Length ; roundToNearest ( str , n ) ; } }


"Row wise sorting in 2D array | C # code to sort 2D matrix row - wise ; loop for rows of matrix ; loop for column of matrix ; loop for comparison and swapping ; swapping of elements ; printing the sorted matrix ; Driver Code"
Solution in C#:

using System ; class GFG { static int sortRowWise ( int [ , ] m ) { for ( int i = 0 ; i < m . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < m . GetLength ( 1 ) ; j ++ ) { for ( int k = 0 ; k < m . GetLength ( 1 ) - j - 1 ; k ++ ) { if ( m [ i , k ] > m [ i , k + 1 ] ) { int t = m [ i , k ] ; m [ i , k ] = m [ i , k + 1 ] ; m [ i , k + 1 ] = t ; } } } } for ( int i = 0 ; i < m . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < m . GetLength ( 1 ) ; j ++ ) Console . Write ( m [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } return 0 ; } public static void Main ( String [ ] args ) { int [ , ] m = { { 9 , 8 , 7 , 1 } , { 7 , 3 , 0 , 2 } , { 9 , 5 , 3 , 2 } , { 6 , 3 , 1 , 2 } } ; sortRowWise ( m ) ; } }


"Row | C # program to find common elements in two diagonals . ; Returns count of row wise same elements in two diagonals of mat [ n ] [ n ] ; Driver Code"
Solution in C#:

using System ; class GFG { static int countCommon ( int [ , ] mat , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( mat [ i , i ] == mat [ i , n - i - 1 ] ) res ++ ; return res ; } public static void Main ( ) { int [ , ] mat = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; Console . WriteLine ( countCommon ( mat , 3 ) ) ; } }


"Run Length Encoding | C # program to implement run length encoding ; Count occurrences of current character ; Print character and its count ; Driver code"
Solution in C#:

using System ; class GFG { public class RunLength_Encoding { public static void printRLE ( String str ) { int n = str . Length ; for ( int i = 0 ; i < n ; i ++ ) { int count = 1 ; while ( i < n - 1 && str [ i ] == str [ i + 1 ] ) { count ++ ; i ++ ; } Console . Write ( str [ i ] ) ; Console . Write ( count ) ; } } public static void Main ( String [ ] args ) { String str = " wwwwaaadexxxxxxywww " ; printRLE ( str ) ; } } }


"Runge | C # program to implement Runge Kutta method ; A sample differential equation " dy / dx ▁ = ▁ ( x ▁ - ▁ y ) /2" ; Finds value of y for a given x using step size h and initial value y0 at x0 . ; Count number of iterations using step size or step height h ; Iterate for number of iterations ; Apply Runge Kutta Formulas to find next value of y ; Update next value of y ; Update next value of x ; Driver Code"
Solution in C#:

using System ; class GFG { static double dydx ( double x , double y ) { return ( x + y - 2 ) ; } static double rungeKutta ( double x0 , double y0 , double x , double h ) { int n = ( int ) ( ( x - x0 ) / h ) ; double k1 , k2 ; double y = y0 ; for ( int i = 1 ; i <= n ; i ++ ) { k1 = h * dydx ( x0 , y ) ; k2 = h * dydx ( x0 + 0.5 * h , y + 0.5 * k1 ) ; y = y + ( 1.0 / 6.0 ) * ( k1 + 2 * k2 ) ; x0 = x0 + h ; } return y ; } public static void Main ( string [ ] args ) { double x0 = 0 , y = 1 , x = 2 , h = 0.2 ; Console . WriteLine ( rungeKutta ( x0 , y , x , h ) ) ; } }


"Saddleback Search Algorithm in a 2D array | C # program to search an element in row - wise and column - wise sorted matrix ; Searches the element x in mat [ m ] [ n ] . If the element is found , then prints its position and returns true , otherwise prints " not ▁ found " and returns false ; set indexes for bottom left element ; if mat [ i ] [ j ] < x ; Driver Code"
Solution in C#:

using System ; class GFG { static bool search ( int [ , ] mat , int m , int n , int x ) { int i = m - 1 , j = 0 ; while ( i >= 0 && j < n ) { if ( mat [ i , j ] == x ) return true ; if ( mat [ i , j ] > x ) i -- ; else j ++ ; } return false ; } public static void Main ( ) { int [ , ] mat = { { 10 , 20 , 30 , 40 } , { 15 , 25 , 35 , 45 } , { 27 , 29 , 37 , 48 } , { 32 , 33 , 39 , 50 } , { 50 , 60 , 70 , 80 } } ; if ( search ( mat , 5 , 4 , 29 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Saint | C # program for above approach ; Function to check if a number is a Saint - Exupery number ; Considering triplets in sorted order . The value of first element in sorted triplet can be at - most n / 3. ; The value of second element must be less than equal to n / 2 ; Driver Code ; Given Number N ; Function Call"
Solution in C#:

using System ; class GFG { static bool isSaintExuperyNum ( int n ) { for ( int i = 1 ; i <= n / 3 ; i ++ ) { for ( int j = i + 1 ; j <= n / 2 ; j ++ ) { int k = n / i / j ; if ( i * i + j * j == k * k ) { if ( i * j * k == n ) return true ; } } } return false ; } public static void Main ( ) { int N = 60 ; if ( isSaintExuperyNum ( N ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Same Number Of Set Bits As N | C # program to find numbers less than N that have same Number Of Set Bits As N ; returns number of set bits in a number ; function ; __builtin_popcount function that count set bits in n ; Iterate from n - 1 to 1 ; check if the number of set bits equals to temp increment count ; Driver Code"
Solution in C#:

using System ; class GFG { static int __builtin_popcount ( int n ) { int d , t = 0 ; while ( n > 0 ) { d = n % 2 ; n = n / 2 ; if ( d == 1 ) t ++ ; } return t ; } static int smallerNumsWithSameSetBits ( int n ) { int temp = __builtin_popcount ( n ) ; int count = 0 ; for ( int i = n - 1 ; i > 0 ; i -- ) { if ( temp == __builtin_popcount ( i ) ) count ++ ; } return count ; } static public void Main ( String [ ] args ) { int n = 4 ; Console . WriteLine ( smallerNumsWithSameSetBits ( n ) ) ; } }


"Sand Timer Flip Counting Problem | C # implementation of the approach ; Recursive function to return the gcd of a and b ; Everything divides 0 ; Function to print the number of flips for both the sand timers ; Driver code"
Solution in C#:

using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void flip ( int a , int b ) { int lcm = ( a * b ) / gcd ( a , b ) ; a = lcm / a ; b = lcm / b ; Console . WriteLine ( ( a - 1 ) + " ▁ " + ( b - 1 ) ) ; } public static void Main ( ) { int a = 10 ; int b = 5 ; flip ( a , b ) ; } }


"Satisfy the parabola when point ( A , B ) and the equation is given | C # implementation of the approach ; Function to find the required values ; Driver code"
Solution in C#:

using System ; class GFG { static void solve ( int A , int B ) { double p = B / 2.0 ; double M = Math . Ceiling ( 4 * p ) ; int N = 1 ; int O = - 2 * A ; double Q = Math . Ceiling ( A * A + 4 * p * p ) ; Console . Write ( M + " ▁ " + N + " ▁ " + O + " ▁ " + Q ) ; } static public void Main ( ) { int a = 1 ; int b = 1 ; solve ( a , b ) ; } }


"Save from Bishop in chessboard | Program to find the total safe positions to place your Bishop ; function to calc total safe position ; i , j denotes row and column of position of bishop ; calc distance in four direction ; calc total sum of distance + 1 for unsafe positions ; return total safe positions ; Driver function"
Solution in C#:

using System ; class GFG { static int calcSafe ( int pos ) { int j = pos % 10 ; int i = pos / 10 ; int dis_11 = Math . Min ( Math . Abs ( 1 - i ) , Math . Abs ( 1 - j ) ) ; int dis_18 = Math . Min ( Math . Abs ( 1 - i ) , Math . Abs ( 8 - j ) ) ; int dis_81 = Math . Min ( Math . Abs ( 8 - i ) , Math . Abs ( 1 - j ) ) ; int dis_88 = Math . Min ( Math . Abs ( 8 - i ) , Math . Abs ( 8 - j ) ) ; int sum = dis_11 + dis_18 + dis_81 + dis_88 + 1 ; return ( 64 - sum ) ; } public static void Main ( ) { int pos = 34 ; Console . WriteLine ( " Safe ▁ Positions ▁ = ▁ " + calcSafe ( pos ) ) ; } }


"Schedule jobs so that each server gets equal load | C # program to schedule jobs so that each server gets equal load . ; Function to find new array a ; find sum S of both arrays a and b . ; Single element case . ; This checks whether sum s can be divided equally between all array elements . i . e . whether all elements can take equal value or not . ; Compute possible value of new array elements . ; Possibility 1 ; ensuring that all elements of array b are used . ; If a ( i ) already updated to x move to next element in array a . ; Possibility 2 ; Possibility 3 ; Possibility 4 ; If a ( i ) can not be made equal to x even after adding all possible elements from b ( i ) then print - 1. ; check whether all elements of b are used . ; Return the new array element value . ; Driver code"
Solution in C#:

using System ; class GFG { static int solve ( int [ ] a , int [ ] b , int n ) { int i ; int s = 0 ; for ( i = 0 ; i < n ; i ++ ) s += ( a [ i ] + b [ i ] ) ; if ( n == 1 ) return a [ 0 ] + b [ 0 ] ; if ( s % n != 0 ) return - 1 ; int x = s / n ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] > x ) return - 1 ; if ( i > 0 ) { a [ i ] += b [ i - 1 ] ; b [ i - 1 ] = 0 ; } if ( a [ i ] == x ) continue ; int y = a [ i ] + b [ i ] ; if ( i + 1 < n ) y += b [ i + 1 ] ; if ( y == x ) { a [ i ] = y ; b [ i ] = 0 ; continue ; } if ( a [ i ] + b [ i ] == x ) { a [ i ] += b [ i ] ; b [ i ] = 0 ; continue ; } if ( i + 1 < n && a [ i ] + b [ i + 1 ] == x ) { a [ i ] += b [ i + 1 ] ; b [ i + 1 ] = 0 ; continue ; } return - 1 ; } for ( i = 0 ; i < n ; i ++ ) if ( b [ i ] != 0 ) return - 1 ; return x ; } public static void Main ( String [ ] args ) { int [ ] a = { 6 , 14 , 21 , 1 } ; int [ ] b = { 15 , 7 , 10 , 10 } ; int n = a . Length ; Console . WriteLine ( solve ( a , b , n ) ) ; } }


"SchrÃ ¶ derâ €“ Hipparchus number | A memoization based optimized C # program to find n - th Schroder - Hipparchus number ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 500 ; static int nthSHN ( int n , int [ ] dp ) { if ( n == 1 n == 2 ) return dp [ n ] = 1 ; if ( dp [ n ] != - 1 ) return dp [ n ] ; return dp [ n ] = ( ( 6 * n - 9 ) * nthSHN ( n - 1 , dp ) - ( n - 3 ) * nthSHN ( n - 2 , dp ) ) / n ; } public static void Main ( ) { int n = 6 ; int [ ] dp = new int [ MAX ] ; for ( int i = 0 ; i < dp . Length ; i ++ ) dp [ i ] = - 1 ; Console . Write ( nthSHN ( n , dp ) ) ; } }


"Score of Parentheses using Tree | C # program to find the score of parentheses using Tree ; Customized tree class or struct , contains all required methods . ; Function to add a child into the list of children ; Function to change the parent pointer to the node passed ; Function to return the parent of the current node ; Function to compute the score recursively . ; Base case ; Adds scores of all children ; Function to create the tree structure ; Creating a node for every " ( ) " ; If we find " ( " we add a node as a child ; On finding " ) " which confirms that a pair is closed , we go back to the parent ; Driver code ; Generating the tree ; Computing the score"
Solution in C#:

using System ; using System . Collections ; class GFG { class TreeNode { public TreeNode parent = null ; public ArrayList children = new ArrayList ( ) ; public void addChild ( TreeNode node ) { children . Add ( node ) ; } public void setParent ( TreeNode node ) { parent = node ; } public TreeNode getParent ( ) { return parent ; } public int computeScore ( ) { if ( children . Count == 0 ) return 1 ; int res = 0 ; foreach ( TreeNode curr in children ) res += curr . computeScore ( ) ; if ( parent == null ) return res ; else return 2 * res ; } } ; static TreeNode computeTree ( string s ) { TreeNode current = new TreeNode ( ) ; TreeNode root = current ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == ' ( ' ) { TreeNode child = new TreeNode ( ) ; child . setParent ( current ) ; current . addChild ( child ) ; current = child ; } else { current = current . getParent ( ) ; } } return root ; } public static void Main ( ) { string s = " ( ( ) ( ( ) ) ) " ; TreeNode root = computeTree ( s ) ; Console . Write ( root . computeScore ( ) ) ; } }


"Search , insert and delete in a sorted array | C # program to delete an element from a sorted array ; binary search ; Function to delete an element ; Find position of element to be deleted ; Deleting element ; Driver Code"
Solution in C#:

 using System ; public class GFG { static int binarySearch ( int [ ] arr , int low , int high , int key ) { if ( high < low ) return - 1 ; int mid = ( low + high ) / 2 ; if ( key == arr [ mid ] ) return mid ; if ( key > arr [ mid ] ) return binarySearch ( arr , ( mid + 1 ) , high , key ) ; return binarySearch ( arr , low , ( mid - 1 ) , key ) ; } static int deleteElement ( int [ ] arr , int n , int key ) { int pos = binarySearch ( arr , 0 , n - 1 , key ) ; if ( pos == - 1 ) { Console . WriteLine ( " Element ▁ not ▁ found " ) ; return n ; } int i ; for ( i = pos ; i < n - 1 ; i ++ ) arr [ i ] = arr [ i + 1 ] ; return n - 1 ; } public static void Main ( ) { int i ; int [ ] arr = { 10 , 20 , 30 , 40 , 50 } ; int n = arr . Length ; int key = 30 ; Console . Write ( " Array ▁ before ▁ deletion :
 " ) ; for ( i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; n = deleteElement ( arr , n , key ) ; Console . Write ( "

 Array ▁ after ▁ deletion :
 " ) ; for ( i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }


"Search , insert and delete in a sorted array | C # program to implement binary search in a sorted array ; function to implement binary search ; low + ( high - low ) / 2 ; ; Driver Code"
Solution in C#:

using System ; public class GFG { public static int binarySearch ( int [ ] arr , int low , int high , int key ) { if ( high < low ) { return - 1 ; } int mid = ( low + high ) / 2 ; if ( key == arr [ mid ] ) { return mid ; } if ( key > arr [ mid ] ) { return binarySearch ( arr , ( mid + 1 ) , high , key ) ; } return binarySearch ( arr , low , ( mid - 1 ) , key ) ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 5 , 6 , 7 , 8 , 9 , 10 } ; int n , key ; n = arr . Length ; key = 10 ; Console . WriteLine ( " Index : ▁ " + binarySearch ( arr , 0 , n - 1 , key ) ) ; } }


"Search , insert and delete in a sorted array | C # program to insert an element in a sorted array ; Inserts a key in arr [ ] of given capacity . n is current size of arr [ ] . This function returns n + 1 if insertion is successful , else n . ; Cannot insert more elements if n is already more than or equal to capcity ; Driver program to test above function ; Inserting key"
Solution in C#:

 using System ; public class GFG { public static int insertSorted ( int [ ] arr , int n , int key , int capacity ) { if ( n >= capacity ) { return n ; } int i ; for ( i = n - 1 ; ( i >= 0 && arr [ i ] > key ) ; i -- ) { arr [ i + 1 ] = arr [ i ] ; } arr [ i + 1 ] = key ; return ( n + 1 ) ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ 20 ] ; arr [ 0 ] = 12 ; arr [ 1 ] = 16 ; arr [ 2 ] = 20 ; arr [ 3 ] = 40 ; arr [ 4 ] = 50 ; arr [ 5 ] = 70 ; int capacity = arr . Length ; int n = 6 ; int key = 26 ; Console . Write ( "
 Before ▁ Insertion : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } n = insertSorted ( arr , n , key , capacity ) ; Console . Write ( "
 After ▁ Insertion : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } }


"Search , insert and delete in an unsorted array | C # program to implement delete operation in an unsorted array ; Function to search a key to be deleted ; Function to delete an element ; Find position of element to be deleted ; Deleting element ; Driver Code"
Solution in C#:

using System ; class main { static int findElement ( int [ ] arr , int n , int key ) { int i ; for ( i = 0 ; i < n ; i ++ ) if ( arr [ i ] == key ) return i ; return - 1 ; } static int deleteElement ( int [ ] arr , int n , int key ) { int pos = findElement ( arr , n , key ) ; if ( pos == - 1 ) { Console . WriteLine ( " Element ▁ not ▁ found " ) ; return n ; } int i ; for ( i = pos ; i < n - 1 ; i ++ ) arr [ i ] = arr [ i + 1 ] ; return n - 1 ; } public static void Main ( ) { int i ; int [ ] arr = { 10 , 50 , 30 , 40 , 20 } ; int n = arr . Length ; int key = 30 ; Console . Write ( " Array ▁ before ▁ deletion ▁ " ) ; for ( i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; n = deleteElement ( arr , n , key ) ; Console . Write ( " Array ▁ after ▁ deletion ▁ " ) ; for ( i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }


"Search , insert and delete in an unsorted array | C # program to implement insert operation in an unsorted array . ; Function to insert a given key in the array . This function returns n + 1 if insertion is successful , else n . ; Cannot insert more elements if n is already more than or equal to capcity ; Driver Code ; Inserting key"
Solution in C#:

using System ; class main { static int insertSorted ( int [ ] arr , int n , int key , int capacity ) { if ( n >= capacity ) return n ; arr [ n ] = key ; return ( n + 1 ) ; } public static void Main ( ) { int [ ] arr = new int [ 20 ] ; arr [ 0 ] = 12 ; arr [ 1 ] = 16 ; arr [ 2 ] = 20 ; arr [ 3 ] = 40 ; arr [ 4 ] = 50 ; arr [ 5 ] = 70 ; int capacity = 20 ; int n = 6 ; int i , key = 26 ; Console . Write ( " Before ▁ Insertion : ▁ " ) ; for ( i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; n = insertSorted ( arr , n , key , capacity ) ; Console . Write ( " After ▁ Insertion : ▁ " ) ; for ( i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }


"Search , insert and delete in an unsorted array | C # program to implement linear search in unsorted arrays ; Function to implement search operation ; Driver Code ; Using a last element as search element"
Solution in C#:

using System ; class main { static int findElement ( int [ ] arr , int n , int key ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == key ) return i ; return - 1 ; } public static void Main ( ) { int [ ] arr = { 12 , 34 , 10 , 6 , 40 } ; int n = arr . Length ; int key = 40 ; int position = findElement ( arr , n , key ) ; if ( position == - 1 ) Console . WriteLine ( " Element ▁ not ▁ found " ) ; else Console . WriteLine ( " Element ▁ Found ▁ at ▁ Position : ▁ " + ( position + 1 ) ) ; } }


"Search a Word in a 2D Grid of characters | C # program to search a word in a 2D grid ; Rows and columns in given grid ; For searching in all 8 direction ; This function searches in all 8 - direction from point ( row , col ) in grid [ , ] ; If first character of word doesn 't match  with given starting point in grid. ; Search word in all 8 directions starting from ( row , col ) ; Initialize starting point for current direction ; First character is already checked , match remaining characters ; If out of bound break ; If not matched , break ; Moving in particular direction ; If all character matched , then value of k must be equal to length of word ; Searches given word in a given matrix in all 8 directions ; Consider every point as starting point and search given word ; Driver code"
Solution in C#:

using System ; class GFG { static int R , C ; static int [ ] x = { - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 } ; static int [ ] y = { - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 } ; static bool search2D ( char [ , ] grid , int row , int col , String word ) { if ( grid [ row , col ] != word [ 0 ] ) { return false ; } int len = word . Length ; for ( int dir = 0 ; dir < 8 ; dir ++ ) { int k , rd = row + x [ dir ] , cd = col + y [ dir ] ; for ( k = 1 ; k < len ; k ++ ) { if ( rd >= R rd < 0 cd >= C cd < 0 ) { break ; } if ( grid [ rd , cd ] != word [ k ] ) { break ; } rd += x [ dir ] ; cd += y [ dir ] ; } if ( k == len ) { return true ; } } return false ; } static void patternSearch ( char [ , ] grid , String word ) { for ( int row = 0 ; row < R ; row ++ ) { for ( int col = 0 ; col < C ; col ++ ) { if ( search2D ( grid , row , col , word ) ) { Console . WriteLine ( " pattern ▁ found ▁ at ▁ " + row + " , ▁ " + col ) ; } } } } public static void Main ( String [ ] args ) { R = 3 ; C = 13 ; char [ , ] grid = { { ' G ' , ' E ' , ' E ' , ' K ' , ' S ' , ' F ' , ' O ' , ' R ' , ' G ' , ' E ' , ' E ' , ' K ' , ' S ' } , { ' G ' , ' E ' , ' E ' , ' K ' , ' S ' , ' Q ' , ' U ' , ' I ' , ' Z ' , ' G ' , ' E ' , ' E ' , ' K ' } , { ' I ' , ' D ' , ' E ' , ' Q ' , ' A ' , ' P ' , ' R ' , ' A ' , ' C ' , ' T ' , ' I ' , ' C ' , ' E ' } } ; patternSearch ( grid , " GEEKS " ) ; Console . WriteLine ( ) ; patternSearch ( grid , " EEE " ) ; } } is by -


"Search an element in a Doubly Linked List | C # program to implement the above approach ; Structure of a node of the doubly linked list ; Stores data value of a node ; Stores pointer to next node ; Stores pointer to previous node ; Function to insert a node at the beginning of the Doubly Linked List ; Allocate memory for new node ; Insert the data ; Since node is added at the beginning , prev is always null ; Link the old list to the new node ; If pointer to head is not null ; Change the prev of head node to new node ; Move the head to point to the new node ; Function to find the position of an integer in doubly linked list ; Stores head Node ; Stores position of the integer in the doubly linked list ; Traverse the doubly linked list ; Update pos ; Update temp ; If the integer not present in the doubly linked list ; If the integer present in the doubly linked list ; Driver Code ; Create the doubly linked list 18 < -> 15 < -> 8 < -> 9 < -> 14"
Solution in C#:

using System ; class GFG { public class Node { public int data ; public Node next ; public Node prev ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . prev = null ; new_node . next = head_ref ; if ( head_ref != null ) { head_ref . prev = new_node ; } head_ref = new_node ; return head_ref ; } static int search ( Node head_ref , int x ) { Node temp = head_ref ; int pos = 0 ; while ( temp . data != x && temp . next != null ) { pos ++ ; temp = temp . next ; } if ( temp . data != x ) return - 1 ; return ( pos + 1 ) ; } public static void Main ( String [ ] args ) { Node head = null ; int X = 8 ; head = push ( head , 14 ) ; head = push ( head , 9 ) ; head = push ( head , 8 ) ; head = push ( head , 15 ) ; head = push ( head , 18 ) ; Console . Write ( search ( head , X ) ) ; } }


"Search an element in a Linked List ( Iterative and Recursive ) | Recursive C # program to search an element in linked list ; Node class ; Linked list class ; Head of list ; Inserts a new node at the front of the list ; Allocate new node and putting data ; Make next of new node as head ; Move the head to point to new Node ; Checks whether the value x is present in linked list ; Base case ; If key is present in current node , return true ; Recur for remaining list ; Driver code ; Start with the empty list ; Use push ( ) to construct below list 14 -> 21 -> 11 -> 30 -> 10"
Solution in C#:

using System ; public class Node { public int data ; public Node next ; public Node ( int d ) { data = d ; next = null ; } } public class LinkedList { Node head ; public void push ( int new_data ) { Node new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; } public bool search ( Node head , int x ) { if ( head == null ) return false ; if ( head . data == x ) return true ; return search ( head . next , x ) ; } public static void Main ( ) { LinkedList llist = new LinkedList ( ) ; llist . push ( 10 ) ; llist . push ( 30 ) ; llist . push ( 11 ) ; llist . push ( 21 ) ; llist . push ( 14 ) ; if ( llist . search ( llist . head , 21 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Search an element in a reverse sorted array | C # Program to implement the above approach ; Function to search if element X is present in reverse sorted array ; Store the first index of the subarray in which X lies ; Store the last index of the subarray in which X lies ; Store the middle index of the subarray ; Check if value at middle index of the subarray equal to X ; Element is found ; If X is smaller than the value at middle index of the subarray ; Search in right half of subarray ; Search in left half of subarray ; If X not found ; Driver code"
Solution in C#:

using System ; class GFG { static int binarySearch ( int [ ] arr , int N , int X ) { int start = 0 ; int end = N ; while ( start <= end ) { int mid = start + ( end - start ) / 2 ; if ( X == arr [ mid ] ) { return mid ; } else if ( X < arr [ mid ] ) { start = mid + 1 ; } else { end = mid - 1 ; } } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 4 , 3 , 2 , 1 } ; int N = arr . Length ; int X = 5 ; Console . WriteLine ( binarySearch ( arr , N , X ) ) ; } }


"Search an element in a sorted array formed by reversing subarrays from a random index | C # program for the above approach ; Function to search an element in a sorted array formed by reversing subarrays from a random index ; Set the boundaries for binary search ; Apply binary search ; Initialize the middle element ; If element found ; Random point is on right side of mid ; From l to mid arr is reverse sorted ; Random point is on the left side of mid ; From mid to h arr is reverse sorted ; Return Not Found ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int find ( List < int > arr , int N , int key ) { int l = 0 ; int h = N - 1 ; while ( l <= h ) { int mid = ( l + h ) / 2 ; if ( arr [ mid ] == key ) return mid ; if ( arr [ l ] >= arr [ mid ] ) { if ( arr [ l ] >= key && key >= arr [ mid ] ) h = mid - 1 ; else l = mid + 1 ; } else { if ( arr [ mid ] >= key && key >= arr [ h ] ) l = mid + 1 ; else h = mid - 1 ; } } return - 1 ; } public static void Main ( ) { List < int > arr = new List < int > ( ) { 10 , 8 , 6 , 5 , 2 , 1 , 13 , 12 } ; int N = arr . Count ; int key = 8 ; int ans = find ( arr , N , key ) ; Console . Write ( ans ) ; } }


"Search an element in an array where difference between adjacent elements is 1 | C # program to search an element in an array where difference between all elements is 1 ; in arr [ 0. . n - 1 ] ; Traverse the given array starting from leftmost element ; If x is found at index i ; Jump the difference between current array element and x ; Driver code"
Solution in C#:

using System ; public class GFG { static int search ( int [ ] arr , int n , int x ) { int i = 0 ; while ( i < n ) { if ( arr [ i ] == x ) return i ; i = i + Math . Abs ( arr [ i ] - x ) ; } Console . WriteLine ( " number ▁ is ▁ not " + " ▁ present ! " ) ; return - 1 ; } public static void Main ( ) { int [ ] arr = { 8 , 7 , 6 , 7 , 6 , 5 , 4 , 3 , 2 , 3 , 4 , 3 } ; int n = arr . Length ; int x = 3 ; Console . WriteLine ( " Element ▁ " + x + " ▁ is ▁ present ▁ at ▁ index ▁ " + search ( arr , n , 3 ) ) ; } }


"Search an element in an unsorted array using minimum number of comparisons | C # implementation to search an element in the unsorted array using minimum number of comparisons ; Function to search an element in minimum number of comparisons ; 1 st comparison ; no termination condition and thus no comparison ; this would be executed at - most n times and therefore at - most n comparisons ; replace arr [ n - 1 ] with its actual element as in original ' arr [ ] ' ; if ' x ' is found before the ' ( n - 1 ) th ' index , then it is present in the array final comparison ; else not present in the array ; driver program"
Solution in C#:

using System ; class GFG { static String search ( int [ ] arr , int n , int x ) { if ( arr [ n - 1 ] == x ) return " Found " ; int backup = arr [ n - 1 ] ; arr [ n - 1 ] = x ; for ( int i = 0 ; ; i ++ ) { if ( arr [ i ] == x ) { arr [ n - 1 ] = backup ; if ( i < n - 1 ) return " Found " ; return " Not ▁ Found " ; } } } public static void Main ( ) { int [ ] arr = { 4 , 6 , 1 , 5 , 8 } ; int n = arr . Length ; int x = 1 ; Console . WriteLine ( search ( arr , n , x ) ) ; } }


"Search an element in given N ranges | C # implementation of the above approach ; Function to return the index of the range in which K lies and uses binary search ; Binary search ; Find the mid element ; If element is found ; Check in first half ; Check in second half ; Not found ; Driver code"
Solution in C#:

using System ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int findNumber ( pair [ ] a , int n , int K ) { int low = 0 , high = n - 1 ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; if ( K >= a [ mid ] . first && K <= a [ mid ] . second ) return mid ; else if ( K < a [ mid ] . first ) high = mid - 1 ; else low = mid + 1 ; } return - 1 ; } public static void Main ( String [ ] args ) { pair [ ] a = { new pair ( 1 , 3 ) , new pair ( 4 , 7 ) , new pair ( 8 , 11 ) } ; int n = a . Length ; int k = 6 ; int index = findNumber ( a , n , k ) ; if ( index != - 1 ) Console . WriteLine ( index ) ; else Console . WriteLine ( - 1 ) ; } }


"Search an element in given N ranges | C # implementation of the approach ; Function to return the index of the range in which K lies and uses linear search ; Iterate and find the element ; If K lies in the current range ; K doesn 't lie in any  of the given ranges ; Driver code"
Solution in C#:

using System ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int findNumber ( pair [ ] a , int n , int K ) { for ( int i = 0 ; i < n ; i ++ ) { if ( K >= a [ i ] . first && K <= a [ i ] . second ) return i ; } return - 1 ; } public static void Main ( String [ ] args ) { pair [ ] a = { new pair ( 1 , 3 ) , new pair ( 4 , 7 ) , new pair ( 8 , 11 ) } ; int n = a . Length ; int k = 6 ; int index = findNumber ( a , n , k ) ; if ( index != - 1 ) Console . WriteLine ( index ) ; else Console . WriteLine ( - 1 ) ; } }


"Search element in a Spirally sorted Matrix | C # implementation of the above approach ; Function to return the ring , the number x belongs to . ; Returns - 1 if number x is smaller than least element of arr ; l and r represent the diagonal elements to search in ; Returns - 1 if number x is greater than the largest element of arr ; Function to perform binary search on an array sorted in increasing order l and r represent the left and right index of the row to be searched ; Function to perform binary search on a particular column of the 2D array t and b represent top and bottom rows ; Function to perform binary search on an array sorted in decreasing order ; Function to perform binary search on a particular column of the 2D array ; Function to find the position of the number x ; Finding the ring ; To store row and column ; Edge case if n is odd ; Check which of the 4 sides , the number x lies in ; Printing the position ; Driver code"
Solution in C#:

using System ; class GFG { static int n = 4 ; static int findRing ( int [ , ] arr , int x ) { if ( arr [ 0 , 0 ] > x ) return - 1 ; int l = 0 , r = ( n + 1 ) / 2 - 1 ; if ( n % 2 == 1 && arr [ r , r ] < x ) return - 1 ; if ( n % 2 == 0 && arr [ r + 1 , r ] < x ) return - 1 ; while ( l < r ) { int mid = ( l + r ) / 2 ; if ( arr [ mid , mid ] <= x ) if ( mid == ( n + 1 ) / 2 - 1 arr [ mid + 1 , mid + 1 ] > x ) return mid ; else l = mid + 1 ; else r = mid - 1 ; } return r ; } static int binarySearchRowInc ( int [ , ] arr , int row , int l , int r , int x ) { while ( l <= r ) { int mid = ( l + r ) / 2 ; if ( arr [ row , mid ] == x ) return mid ; if ( arr [ row , mid ] < x ) l = mid + 1 ; else r = mid - 1 ; } return - 1 ; } static int binarySearchColumnInc ( int [ , ] arr , int col , int t , int b , int x ) { while ( t <= b ) { int mid = ( t + b ) / 2 ; if ( arr [ mid , col ] == x ) return mid ; if ( arr [ mid , col ] < x ) t = mid + 1 ; else b = mid - 1 ; } return - 1 ; } static int binarySearchRowDec ( int [ , ] arr , int row , int l , int r , int x ) { while ( l <= r ) { int mid = ( l + r ) / 2 ; if ( arr [ row , mid ] == x ) return mid ; if ( arr [ row , mid ] < x ) r = mid - 1 ; else l = mid + 1 ; } return - 1 ; } static int binarySearchColumnDec ( int [ , ] arr , int col , int t , int b , int x ) { while ( t <= b ) { int mid = ( t + b ) / 2 ; if ( arr [ mid , col ] == x ) return mid ; if ( arr [ mid , col ] < x ) b = mid - 1 ; else t = mid + 1 ; } return - 1 ; } static void spiralBinary ( int [ , ] arr , int x ) { int f1 = findRing ( arr , x ) ; int r , c ; if ( f1 == - 1 ) { Console . Write ( " - 1" ) ; return ; } if ( n % 2 == 1 && f1 == ( n + 1 ) / 2 - 1 ) { Console . WriteLine ( f1 + " ▁ " + f1 ) ; return ; } if ( x < arr [ f1 , n - f1 - 1 ] ) { c = binarySearchRowInc ( arr , f1 , f1 , n - f1 - 2 , x ) ; r = f1 ; } else if ( x < arr [ n - f1 - 1 , n - f1 - 1 ] ) { c = n - f1 - 1 ; r = binarySearchColumnInc ( arr , n - f1 - 1 , f1 , n - f1 - 2 , x ) ; } else if ( x < arr [ n - f1 - 1 , f1 ] ) { c =...


"Search element in a sorted matrix | C # implementation to search an element in a sorted matrix ; This function does Binary search for x in i - th row . It does the search from mat [ i ] [ j_low ] to mat [ i ] [ j_high ] ; Element found ; element not found ; Function to perform binary search on the mid values of row to get the desired pair of rows where the element can be found ; Single row matrix ; Do binary search in middle column . Condition to terminate the loop when the 2 desired rows are found ; element found ; If element is present on the mid of the two rows ; search element on 1 st half of 1 st row ; Search element on 2 nd half of 1 st row ; Search element on 1 st half of 2 nd row ; search element on 2 nd half of 2 nd row ; Driver program"
Solution in C#:

using System ; class GFG { static void binarySearch ( int [ , ] mat , int i , int j_low , int j_high , int x ) { while ( j_low <= j_high ) { int j_mid = ( j_low + j_high ) / 2 ; if ( mat [ i , j_mid ] == x ) { Console . Write ( " Found ▁ at ▁ ( " + i + " , ▁ " + j_mid + " ) " ) ; return ; } else if ( mat [ i , j_mid ] > x ) j_high = j_mid - 1 ; else j_low = j_mid + 1 ; } Console . Write ( " Element ▁ no ▁ found " ) ; } static void sortedMatrixSearch ( int [ , ] mat , int n , int m , int x ) { if ( n == 1 ) { binarySearch ( mat , 0 , 0 , m - 1 , x ) ; return ; } int i_low = 0 ; int i_high = n - 1 ; int j_mid = m / 2 ; while ( ( i_low + 1 ) < i_high ) { int i_mid = ( i_low + i_high ) / 2 ; if ( mat [ i_mid , j_mid ] == x ) { Console . Write ( " Found ▁ at ▁ ( " + i_mid + " , ▁ " + j_mid + " ) " ) ; return ; } else if ( mat [ i_mid , j_mid ] > x ) i_high = i_mid ; else i_low = i_mid ; } if ( mat [ i_low , j_mid ] == x ) Console . Write ( " Found ▁ at ▁ ( " + i_low + " , " + j_mid + " ) " ) ; else if ( mat [ i_low + 1 , j_mid ] == x ) Console . Write ( " Found ▁ at ▁ ( " + ( i_low + 1 ) + " , ▁ " + j_mid + " ) " ) ; else if ( x <= mat [ i_low , j_mid - 1 ] ) binarySearch ( mat , i_low , 0 , j_mid - 1 , x ) ; else if ( x >= mat [ i_low , j_mid + 1 ] && x <= mat [ i_low , m - 1 ] ) binarySearch ( mat , i_low , j_mid + 1 , m - 1 , x ) ; else if ( x <= mat [ i_low + 1 , j_mid - 1 ] ) binarySearch ( mat , i_low + 1 , 0 , j_mid - 1 , x ) ; else binarySearch ( mat , i_low + 1 , j_mid + 1 , m - 1 , x ) ; } public static void Main ( String [ ] args ) { int n = 4 , m = 5 , x = 8 ; int [ , ] mat = { { 0 , 6 , 8 , 9 , 11 } , { 20 , 22 , 28 , 29 , 31 } , { 36 , 38 , 50 , 61 , 63 } , { 64 , 66 , 100 , 122 , 128 } } ; sortedMatrixSearch ( mat , n , m , x ) ; } }


"Search for an element in a Mountain Array | C # program for the above approach ; Function to find the index of the peak element in the array ; Stores left most index in which the peak element can be found ; Stores right most index in which the peak element can be found ; Stores mid of left and right ; If element at mid is less than element at ( mid + 1 ) ; Update left ; Update right ; Function to perform binary search in an a subarray if elements of the subarray are in an ascending order ; Stores mid of left and right ; If X found at mid ; If X is greater than mid ; Update left ; Update right ; Function to perform binary search in an a subarray if elements of the subarray are in an ascending order ; Stores mid of left and right ; If X found at mid ; Update right ; Update left ; Function to find the smallest index of X ; Stores index of peak element in array ; Stores index of X in the array ; If X greater than or equal to first element of array and less than the peak element ; Update res ; If element not found on left side of peak element ; Update res ; Print res ; Driver Code ; Given X ; Given array ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; using System . Linq ; class GFG { public static int findPeak ( List < int > arr ) { int left = 0 ; int right = arr . Count - 1 ; while ( left < right ) { int mid = left + ( right - left ) / 2 ; if ( arr [ mid ] < arr [ ( mid + 1 ) ] ) { left = mid + 1 ; } else { right = mid ; } } return left ; } static int BS ( int X , int left , int right , List < int > arr ) { while ( left <= right ) { int mid = left + ( right - left ) / 2 ; if ( arr [ ( mid ) ] == X ) { return mid ; } else if ( X > arr [ mid ] ) { left = mid + 1 ; } else { right = mid - 1 ; } } return - 1 ; } static int reverseBS ( int X , int left , int right , List < int > arr ) { while ( left <= right ) { int mid = left + ( right - left ) / 2 ; if ( arr [ mid ] == X ) { return mid ; } else if ( X > arr [ mid ] ) { right = mid - 1 ; } else { left = mid + 1 ; } } return - 1 ; } static void findInMA ( int X , List < int > mountainArr ) { int peakIndex = findPeak ( mountainArr ) ; int res = - 1 ; if ( X >= mountainArr [ 0 ] && X <= mountainArr [ peakIndex ] ) { res = BS ( X , 0 , peakIndex , mountainArr ) ; } if ( res == - 1 ) { res = reverseBS ( X , peakIndex + 1 , mountainArr . Count - 1 , mountainArr ) ; } Console . WriteLine ( res ) ; } public static void Main ( ) { int X = 3 ; List < int > list = new List < int > ( ) { 1 , 2 , 3 , 4 , 5 , 3 , 1 } ; findInMA ( X , list ) ; } }


"Search in a row wise and column wise sorted matrix | C # Code for Search in a row wise and column wise sorted matrix ; Searches the element x in mat [ ] [ ] . If the element is found , then prints its position and returns true , otherwise prints " not ▁ found " and returns false ; set indexes for top right element ; if mat [ i ] [ j ] < x ; if ( i == n j == - 1 ) ; driver program to test above function"
Solution in C#:

using System ; class GFG { private static void search ( int [ , ] mat , int n , int x ) { int i = 0 , j = n - 1 ; while ( i < n && j >= 0 ) { if ( mat [ i , j ] == x ) { Console . Write ( " n ▁ Found ▁ at ▁ " + i + " , ▁ " + j ) ; return ; } if ( mat [ i , j ] > x ) j -- ; else i ++ ; } Console . Write ( " n ▁ Element ▁ not ▁ found " ) ; return ; } public static void Main ( ) { int [ , ] mat = { { 10 , 20 , 30 , 40 } , { 15 , 25 , 35 , 45 } , { 27 , 29 , 37 , 48 } , { 32 , 33 , 39 , 50 } } ; search ( mat , 4 , 29 ) ; } }


"Search in a row wise and column wise sorted matrix | C # program to search an element in row - wise and column - wise sorted matrix ; Searches the element x in mat [ ] [ ] . If the element is found , then prints its position and returns true , otherwise prints " not ▁ found " and returns false ; Traverse through the matrix ; If the element is found ; Driver code"
Solution in C#:

 using System ; class GFG { static int search ( int [ , ] mat , int n , int x ) { if ( n == 0 ) return - 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) if ( mat [ i , j ] == x ) { Console . Write ( " Element ▁ found ▁ at ▁ ( " + i + " , ▁ " + j + " )
 " ) ; return 1 ; } } Console . Write ( " ▁ Element ▁ not ▁ found " ) ; return 0 ; } static public void Main ( ) { int [ , ] mat = { { 10 , 20 , 30 , 40 } , { 15 , 25 , 35 , 45 } , { 27 , 29 , 37 , 48 } , { 32 , 33 , 39 , 50 } } ; search ( mat , 4 , 29 ) ; } }


"Search in a sorted 2D matrix ( Stored in row major order ) | C # program to find whether a given element is present in the given 2 - D matrix ; Basic binary search to find an element in a 1 - D array ; if element found return true ; if middle less than K then skip the left part of the array else skip the right part ; if not found return false ; Function to search an element in a matrix based on Divide and conquer approach ; if the element lies in the range of this row then call 1 - D binary search on this row ; if the element is less then the starting element of that row then search in upper rows else search in the lower rows ; if not found ; Driver code"
Solution in C#:

using System ; class GFG { static int M = 3 ; static int N = 4 ; static bool binarySearch1D ( int [ ] arr , int K ) { int low = 0 ; int high = N - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( arr [ mid ] == K ) { return true ; } if ( arr [ mid ] < K ) { low = mid + 1 ; } else { high = mid - 1 ; } } return false ; } static bool searchMatrix ( int [ , ] matrix , int K ) { int low = 0 ; int high = M - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( K >= matrix [ mid , 0 ] && K <= matrix [ mid , N - 1 ] ) { return binarySearch1D ( GetRow ( matrix , mid ) , K ) ; } if ( K < matrix [ mid , 0 ] ) { high = mid - 1 ; } else { low = mid + 1 ; } } return false ; } 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 ; } public static void Main ( String [ ] args ) { int [ , ] matrix = { { 1 , 3 , 5 , 7 } , { 10 , 11 , 16 , 20 } , { 23 , 30 , 34 , 50 } } ; int K = 3 ; if ( searchMatrix ( matrix , K ) ) { Console . WriteLine ( " Found " ) ; } else { Console . WriteLine ( " Not ▁ found " ) ; } } }


"Search in an almost sorted array | C # program to find an element in an almost sorted array ; A recursive binary search based function . It returns index of x in given array arr [ l . . r ] is present , otherwise - 1 ; If the element is present at one of the middle 3 positions ; 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 code"
Solution in C#:

using System ; class GFG { 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 ( mid > l && arr [ mid - 1 ] == x ) return ( mid - 1 ) ; if ( mid < r && arr [ mid + 1 ] == x ) return ( mid + 1 ) ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 2 , x ) ; return binarySearch ( arr , mid + 2 , r , x ) ; } return - 1 ; } public static void Main ( ) { GFG ob = new GFG ( ) ; int [ ] arr = { 3 , 2 , 10 , 4 , 40 } ; int n = arr . Length ; int x = 4 ; int result = ob . binarySearch ( arr , 0 , n - 1 , x ) ; if ( result == - 1 ) Console . Write ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ) ; else Console . Write ( " Element ▁ is ▁ present ▁ at ▁ index ▁ " + result ) ; } }


"Search insert position of K in a sorted array | C # program for the above approach ; Function to find insert position of K ; Lower and upper bounds ; Traverse the search space ; If K is found ; Return insert position ; Driver Code"
Solution in C#:

using System ; class GFG { static int find_index ( int [ ] arr , int n , int K ) { int start = 0 ; int end = n - 1 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( arr [ mid ] == K ) return mid ; else if ( arr [ mid ] < K ) start = mid + 1 ; else end = mid - 1 ; } return end + 1 ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 6 } ; int n = arr . Length ; int K = 2 ; Console . WriteLine ( find_index ( arr , n , K ) ) ; } }


"Search insert position of K in a sorted array | C # program for the above approach ; Function to find insert position of K ; Traverse the array ; If K is found ; If current array element exceeds K ; If all elements are smaller than K ; Driver Code"
Solution in C#:

using System ; class GFG { static int find_index ( int [ ] arr , int n , int K ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == K ) return i ; else if ( arr [ i ] > K ) return i ; return n ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 6 } ; int n = arr . Length ; int K = 2 ; Console . WriteLine ( find_index ( arr , n , K ) ) ; } }


"Seating arrangement of N boys sitting around a round table such that two particular boys sit together | C # implementation of the approach ; Function to return the total count of ways ; Find ( n - 1 ) factorial ; Return ( n - 1 ) ! * 2 ! ; Driver code"
Solution in C#:

using System ; class GFG { static int Total_Ways ( int n ) { int fac = 1 ; for ( int i = 2 ; i <= n - 1 ; i ++ ) { fac = fac * i ; } return ( fac * 2 ) ; } static public void Main ( ) { int n = 5 ; Console . Write ( Total_Ways ( n ) ) ; } }


"Second Pentagonal numbers | C # implementation to find N - th term in the series ; Function to find N - th term in the series ; Driver code"
Solution in C#:

 using System ; class GFG { static void findNthTerm ( int n ) { Console . Write ( n * ( 3 * n + 1 ) / 2 + "
 " ) ; } public static void Main ( ) { int N = 4 ; findNthTerm ( N ) ; } }


"Second decagonal numbers | C # program for the above approach ; Function to find N - th term in the series ; Driver code"
Solution in C#:

using System ; class GFG { static void findNthTerm ( int n ) { Console . WriteLine ( n * ( 4 * n + 3 ) ) ; } public static void Main ( String [ ] args ) { int N = 4 ; findNthTerm ( N ) ; } }


"Second heptagonal numbers | C # implementation to find N - th term in the series ; Function to find N - th term in the series ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static void findNthTerm ( int n ) { Console . Write ( n * ( 5 * n + 3 ) / 2 ) ; } public static void Main ( ) { int N = 4 ; findNthTerm ( N ) ; } }


"Second hexagonal numbers | C # implementation to find N - th term in the series ; Function to find N - th term in the series ; Driver code"
Solution in C#:

using System ; class GFG { static void findNthTerm ( int n ) { Console . Write ( n * ( 2 * n + 1 ) ) ; } public static void Main ( ) { int N = 4 ; findNthTerm ( N ) ; } }


"Second most repeated word in a sequence | C # program to find out the second most repeated word ; Method to find the word ; Store all the words with its occurrence ; find the second largest occurrence ; Return string with occurrence equals to sec_max ; Driver method"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static String secMostRepeated ( List < String > seq ) { Dictionary < String , int > occ = new Dictionary < String , int > ( ) ; for ( int i = 0 ; i < seq . Count ; i ++ ) if ( occ . ContainsKey ( seq [ i ] ) ) occ [ seq [ i ] ] = occ [ seq [ i ] ] + 1 ; else occ . Add ( seq [ i ] , 1 ) ; int first_max = int . MinValue , sec_max = int . MinValue ; foreach ( KeyValuePair < String , int > entry in occ ) { int v = entry . Value ; if ( v > first_max ) { sec_max = first_max ; first_max = v ; } else if ( v > sec_max && v != first_max ) sec_max = v ; } foreach ( KeyValuePair < String , int > entry in occ ) { int v = entry . Value ; if ( v == sec_max ) return entry . Key ; } return null ; } public static void Main ( String [ ] args ) { String [ ] arr = { " ccc " , " aaa " , " ccc " , " ddd " , " aaa " , " aaa " } ; List < String > seq = new List < String > ( arr ) ; Console . WriteLine ( secMostRepeated ( seq ) ) ; } }


"Second | C # implementation to find N - th term in the series ; Function to find N - th term in the series ; Driver code"
Solution in C#:

using System ; class GFG { static void findNthTerm ( int n ) { Console . Write ( Math . Pow ( 2 , n ) - 2 * n ) ; } public static void Main ( ) { int N = 4 ; findNthTerm ( N ) ; } }


"Secretary Problem ( A Optimal Stopping Problem ) | C # Program to test 1 / e law for Secretary Problem ; To find closest integer of num . ; Finds best candidate using n / e rule . candidate [ ] represents talents of n candidates . ; Calculating sample size for benchmarking . ; Finding best candidate in sample size ; Finding the first best candidate that is better than benchmark set . ; Driver Code ; n = 8 candidates and candidate array contains talents of n candidate where the largest number means highest talented candidate . ; generating random numbers between 1 to 8 for talent of candidate"
Solution in C#:

 using System ; class GFG { static double e = 2.71828 ; static int roundNo ( float num ) { return ( int ) ( num < 0 ? num - 0.5 : num + 0.5 ) ; } static void printBestCandidate ( int [ ] candidate , int n ) { int sample_size = roundNo ( ( float ) ( n / e ) ) ; Console . WriteLine ( "

 Sample ▁ size ▁ is ▁ " + sample_size ) ; int best = 0 ; for ( int i = 1 ; i < sample_size ; i ++ ) if ( candidate [ i ] > candidate [ best ] ) best = i ; for ( int i = sample_size ; i < n ; i ++ ) if ( candidate [ i ] >= candidate [ best ] ) { best = i ; break ; } if ( best >= sample_size ) Console . WriteLine ( " found is " ( best + 1 ) + " ▁ with ▁ talent ▁ " + candidate [ best ] ) ; else Console . Write ( " Couldn ' t ▁ find ▁ a ▁ best ▁ candidate
 " ) ; } public static void Main ( String [ ] args ) { int n = 8 ; int [ ] candidate = new int [ n ] ; Random rand = new Random ( ) ; for ( int i = 0 ; i < n ; i ++ ) candidate [ i ] = 1 + rand . Next ( 1 , 8 ) ; Console . Write ( " Candidate ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( i + 1 + " ▁ " ) ; Console . WriteLine ( ) ; Console . Write ( " Talents ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( candidate [ i ] + " ▁ " ) ; printBestCandidate ( candidate , n ) ; } }


"Section formula for 3 D | C # program to find point that divides given line in given ratio in 3D . ; Function to find the section of the line ; Applying section formula ; Printing result ; Driver code"
Solution in C#:

using System ; class GFG { static void section ( double x1 , double x2 , double y1 , double y2 , double z1 , double z2 , double m , double n ) { double x = ( ( m * x2 ) + ( n * x1 ) ) / ( m + n ) ; double y = ( ( m * y2 ) + ( n * y1 ) ) / ( m + n ) ; double z = ( ( m * z2 ) + ( n * z1 ) ) / ( m + n ) ; Console . Write ( " ( " + x + " , ▁ " ) ; Console . Write ( y + " , ▁ " ) ; Console . WriteLine ( z + " ) " ) ; } static public void Main ( ) { double x1 = 2 , x2 = 4 , y1 = - 1 , y2 = 3 , z1 = 4 , z2 = 2 , m = 2 , n = 3 ; section ( x1 , x2 , y1 , y2 , z1 , z2 , m , n ) ; } }


"Segmented Sieve | C # program to print print all primes smaller than n using segmented sieve ; This methid finds all primes smaller than ' limit ' using simple sieve of eratosthenes . It also stores found primes in vector prime [ ] ; Create a boolean array " mark [ 0 . . n - 1 ] " and initialize all entries of it as true . A value in mark [ p ] will finally be false if ' p ' is Not a prime , else true . ; If p is not changed , then it is a prime ; Update all multiples of p ; Print all prime numbers and store them in prime ; Prints all prime numbers smaller than ' n ' ; Compute all primes smaller than or equal to square root of n using simple sieve ; Divide the range [ 0. . n - 1 ] in different segments We have chosen segment size as sqrt ( n ) . ; While all segments of range [ 0. . n - 1 ] are not processed , process one segment at a time ; To mark primes in current range . A value in mark [ i ] will finally be false if ' i - low ' is Not a prime , else true . ; Use the found primes by simpleSieve ( ) to find primes in current range ; Find the minimum number in [ low . . high ] that is a multiple of prime . get ( i ) ( divisible by prime . get ( i ) ) For example , if low is 31 and prime . get ( i ) is 3 , we start with 33. ; Mark multiples of prime . get ( i ) in [ low . . high ] : We are marking j - low for j , i . e . each number in range [ low , high ] is mapped to [ 0 , high - low ] so if range is [ 50 , 100 ] marking 50 corresponds to marking 0 , marking 51 corresponds to 1 and so on . In this way we need to allocate space only for range ; Numbers which are not marked as false are prime ; Update low and high for next segment ; Driver code"
Solution in C#:

using System ; using System . Collections ; class GFG { static void simpleSieve ( int limit , ArrayList prime ) { bool [ ] mark = new bool [ limit + 1 ] ; for ( int i = 0 ; i < mark . Length ; i ++ ) mark [ i ] = true ; for ( int p = 2 ; p * p < limit ; p ++ ) { if ( mark [ p ] == true ) { for ( int i = p * p ; i < limit ; i += p ) mark [ i ] = false ; } } for ( int p = 2 ; p < limit ; p ++ ) { if ( mark [ p ] == true ) { prime . Add ( p ) ; Console . Write ( p + " ▁ " ) ; } } } static void segmentedSieve ( int n ) { int limit = ( int ) ( Math . Floor ( Math . Sqrt ( n ) ) + 1 ) ; ArrayList prime = new ArrayList ( ) ; simpleSieve ( limit , prime ) ; int low = limit ; int high = 2 * limit ; while ( low < n ) { if ( high >= n ) high = n ; bool [ ] mark = new bool [ limit + 1 ] ; for ( int i = 0 ; i < mark . Length ; i ++ ) mark [ i ] = true ; for ( int i = 0 ; i < prime . Count ; i ++ ) { int loLim = ( ( int ) Math . Floor ( ( double ) ( low / ( int ) prime [ i ] ) ) * ( int ) prime [ i ] ) ; if ( loLim < low ) loLim += ( int ) prime [ i ] ; for ( int j = loLim ; j < high ; j += ( int ) prime [ i ] ) mark [ j - low ] = false ; } for ( int i = low ; i < high ; i ++ ) if ( mark [ i - low ] == true ) Console . Write ( i + " ▁ " ) ; low = low + limit ; high = high + limit ; } } static void Main ( ) { int n = 100 ; Console . WriteLine ( " Primes ▁ smaller ▁ than ▁ " + n + " : " ) ; segmentedSieve ( n ) ; } }


"Segmented Sieve | This functions finds all primes smaller than ' limit ' using simple sieve of eratosthenes . ; Create a boolean array " mark [ 0 . . limit - 1 ] " and initialize all entries of it as true . A value in mark [ p ] will finally be false if ' p ' is Not a prime , else true . ; One by one traverse all numbers so that their multiples can be marked as composite . ; If p is not changed , then it is a prime ; Update all multiples of p ; Print all prime numbers and store them in prime"
Solution in C#:

static void simpleSieve ( int limit ) { bool [ ] mark = new bool [ limit ] ; Array . Fill ( mark , true ) ; for ( int p = 2 ; p * p < limit ; p ++ ) { if ( mark [ p ] == true ) { for ( int i = p * p ; i < limit ; i += p ) mark [ i ] = false ; } } for ( int p = 2 ; p < limit ; p ++ ) if ( mark [ p ] == true ) Console . Write ( p + " ▁ " ) ; }


"Segregate 1 s and 0 s in separate halves of a Binary String | C # program for the above approach ; Function to count the minimum number of operations required to segregate all 1 s and 0 s in a binary string ; Stores the count of unequal pair of adjacent characters ; If an unequal pair of adjacent characters occurs ; For odd count ; For even count ; Driver Code ; Given string ; Length of the string ; Prints the minimum count of operations required"
Solution in C#:

using System ; public class GFG { static void minOps ( String s , int N ) { int ans = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( s [ i ] != s [ i - 1 ] ) { ans ++ ; } } if ( ans % 2 == 1 ) { Console . Write ( ( ans - 1 ) / 2 ) ; return ; } Console . Write ( ans / 2 ) ; } public static void Main ( String [ ] args ) { String str = "01011100" ; int N = str . Length ; minOps ( str , N ) ; } }


"Segregate even and odd numbers | Set 3 | C # code to segregate even odd numbers in an array ; Function to segregate even odd numbers ; Swapping even and odd numbers ; Printing segregated array ; Driver code"
Solution in C#:

using System ; class GFG { static void arrayEvenAndOdd ( int [ ] arr , int n ) { int i = - 1 , j = 0 ; while ( j != n ) { if ( arr [ j ] % 2 == 0 ) { i ++ ; int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } j ++ ; } for ( int k = 0 ; k < n ; k ++ ) Console . Write ( arr [ k ] + " ▁ " ) ; } static void Main ( ) { int [ ] arr = { 1 , 3 , 2 , 4 , 7 , 6 , 9 , 10 } ; int n = arr . Length ; arrayEvenAndOdd ( arr , n ) ; } }


"Select a Random Node from a tree with equal probability | C # program to Select a Random Node from a tree ; This is used to fill children counts . ; Inserts Children count for each node ; returns number of children for root ; Helper Function to return a random node ; Returns Random node ; Driver Code ; Creating Above Tree"
Solution in C#:

using System ; class GFG { class Node { public int data ; public int children ; public Node left , right ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; temp . children = 0 ; return temp ; } static int getElements ( Node root ) { if ( root == null ) return 0 ; return getElements ( root . left ) + getElements ( root . right ) + 1 ; } static Node insertChildrenCount ( Node root ) { if ( root == null ) return null ; root . children = getElements ( root ) - 1 ; root . left = insertChildrenCount ( root . left ) ; root . right = insertChildrenCount ( root . right ) ; return root ; } static int children ( Node root ) { if ( root == null ) return 0 ; return root . children + 1 ; } static int randomNodeUtil ( Node root , int count ) { if ( root == null ) return 0 ; if ( count == children ( root . left ) ) return root . data ; if ( count < children ( root . left ) ) return randomNodeUtil ( root . left , count ) ; return randomNodeUtil ( root . right , count - children ( root . left ) - 1 ) ; } static int randomNode ( Node root ) { int count = ( int ) new Random ( ) . Next ( 0 , root . children + 1 ) ; return randomNodeUtil ( root , count ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 20 ) ; root . right = newNode ( 30 ) ; root . left . right = newNode ( 40 ) ; root . left . right = newNode ( 50 ) ; root . right . left = newNode ( 60 ) ; root . right . right = newNode ( 70 ) ; insertChildrenCount ( root ) ; Console . Write ( " A ▁ Random ▁ Node ▁ From ▁ Tree ▁ : ▁ " + randomNode ( root ) ) ; } }


"Selection Sort VS Bubble Sort |  ; Driver code"
Solution in C#:

using System ; public class GFG { static void Selection_Sort ( int [ ] arr , int n ) { for ( int i = 0 ; i < n - 1 ; ++ i ) { int min_index = i ; for ( int j = i + 1 ; j < n ; ++ j ) { if ( arr [ j ] < arr [ min_index ] ) min_index = j ; } int temp = arr [ i ] ; arr [ i ] = arr [ min_index ] ; arr [ min_index ] = temp ; } } public static void Main ( String [ ] args ) { int n = 5 ; int [ ] arr = { 2 , 0 , 1 , 4 , 3 } ; Selection_Sort ( arr , n ) ; Console . Write ( " The ▁ Sorted ▁ Array ▁ by ▁ using ▁ Selection ▁ Sort ▁ is ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; ++ i ) Console . Write ( arr [ i ] + " ▁ " ) ; } }


"Selection Sort VS Bubble Sort | C # program for the above approach ; Driver Code"
Solution in C#:

using System ; public class GFG { static void Bubble_Sort ( int [ ] arr , int n ) { for ( int i = 1 ; i < n ; ++ i ) { for ( int j = 0 ; j <= ( n - i - 1 ) ; ++ j ) { if ( arr [ j ] > arr [ j + 1 ] ) { int temp = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; } } } } static public void Main ( ) { int n = 5 ; int [ ] arr = { 2 , 0 , 1 , 4 , 3 } ; Bubble_Sort ( arr , n ) ; Console . Write ( " The ▁ Sorted ▁ Array ▁ by ▁ using ▁ Bubble ▁ Sort ▁ is ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; ++ i ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } }


"Selection Sort VS Bubble Sort | C # program for the above approach ; Function for bubble sort ; Iterate from 1 to n - 1 ; Iterate from 0 to n - i - 1 ; Driver Code"
Solution in C#:

using System ; public class GFG { static void Bubble_Sort ( int [ ] arr , int n ) { bool flag ; for ( int i = 1 ; i < n ; ++ i ) { flag = false ; for ( int j = 0 ; j <= ( n - i - 1 ) ; ++ j ) { if ( arr [ j ] > arr [ j + 1 ] ) { int temp = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; flag = true ; } } if ( flag == false ) break ; } } static public void Main ( ) { int n = 5 ; int [ ] arr = { 2 , 0 , 1 , 4 , 3 } ; Bubble_Sort ( arr , n ) ; Console . Write ( " The ▁ Sorted ▁ Array ▁ by ▁ using ▁ Bubble ▁ Sort ▁ is ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; ++ i ) Console . Write ( arr [ i ] + " ▁ " ) ; } }


"Self Numbers | C # implementation to check if the number is a self number or not ; Function to find the sum of digits of a number N ; Function to check for Self number ; Driver code"
Solution in C#:

using System ; class GFG { static int getSum ( int n ) { int sum = 0 ; while ( n != 0 ) { sum = sum + n % 10 ; n = n / 10 ; } return sum ; } static bool isSelfNum ( int n ) { for ( int m = 1 ; m <= n ; m ++ ) { if ( m + getSum ( m ) == n ) return false ; } return true ; } public static void Main ( ) { int n = 20 ; if ( isSelfNum ( n ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }


"Semiperfect Number | C # program to check if the number is semi - perfect or not ; Code to find all the factors of the number excluding the number itself ; vector to store the factors ; note that this loop runs till Math . Sqrt ( n ) ; if the value of i is a factor ; condition to check the divisor is not the number itself ; return the vector ; Function to check if the number is semi - perfect or not ; find the divisors ; sorting the vector ; subset to check if no is semiperfect ; initialising 1 st column to true ; initializing 1 st row except zero position to 0 ; loop to find whether the number is semiperfect ; calculation to check if the number can be made by summation of divisors ; If not possible to make the number by any combination of divisors ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < int > factors ( int n ) { List < int > v = new List < int > ( ) ; v . Add ( 1 ) ; for ( int i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v . Add ( i ) ; if ( n / i != i ) { v . Add ( n / i ) ; } } } return v ; } static bool check ( int n ) { List < int > v = new List < int > ( ) ; v = factors ( n ) ; v . Sort ( ) ; int r = v . Count ; bool [ , ] subset = new bool [ r + 1 , n + 1 ] ; for ( int i = 0 ; i <= r ; i ++ ) subset [ i , 0 ] = true ; for ( int i = 1 ; i <= n ; i ++ ) subset [ 0 , i ] = false ; for ( int i = 1 ; i <= r ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( j < v [ i - 1 ] ) subset [ i , j ] = subset [ i - 1 , j ] ; else { subset [ i , j ] = subset [ i - 1 , j ] || subset [ i - 1 , ( j - v [ i - 1 ] ) ] ; } } } if ( ( subset [ r , n ] ) == false ) return false ; else return true ; } public static void Main ( String [ ] args ) { int n = 40 ; if ( check ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Sentence Palindrome ( Palindrome after removing spaces , dots , . . etc ) | C # program to find if a sentence is palindrome ; To check sentence is palindrome or not ; Lowercase string ; Compares character until they are equal ; If there is another symbol in left of sentence ; If there is another symbol in right of sentence ; If characters are equal ; If characters are not equal then sentence is not palindrome ; Returns true if sentence is palindrome ; Driver Code"
Solution in C#:

using System ; public class GFG { static bool sentencePalindrome ( String str ) { int l = 0 ; int h = str . Length - 1 ; str = str . ToLower ( ) ; while ( l <= h ) { char getAtl = str [ l ] ; char getAth = str [ h ] ; if ( ! ( getAtl >= ' a ' && getAtl <= ' z ' ) ) l ++ ; else if ( ! ( getAth >= ' a ' && getAth <= ' z ' ) ) h -- ; else if ( getAtl == getAth ) { l ++ ; h -- ; } else return false ; } return true ; } public static void Main ( ) { String str = " Too ▁ hot ▁ to ▁ hoot . " ; if ( sentencePalindrome ( str ) ) Console . Write ( " Sentence ▁ is ▁ palindrome " ) ; else Console . Write ( " Sentence ▁ is ▁ not " + " ▁ " + " palindrome " ) ; } }


"Sequence Alignment problem | C # program to implement sequence alignment problem . ; function to find out the minimum penalty ; table for storing optimal substructure answers ; initialising the table ; calculating the minimum penalty ; Reconstructing the solution int l = n + m ; maximum possible length ; Final answers for the respective strings ; Since we have assumed the answer to be n + m long , we need to remove the extra gaps in the starting id represents the index from which the arrays xans , yans are useful ; Printing the final answer ; Driver code ; input strings ; initialising penalties of different types ; calling the function to calculate the result"
Solution in C#:

 using System ; class GFG { public static void getMinimumPenalty ( string x , string y , int pxy , int pgap ) { int [ , ] dp = new int [ n + m + 1 , n + m + 1 ] ; for ( int q = 0 ; q < n + m + 1 ; q ++ ) for ( int w = 0 ; w < n + m + 1 ; w ++ ) dp [ q , w ] = 0 ; for ( i = 0 ; i <= ( n + m ) ; i ++ ) { dp [ i , 0 ] = i * pgap ; dp [ 0 , i ] = i * pgap ; } for ( i = 1 ; i <= m ; i ++ ) { for ( j = 1 ; j <= n ; j ++ ) { if ( x [ i - 1 ] == y [ j - 1 ] ) { dp [ i , j ] = dp [ i - 1 , j - 1 ] ; } else { dp [ i , j ] = Math . Min ( Math . Min ( dp [ i - 1 , j - 1 ] + pxy , dp [ i - 1 , j ] + pgap ) , dp [ i , j - 1 ] + pgap ) ; } } } i = m ; j = n ; int xpos = l ; int ypos = l ; int [ ] xans = new int [ l + 1 ] ; int [ ] yans = new int [ l + 1 ] ; while ( ! ( i == 0 j == 0 ) ) { if ( x [ i - 1 ] == y [ j - 1 ] ) { xans [ xpos -- ] = ( int ) x [ i - 1 ] ; yans [ ypos -- ] = ( int ) y [ j - 1 ] ; i -- ; j -- ; } else if ( dp [ i - 1 , j - 1 ] + pxy == dp [ i , j ] ) { xans [ xpos -- ] = ( int ) x [ i - 1 ] ; yans [ ypos -- ] = ( int ) y [ j - 1 ] ; i -- ; j -- ; } else if ( dp [ i - 1 , j ] + pgap == dp [ i , j ] ) { xans [ xpos -- ] = ( int ) x [ i - 1 ] ; yans [ ypos -- ] = ( int ) ' _ ' ; i -- ; } else if ( dp [ i , j - 1 ] + pgap == dp [ i , j ] ) { xans [ xpos -- ] = ( int ) ' _ ' ; yans [ ypos -- ] = ( int ) y [ j - 1 ] ; j -- ; } } while ( xpos > 0 ) { if ( i > 0 ) xans [ xpos -- ] = ( int ) x [ -- i ] ; else xans [ xpos -- ] = ( int ) ' _ ' ; } while ( ypos > 0 ) { if ( j > 0 ) yans [ ypos -- ] = ( int ) y [ -- j ] ; else yans [ ypos -- ] = ( int ) ' _ ' ; } int id = 1 ; for ( i = l ; i >= 1 ; i -- ) { if ( ( char ) yans [ i ] == ' _ ' && ( char ) xans [ i ] == ' _ ' ) { id = i + 1 ; break ; } } Console . Write ( " Minimum ▁ Penalty ▁ in ▁ aligning ▁ the ▁ genes ▁ = ▁ " + dp [ m , n ] + "
 " ) ; Console . Write ( " The ▁ aligned ▁ genes ▁ are ▁ :
 " ) ; for ( i = id ; i <= l ; i ++ ) { Console . Write ( ( char ) xans [ i ] ) ; } Console . Write ( "
 " ) ; for ( i = id ; i <= l ; i ++ ) { Console . Write (...


"Sequence with sum K and minimum sum of absolute differences between consecutive elements | C # implementation of the approach ; Function to return the minimized sum ; If k is divisible by n then the answer will be 0 ; Else the answer will be 1 ; Driver code"
Solution in C#:

using System ; class GFG { static int minimum_sum ( int n , int k ) { if ( k % n == 0 ) return 0 ; return 1 ; } static public void Main ( ) { int n = 3 , k = 56 ; Console . Write ( minimum_sum ( n , k ) ) ; } }


"Sequences of given length where every element is more than or equal to twice of previous | C # program to count total number of special sequences of length n where every element is more than or equal to twice of previous ; Recursive function to find the number of special sequences ; A special sequence cannot exist if length n is more than the maximum value m . ; If n is 0 , found an empty special sequence ; There can be two possibilities : ( 1 ) Reduce last element value ( 2 ) Consider last element as m and reduce number of terms ; Driver code"
Solution in C#:

using System ; class GFG { static int getTotalNumberOfSequences ( int m , int n ) { if ( m < n ) return 0 ; if ( n == 0 ) return 1 ; return getTotalNumberOfSequences ( m - 1 , n ) + getTotalNumberOfSequences ( m / 2 , n - 1 ) ; } public static void Main ( ) { int m = 10 ; int n = 4 ; Console . Write ( " Total ▁ number ▁ of ▁ possible ▁ sequences ▁ " + getTotalNumberOfSequences ( m , n ) ) ; } }


"Sequences of given length where every element is more than or equal to twice of previous | Efficient C # program to count total number of special sequences of length n where ; DP based function to find the number of special sequences ; define T and build in bottom manner to store number of special sequences of length n and maximum value m ; Base case : If length of sequence is 0 or maximum value is 0 , there cannot exist any special sequence ; if length of sequence is more than the maximum value , special sequence cannot exist ; If length of sequence is 1 then the number of special sequences is equal to the maximum value For example with maximum value 2 and length 1 , there can be 2 special sequences { 1 } , { 2 } ; otherwise calculate ; Driver Code"
Solution in C#:

using System ; class Sequences { static int getTotalNumberOfSequences ( int m , int n ) { int [ , ] T = new int [ m + 1 , n + 1 ] ; for ( int i = 0 ; i < m + 1 ; i ++ ) { for ( int j = 0 ; j < n + 1 ; j ++ ) { if ( i == 0 j == 0 ) T [ i , j ] = 0 ; else if ( i < j ) T [ i , j ] = 0 ; else if ( j == 1 ) T [ i , j ] = i ; else T [ i , j ] = T [ i - 1 , j ] + T [ i / 2 , j - 1 ] ; } } return T [ m , n ] ; } public static void Main ( ) { int m = 10 ; int n = 4 ; Console . WriteLine ( " Total ▁ number ▁ of ▁ possible ▁ sequences ▁ " + getTotalNumberOfSequences ( m , n ) ) ; } }


"Series summation if T ( n ) is given and n is very large | C # implementation of the approach ; Function to return the sum of the given series ; Driver code"
Solution in C#:

using System ; class GFG { const int MOD = 1000000007 ; static int sumOfSeries ( int n ) { int ans = ( int ) Math . Pow ( n % MOD , 2 ) ; return ( ans % MOD ) ; } public static void Main ( ) { int n = 10 ; Console . Write ( sumOfSeries ( n ) ) ; } }


"Series with largest GCD and sum equals to n | C # program to find the series with largest GCD and sum equals to n ; function to generate and print the sequence ; stores the maximum gcd that can be possible of sequence . ; if maximum gcd comes out to be zero then not possible ; the smallest gcd possible is 1 ; traverse the array to find out the max gcd possible ; checks if the number is divisible or not ; checks if x is smaller then the max gcd possible and x is greater then the resultant gcd till now , then r = x ; checks if n / x is smaller than the max gcd possible and n / x is greater then the resultant gcd till now , then r = x ; traverses and prints d , 2d , 3d , ... , ( k - 1 ) ; computes the last element of the sequence n - s . ; prints the last element ; Driver Code"
Solution in C#:

using System ; class GFG { static void print_sequence ( int n , int k ) { int b = n / ( k * ( k + 1 ) / 2 ) ; if ( b == 0 ) { Console . Write ( " - 1" ) ; } else { int r = 1 ; for ( int x = 1 ; x * x <= n ; x ++ ) { if ( n % x != 0 ) continue ; if ( x <= b && x > r ) r = x ; if ( n / x <= b && n / x > r ) r = n / x ; } for ( int i = 1 ; i < k ; i ++ ) Console . Write ( r * i + " ▁ " ) ; int res = n - ( r * ( k * ( k - 1 ) / 2 ) ) ; Console . WriteLine ( res ) ; } } public static void Main ( ) { int n = 24 ; int k = 4 ; print_sequence ( n , k ) ; n = 24 ; k = 5 ; print_sequence ( n , k ) ; n = 6 ; k = 4 ; print_sequence ( n , k ) ; } }


"Set all even bits of a number | Efficient C # program to set all even bits of a number ; return msb set number ; set all bits ; return msb increment n by 1 and shift by 1 ; return even seted number ; get msb here ; generate even bits like 101010. . ; if bits is odd then shift by 1 ; return even set bits number ; set all even bits here ; take or with even set bits number ; Driver Code"
Solution in C#:

using System ; class GFG { static int getmsb ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return ( n + 1 ) >> 1 ; } static int getevenbits ( int n ) { n = getmsb ( n ) ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; if ( ( n & 1 ) == 1 ) n = n >> 1 ; return n ; } static int setallevenbits ( int n ) { return n | getevenbits ( n ) ; } public static void Main ( ) { int n = 10 ; Console . WriteLine ( setallevenbits ( n ) ) ; } }


"Set all even bits of a number | Simple C # program to set all even bits of a number ; Sets even bits of n and returns modified number . ; Generate 101010. . .10 number and store in res . ; if bit is even then generate number and or with res ; return OR number ; Driver code"
Solution in C#:

using System ; class GFG { static int evenbitsetnumber ( int n ) { int count = 0 , res = 0 ; for ( int temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 1 ) res |= ( 1 << count ) ; count ++ ; } return ( n res ) ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( evenbitsetnumber ( n ) ) ; } }


"Set all odd bits of a number | C # code to Set all odd bits of a number ; set all odd bit ; res for store 010101. . number ; generate number form of 010101. . ... till temp size ; if bit is odd , then generate number and or with res ; Driver Code"
Solution in C#:

using System ; class GFG { static int oddbitsetnumber ( int n ) { int count = 0 ; int res = 0 ; for ( int temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 0 ) res |= ( 1 << count ) ; count ++ ; } return ( n res ) ; } static public void Main ( ) { int n = 10 ; Console . WriteLine ( oddbitsetnumber ( n ) ) ; } }


"Set all odd bits of a number | Efficient C # program to set all odd bits number ; return MSB set number ; set all bits including MSB . ; return MSB ; Returns a number of same size ( MSB at same position ) as n and all odd bits set . ; generate odd bits like 010101. . ; if bits is even then shift by 1 ; return odd set bits number ; set all odd bits here ; take OR with odd set bits number ; Driver Code"
Solution in C#:

using System ; class GFG { static int getmsb ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return ( n + 1 ) >> 1 ; } static int getevenbits ( int n ) { n = getmsb ( n ) ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; if ( ( n & 1 ) == 0 ) n = n >> 1 ; return n ; } static int setalloddbits ( int n ) { return n | getevenbits ( n ) ; } static public void Main ( ) { int n = 10 ; Console . WriteLine ( setalloddbits ( n ) ) ; } }


"Set all the bits in given range of a number | C # implementation to Set bits in the given range ; function to toggle bits in the given range ; calculating a number ' range ' having set bits in the range from l to r and all other bits as 0 ( or unset ) . ; Driver code"
Solution in C#:

using System ; class GFG { static int setallbitgivenrange ( int n , int l , int r ) { int range = ( ( ( 1 << ( l - 1 ) ) - 1 ) ^ ( ( 1 << ( r ) ) - 1 ) ) ; return ( n range ) ; } static void Main ( ) { int n = 17 , l = 2 , r = 3 ; Console . Write ( setallbitgivenrange ( n , l , r ) ) ; } }


"Set the K | C # implementation to set the kth bit of the given number ; function to set the kth bit ; kth bit of n is being set by this operation ; Driver code"
Solution in C#:

using System ; class GFG { static int setKthBit ( int n , int k ) { return ( ( 1 << k ) n ) ; } public static void Main ( ) { int n = 10 , k = 2 ; Console . Write ( " Kth ▁ bit ▁ set ▁ number ▁ = ▁ " + setKthBit ( n , k ) ) ; } }


"Set the Left most unset bit | C # program to set the leftmost unset bit ; set left most unset bit ; if number contain all 1 then return n ; Find position of leftmost unset bit . ; if temp L . S . B is zero then unset bit pos is change ; return OR of number and unset bit pos ; Driver Function"
Solution in C#:

using System ; class GFG { static int setleftmostunsetbit ( int n ) { if ( ( n & ( n + 1 ) ) == 0 ) return n ; int pos = 0 ; for ( int temp = n , count = 0 ; temp > 0 ; temp >>= 1 , count ++ ) if ( ( temp & 1 ) == 0 ) pos = count ; return ( n | ( 1 << ( pos ) ) ) ; } public static void Main ( ) { int n = 10 ; Console . WriteLine ( setleftmostunsetbit ( n ) ) ; } }


"Set the rightmost off bit | C # program to set the rightmost unset bit ; If all bits are set ; Set rightmost 0 bit ; Driver program to test above"
Solution in C#:

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


"Set the rightmost unset bit | C # implementation to set the rightmost unset bit ; Function to find the position of rightmost set bit ; If n = 0 , return 1 ; If all bits of ' n ' are set ; Position of rightmost unset bit in ' n ' passing ~ n as argument ; Set the bit at position ' pos ' ; Driver code"
Solution in C#:

using System ; class GFG { static int getPosOfRightmostSetBit ( int n ) { return ( int ) ( ( Math . Log10 ( n & - n ) ) / ( Math . Log10 ( 2 ) ) ) + 1 ; } static int setRightmostUnsetBit ( int n ) { if ( n == 0 ) return 1 ; if ( ( n & ( n + 1 ) ) == 0 ) return n ; int pos = getPosOfRightmostSetBit ( ~ n ) ; return ( ( 1 << ( pos - 1 ) ) n ) ; } public static void Main ( String [ ] arg ) { int n = 21 ; Console . Write ( setRightmostUnsetBit ( n ) ) ; } }


"Shell | C # implementation of Shell - Metzner Sort ; Function to swap two elements ; Function to sort arr [ ] using Shell Metzner sort ; Declare variables ; Set initial step size to the size of the array ; Step size decreases by half each time ; k is the upper limit for j ; j is the starting point ; i equals to smaller value ; l equals to larger value ; Compare and swap arr [ i ] with arr [ l ] ; Decrease smaller value by step size ; Increment the lower limit of i ; Function to print the contents of an array ; Driver code ; Sort the array using Shell Metzner Sort ; Print the sorted array"
Solution in C#:

using System ; class GFG { static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } static void sort_shell_metzner ( int [ ] arr , int n ) { int i , j , k , l , m , temp ; m = n ; while ( m > 0 ) { m /= 2 ; k = n - m ; j = 0 ; do { i = j ; do { l = i + m ; if ( l < n && arr [ i ] > arr [ l ] ) { swap ( arr , i , l ) ; i -= m ; } else { break ; } } while ( i >= 0 ) ; j ++ ; } while ( j <= k ) ; } } static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , - 2 , 8 , 5 , 1 } ; int n = arr . Length ; sort_shell_metzner ( arr , n ) ; printArray ( arr , n ) ; } }


"ShellSort | C # implementation of ShellSort ; An utility function to print array of size n ; function to sort arr using shellSort ; Start with a big gap , then reduce the gap ; Do a gapped insertion sort for this gap size . The first gap elements a [ 0. . gap - 1 ] are already in gapped order keep adding one more element until the entire array is gap sorted ; add a [ i ] to the elements that have been gap sorted save a [ i ] in temp and make a hole at position i ; shift earlier gap - sorted elements up until the correct location for a [ i ] is found ; put temp ( the original a [ i ] ) in its correct location ; Driver method"
Solution in C#:

 using System ; class ShellSort { static void printArray ( int [ ] arr ) { int n = arr . Length ; for ( int i = 0 ; i < n ; ++ i ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } int sort ( int [ ] arr ) { int n = arr . Length ; for ( int gap = n / 2 ; gap > 0 ; gap /= 2 ) { for ( int i = gap ; i < n ; i += 1 ) { int temp = arr [ i ] ; int j ; for ( j = i ; j >= gap && arr [ j - gap ] > temp ; j -= gap ) arr [ j ] = arr [ j - gap ] ; arr [ j ] = temp ; } } return 0 ; } public static void Main ( ) { int [ ] arr = { 12 , 34 , 54 , 2 , 3 } ; Console . Write ( " Array ▁ before ▁ sorting ▁ :
 " ) ; printArray ( arr ) ; ShellSort ob = new ShellSort ( ) ; ob . sort ( arr ) ; Console . Write ( " Array ▁ after ▁ sorting ▁ :
 " ) ; printArray ( arr ) ; } }


"Shortest Common Supersequence | A Naive recursive C # program to find length of the shortest supersequence ; Driver Code"
Solution in C#:

using System ; class GFG { static int superSeq ( String X , String Y , int m , int n ) { if ( m == 0 ) return n ; if ( n == 0 ) return m ; if ( X [ m - 1 ] == Y [ n - 1 ] ) return 1 + superSeq ( X , Y , m - 1 , n - 1 ) ; return 1 + Math . Min ( superSeq ( X , Y , m - 1 , n ) , superSeq ( X , Y , m , n - 1 ) ) ; } public static void Main ( ) { String X = " AGGTAB " ; String Y = " GXTXAYB " ; Console . WriteLine ( " Length ▁ of ▁ the ▁ shortest ▁ supersequence ▁ is : ▁ " + superSeq ( X , Y , X . Length , Y . Length ) ) ; } }


"Shortest Common Supersequence | A dynamic programming based C # program to find length of the shortest supersequence ; Returns length of the shortest supersequence of X and Y ; Fill table in bottom up manner ; Below steps follow above recurrence ; Driver code"
Solution in C#:

using System ; class GFG { static int superSeq ( String X , String Y , int m , int n ) { int [ , ] dp = new int [ m + 1 , n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 ) dp [ i , j ] = j ; else if ( j == 0 ) dp [ i , j ] = i ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) dp [ i , j ] = 1 + dp [ i - 1 , j - 1 ] ; else dp [ i , j ] = 1 + Math . Min ( dp [ i - 1 , j ] , dp [ i , j - 1 ] ) ; } } return dp [ m , n ] ; } public static void Main ( ) { String X = " AGGTAB " ; String Y = " GXTXAYB " ; Console . WriteLine ( " Length ▁ of ▁ the ▁ shortest ▁ supersequence ▁ is ▁ " + superSeq ( X , Y , X . Length , Y . Length ) ) ; } }


"Shortest Palindromic Substring | C # program to find the shortest palindromic subString ; Function return the shortest palindromic subString ; Finding the smallest character present in the String ; Driver code"
Solution in C#:

using System ; class GFG { static char ShortestPalindrome ( String s ) { int n = s . Length ; char ans = s [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { ans = ( char ) Math . Min ( ans , s [ i ] ) ; } return ans ; } public static void Main ( String [ ] args ) { String s = " geeksforgeeks " ; Console . Write ( ShortestPalindrome ( s ) ) ; } }


"Shortest Palindromic Substring | C # program to find the shortest palindromic substring ; Function return the shortest palindromic substring ; One by one consider every character as center point of even and length palindromes ; Find the longest odd length palindrome with center point as i ; Find the even length palindrome with center points as i - 1 and i . ; Smallest substring which is not empty ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static string ShortestPalindrome ( string s ) { int n = s . Length ; List < string > v = new List < string > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int l = i ; int r = i ; string ans1 = " " ; string ans2 = " " ; while ( l >= 0 && r < n && s [ l ] == s [ r ] ) { ans1 += s [ l ] ; l -- ; r ++ ; } l = i - 1 ; r = i ; while ( l >= 0 && r < n && s [ l ] == s [ r ] ) { ans2 += s [ l ] ; l -- ; r ++ ; } v . Add ( ans1 ) ; v . Add ( ans2 ) ; } string ans = v [ 0 ] ; for ( int i = 0 ; i < v . Count ; i ++ ) { if ( v [ i ] != " " ) { if ( ans [ 0 ] >= v [ i ] [ 0 ] ) { ans = v [ i ] ; } } } return ans ; } static public void Main ( ) { string s = " geeksforgeeks " ; Console . WriteLine ( ShortestPalindrome ( s ) ) ; } }


"Shortest Superstring Problem | C # program to find shortest superstring using Greedy Approximate Algorithm ; Utility function to calculate minimum of two numbers ; Function to calculate maximum overlap in two given strings ; max will store maximum overlap i . e maximum length of the matching prefix and suffix ; check suffix of str1 matches with prefix of str2 ; compare last i characters in str1 with first i characters in str2 ; Update max and str ; check prefix of str1 matches with suffix of str2 ; compare first i characters in str1 with last i characters in str2 ; pdate max and str ; Function to calculate smallest string that contains each string in the given set as substring . ; run len - 1 times to consider every pair ; To store maximum overlap ; To store array index of strings involved in maximum overlap ; to store resultant string after maximum overlap ; res will store maximum length of the matching prefix and suffix str is passed by reference and will store the resultant string after maximum overlap of arr [ i ] and arr [ j ] , if any . ; Check for maximum overlap ; Ignore last element in next cycle ; If no overlap , append arr [ len ] to arr [ 0 ] ; Copy resultant string to index l ; Copy string at last index to index r ; Driver Code"
Solution in C#:

using System ; class GFG { static String str ; static int min ( int a , int b ) { return ( a < b ) ? a : b ; } static int findOverlappingPair ( String str1 , String str2 ) { int max = Int32 . MinValue ; int len1 = str1 . Length ; int len2 = str2 . Length ; for ( int i = 1 ; i <= min ( len1 , len2 ) ; i ++ ) { if ( str1 . Substring ( len1 - i ) . CompareTo ( str2 . Substring ( 0 , i ) ) == 0 ) { if ( max < i ) { max = i ; str = str1 + str2 . Substring ( i ) ; } } } for ( int i = 1 ; i <= min ( len1 , len2 ) ; i ++ ) { if ( str1 . Substring ( 0 , i ) . CompareTo ( str2 . Substring ( len2 - i ) ) == 0 ) { if ( max < i ) { max = i ; str = str2 + str1 . Substring ( i ) ; } } } return max ; } static String findShortestSuperstring ( String [ ] arr , int len ) { while ( len != 1 ) { int max = Int32 . MinValue ; int l = 0 , r = 0 ; String resStr = " " ; for ( int i = 0 ; i < len ; i ++ ) { for ( int j = i + 1 ; j < len ; j ++ ) { int res = findOverlappingPair ( arr [ i ] , arr [ j ] ) ; if ( max < res ) { max = res ; resStr = str ; l = i ; r = j ; } } } len -- ; if ( max == Int32 . MinValue ) arr [ 0 ] += arr [ len ] ; else { arr [ l ] = resStr ; arr [ r ] = arr [ len ] ; } } return arr [ 0 ] ; } public static void Main ( String [ ] args ) { String [ ] arr = { " catgc " , " ctaagt " , " gcta " , " ttca " , " atgcatc " } ; int len = arr . Length ; Console . Write ( " The ▁ Shortest ▁ Superstring ▁ is ▁ " + findShortestSuperstring ( arr , len ) ) ; } }


"Shortest Un | Program to find the shortest subarray which is unsorted . ; boolean function to check array elements are in the increasing order or not ; boolean function to check array elements are in the decreasing order or not ; increasing and decreasing are two functions . function return true value then print 0 else 3 ; Driver program"
Solution in C#:

using System ; class GFG { public static bool increasing ( int [ ] a , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) if ( a [ i ] >= a [ i + 1 ] ) return false ; return true ; } public static bool decreasing ( int [ ] arr , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] < arr [ i + 1 ] ) return false ; return true ; } public static int shortestUnsorted ( int [ ] a , int n ) { if ( increasing ( a , n ) == true || decreasing ( a , n ) == true ) return 0 ; else return 3 ; } public static void Main ( ) { int [ ] ar = new int [ ] { 7 , 9 , 10 , 8 , 11 } ; int n = ar . Length ; Console . WriteLine ( shortestUnsorted ( ar , n ) ) ; } }


"Shortest Uncommon Subsequence | A dynamic programming based C # program to find shortest uncommon subsequence . ; Returns length of shortest common subsequence ; declaring 2D array of m + 1 rows and n + 1 columns dynamically ; T string is empty ; S string is empty ; char not present in T ; Driver code"
Solution in C#:

using System ; class GFG { static readonly int MAX = 1005 ; static int shortestSeq ( char [ ] S , char [ ] T ) { int m = S . Length , n = T . Length ; int [ , ] dp = new int [ m + 1 , n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { dp [ i , 0 ] = 1 ; } for ( int i = 0 ; i <= n ; i ++ ) { dp [ 0 , i ] = MAX ; } for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { char ch = S [ i - 1 ] ; int k ; for ( k = j - 1 ; k >= 0 ; k -- ) { if ( T [ k ] == ch ) { break ; } } if ( k == - 1 ) { dp [ i , j ] = 1 ; } else { dp [ i , j ] = Math . Min ( dp [ i - 1 , j ] , dp [ i - 1 , k ] + 1 ) ; } } } int ans = dp [ m , n ] ; if ( ans >= MAX ) { ans = - 1 ; } return ans ; } public static void Main ( String [ ] args ) { char [ ] S = " babab " . ToCharArray ( ) ; char [ ] T = " babba " . ToCharArray ( ) ; int m = S . Length , n = T . Length ; Console . WriteLine ( " Length ▁ of ▁ shortest " + " subsequence ▁ is ▁ : ▁ " + shortestSeq ( S , T ) ) ; } }


"Shortest Uncommon Subsequence | A simple recursive C # program to find shortest uncommon subsequence . ; A recursive function to find the length of shortest uncommon subsequence ; S String is empty ; T String is empty ; Loop to search for current character ; char not found in T ; Return minimum of following two Not including current char in answer Including current char ; Driver code"
Solution in C#:

 using System ; class GFG { static readonly int MAX = 1005 ; static int shortestSeq ( char [ ] S , char [ ] T , int m , int n ) { if ( m == 0 ) return MAX ; if ( n <= 0 ) return 1 ; int k ; for ( k = 0 ; k < n ; k ++ ) if ( T [ k ] == S [ 0 ] ) break ; if ( k == n ) return 1 ; char [ ] St1 = new Char [ S . Length - 1 ] ; Array . Copy ( S , 1 , St1 , 0 , S . Length - 1 ) ; char [ ] St2 = new Char [ S . Length - 1 ] ; Array . Copy ( S , 1 , St2 , 0 , S . Length - 1 ) ; char [ ] Tt1 = new Char [ T . Length - ( k + 1 ) ] ; Array . Copy ( T , k + 1 , Tt1 , 0 , T . Length - ( k + 1 ) ) ; return Math . Min ( shortestSeq ( St1 , T , m - 1 , n ) , 1 + shortestSeq ( St2 , Tt1 , m - 1 , n - k - 1 ) ) ; } public static void Main ( String [ ] args ) { char [ ] S = " babab " . ToCharArray ( ) ; char [ ] T = " babba " . ToCharArray ( ) ; int m = S . Length , n = T . Length ; int ans = shortestSeq ( S , T , m , n ) ; if ( ans >= MAX ) ans = - 1 ; Console . Write ( " Length ▁ of ▁ shortest ▁ subsequence ▁ is : ▁ " + ans + "
 " ) ; } }


"Shortest distance between a point and a circle | C # program to find the Shortest distance between a point and a circle ; Function to find the shortest distance ; Driver code"
Solution in C#:

using System ; class GFG { static void dist ( double x1 , double y1 , double x2 , double y2 , double r ) { Console . WriteLine ( " The ▁ shortest ▁ distance ▁ " + " between ▁ a ▁ point ▁ and ▁ a ▁ circle ▁ is ▁ " + ( Math . Sqrt ( ( Math . Pow ( ( x2 - x1 ) , 2 ) ) + ( Math . Pow ( ( y2 - y1 ) , 2 ) ) ) - r ) ) ; } public static void Main ( String [ ] args ) { double x1 = 4 , y1 = 6 , x2 = 35 , y2 = 42 , r = 5 ; dist ( x1 , y1 , x2 , y2 , r ) ; } }


"Shortest distance between two nodes in BST | C # program to find distance between two nodes in BST ; Standard BST insert function ; This function returns distance of x from root . This function assumes that x exists in BST and BST is not NULL . ; Returns minimum distance beween a and b . This function assumes that a and b exist in BST . ; Both keys lie in left ; Both keys lie in right same path ; Lie in opposite directions ( Root is LCA of two nodes ) ; This function make sure that a is smaller than b before making a call to findDistWrapper ( ) ; Driver code"
Solution in C#:

using System ; class GfG { public class Node { public Node left , right ; public int key ; } static Node newNode ( int key ) { Node ptr = new Node ( ) ; ptr . key = key ; ptr . left = null ; ptr . right = null ; return ptr ; } static Node insert ( Node root , int key ) { if ( root == null ) root = newNode ( key ) ; else if ( root . key > key ) root . left = insert ( root . left , key ) ; else if ( root . key < key ) root . right = insert ( root . right , key ) ; return root ; } static int distanceFromRoot ( Node root , int x ) { if ( root . key == x ) return 0 ; else if ( root . key > x ) return 1 + distanceFromRoot ( root . left , x ) ; return 1 + distanceFromRoot ( root . right , x ) ; } static int distanceBetween2 ( Node root , int a , int b ) { if ( root == null ) return 0 ; if ( root . key > a && root . key > b ) return distanceBetween2 ( root . left , a , b ) ; if ( root . key < a && root . key < b ) return distanceBetween2 ( root . right , a , b ) ; if ( root . key >= a && root . key <= b ) return distanceFromRoot ( root , a ) + distanceFromRoot ( root , b ) ; return 0 ; } static int findDistWrapper ( Node root , int a , int b ) { int temp = 0 ; if ( a > b ) { temp = a ; a = b ; b = temp ; } return distanceBetween2 ( root , a , b ) ; } public static void Main ( String [ ] args ) { Node root = null ; root = insert ( root , 20 ) ; insert ( root , 10 ) ; insert ( root , 5 ) ; insert ( root , 15 ) ; insert ( root , 30 ) ; insert ( root , 25 ) ; insert ( root , 35 ) ; Console . WriteLine ( findDistWrapper ( root , 5 , 35 ) ) ; } }


"Shortest path from a source cell to a destination cell of a Binary Matrix through cells consisting only of 1 s | C # program for the above approach ; Stores the coordinates of the matrix cell ; Stores coordinates of a cell and its distance ; Check if the given cell is valid or not ; Stores the moves of the directions of adjacent cells ; Function to find the shortest path from the source to destination in the given matrix ; Stores the distance for each cell from the source cell ; Distance of source cell is 0 ; Initialize a visited array ; Mark source cell as visited ; Create a queue for BFS ; Distance of source cell is 0 ; Enqueue source cell ; Keeps track of whether destination is reached or not ; Iterate until queue is not empty ; Deque front of the queue ; If the destination cell is reached , then find the path ; Assign the distance of destination to the distance matrix ; Stores the smallest path ; Iterate until source is reached ; Append D ; Append U ; Append R ; Append L ; Print reverse the backtracked path ; Pop the start of queue ; Explore all adjacent directions ; If the current cell is valid cell and can be traversed ; Mark the adjacent cells as visited ; Enque the adjacent cells ; Update the distance of the adjacent cells ; If the destination is not reachable ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int ROW ; static int COL ; class Point { public int x , y ; } ; static Point newPoint ( int x , int y ) { Point temp = new Point ( ) ; temp . x = x ; temp . y = y ; return temp ; } class Node { public Point pt ; public int dist ; } ; static Node newNode ( Point p , int dist ) { Node temp = new Node ( ) ; temp . pt = p ; temp . dist = dist ; return temp ; } static bool isValid ( int row , int col ) { return ( row >= 0 ) && ( col >= 0 ) && ( row < ROW ) && ( col < COL ) ; } static int [ ] dRow = { - 1 , 0 , 0 , 1 } ; static int [ ] dCol = { 0 , - 1 , 1 , 0 } ; static void pathMoves ( char [ , ] mat , Point src , Point dest ) { int [ , ] d = new int [ ROW , COL ] ; for ( int i = 0 ; i < ROW ; i ++ ) { for ( int j = 0 ; j < COL ; j ++ ) d [ i , j ] = - 1 ; } d [ src . x , src . y ] = 0 ; bool [ , ] visited = new bool [ ROW , COL ] ; visited [ src . x , src . y ] = true ; Queue < Node > q = new Queue < Node > ( ) ; Node s = newNode ( src , 0 ) ; q . Enqueue ( s ) ; bool ok = false ; while ( q . Count > 0 ) { Node curr = q . Peek ( ) ; q . Dequeue ( ) ; Point pt = curr . pt ; if ( pt . x == dest . x && pt . y == dest . y ) { int xx = pt . x , yy = pt . y ; int dist = curr . dist ; d [ pt . x , pt . y ] = dist ; string pathmoves = " " ; while ( xx != src . x yy != src . y ) { if ( xx > 0 && d [ xx - 1 , yy ] == dist - 1 ) { pathmoves += ' D ' ; xx -- ; } if ( xx < ROW - 1 && d [ xx + 1 , yy ] == dist - 1 ) { pathmoves += ' U ' ; xx ++ ; } if ( yy > 0 && d [ xx , yy - 1 ] == dist - 1 ) { pathmoves += ' R ' ; yy -- ; } if ( yy < COL - 1 && d [ xx , yy + 1 ] == dist - 1 ) { pathmoves += ' L ' ; yy ++ ; } dist -- ; } for ( int i = pathmoves . Length - 1 ; i >= 0 ; -- i ) Console . Write ( pathmoves [ i ] ) ; ok = true ; break ; } if ( q . Count > 0 ) { q . Peek ( ) ; q . Dequeue ( ) ; } for ( int i = 0 ; i < 4 ; i ++ ) { int row = pt . x + dRow [ i ] ; int col = pt . y + dCol [ i ] ; if ( isValid ( row , col ) && ( mat [ row , col ] == '1' mat [...