"Check whether the number can be made perfect square after adding K | C # program to check whether the number can be made perfect square after adding K ; Function to check whether the number can be made perfect square after adding K ; Computing the square root of the number ; Print Yes if the number is a perfect square ; Driver code"
Solution in C#:

using System ; class GFG { static void isPerfectSquare ( int x ) { int sr = ( int ) Math . Sqrt ( x ) ; if ( sr * sr == x ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( String [ ] args ) { int n = 7 ; int k = 2 ; isPerfectSquare ( n + k ) ; } }


"Check whether the number has only first and last bits set | C # program to check whether the number has only first and last bits set ; function to check whether ' n ' is a power of 2 or not ; function to check whether the number has only first and last bits set ; Driver program to test above"
Solution in C#:

using System ; class GFG { static bool powerOfTwo ( uint n ) { return ( ( n & n - 1 ) == 0 ) ; } static bool onlyFirstAndLastAreSet ( uint n ) { if ( n == 1 ) return true ; return powerOfTwo ( n - 1 ) ; } public static void Main ( ) { uint n = ( uint ) 9 ; if ( onlyFirstAndLastAreSet ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check whether the number has only first and last bits set | Set 2 | C # to check whether the number has only first and last bits set ; function to check whether the number has only first and last bits set ; Driver Code"
Solution in C#:

using System ; class GFG { static bool onlyFirstAndLastAreSet ( int n ) { if ( n == 1 ) return true ; if ( n == 2 ) return false ; return ( ( ( n - 1 ) & ( n - 2 ) ) == 0 ) ; } public static void Main ( ) { int n = 9 ; if ( onlyFirstAndLastAreSet ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check whether the point ( x , y ) lies on a given line | C # implementation of the approach ; Function that return true if the given point lies on the given line ; If ( x , y ) satisfies the equation of the line ; Driver code"
Solution in C#:

using System ; class GFG { static bool pointIsOnLine ( int m , int c , int x , int y ) { if ( y == ( ( m * x ) + c ) ) return true ; return false ; } public static void Main ( ) { int m = 3 , c = 2 ; int x = 1 , y = 5 ; if ( pointIsOnLine ( m , c , x , y ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check whether the product of every subsequence is a perfect square or not | C # program for the above approach ; Function to check if the product of every subsequence of the array is a perfect square or not ; Traverse the given array ; If arr [ i ] is a perfect square or not ; Return " Yes " ; Driver Code"
Solution in C#:

using System ; class GFG { static String perfectSquare ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int p = ( int ) Math . Sqrt ( arr [ i ] ) ; if ( p * p != arr [ i ] ) { return " No " ; } } return " Yes " ; } public static void Main ( ) { int [ ] arr = { 1 , 4 , 100 } ; int N = arr . Length ; Console . WriteLine ( perfectSquare ( arr , N ) ) ; } }


"Check whether the string S1 can be made equal to S2 with the given operation | C # implementation of the approach ; Function to return the string formed by the odd indexed characters of s ; Function to return the string formed by the even indexed characters of s ; Function that returns true if s1 can be made equal to s2 with the given operation ; Get the string formed by the even indexed characters of s1 ; Get the string formed by the even indexed characters of s2 ; Get the string formed by the odd indexed characters of s1 ; Get the string formed by the odd indexed characters of s2 ; Sorting all the lists ; If the strings can be made equal ; Driver Code"
Solution in C#:

using System ; class GFG { static string partOdd ( string s ) { string st = " " ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( i % 2 != 0 ) st += s [ i ] ; } return st ; } static string partEven ( string str ) { string s = " " ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( i % 2 == 0 ) s += str [ i ] ; } return s ; } static bool canBeMadeEqual ( string s1 , string s2 ) { char [ ] even1 = partEven ( s1 ) . ToCharArray ( ) ; char [ ] even2 = partEven ( s2 ) . ToCharArray ( ) ; char [ ] odd1 = partOdd ( s1 ) . ToCharArray ( ) ; char [ ] odd2 = partOdd ( s2 ) . ToCharArray ( ) ; Array . Sort ( even1 ) ; Array . Sort ( even2 ) ; Array . Sort ( odd1 ) ; Array . Sort ( odd2 ) ; string even_s1 = new string ( even1 ) ; string even_s2 = new string ( even2 ) ; string odd_s1 = new string ( odd1 ) ; string odd_s2 = new string ( odd2 ) ; if ( even_s1 . Equals ( even_s2 ) && odd_s1 . Equals ( odd_s2 ) ) return true ; return false ; } public static void Main ( ) { string s1 = " cdab " ; string s2 = " abcd " ; if ( canBeMadeEqual ( s1 , s2 ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check whether the string can be printed using same row of qwerty keypad | C # program to check whether the string can be printed using same row of qwerty keypad ; Function to find the row of the character in the qwerty keypad ; Sets to include the characters from the same row of the qwerty keypad ; Condition to check the row of the current character of the string ; Function to check the characters are from the same row of the qwerty keypad ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int checkQwertyRow ( char x ) { char [ ] first_row1 = { '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '0' , ' - ' , ' = ' } ; HashSet < char > first_row = new HashSet < char > ( first_row1 ) ; char [ ] second_row1 = { ' Q ' , ' W ' , ' E ' , ' R ' , ' T ' , ' Y ' , ' U ' , ' I ' , ' O ' , ' P ' , ' [ ' , ' ] ' , ' q ' , ' w ' , ' e ' , ' r ' , ' t ' , ' y ' , ' u ' , ' i ' , ' o ' , ' p ' } ; HashSet < char > second_row = new HashSet < char > ( second_row1 ) ; char [ ] third_row1 = { ' A ' , ' S ' , ' D ' , ' F ' , ' G ' , ' H ' , ' J ' , ' K ' , ' L ' , ' ; ' , ' : ' , ' a ' , ' s ' , ' d ' , ' f ' , ' g ' , ' h ' , ' j ' , ' k ' , ' l ' } ; HashSet < char > third_row = new HashSet < char > ( third_row1 ) ; char [ ] fourth_row1 = { ' Z ' , ' X ' , ' C ' , ' V ' , ' B ' , ' N ' , ' M ' , ' , ' , ' . ' , ' / ' , ' z ' , ' x ' , ' c ' , ' v ' , ' b ' , ' n ' , ' m ' } ; HashSet < char > fourth_row = new HashSet < char > ( fourth_row1 ) ; if ( first_row . Contains ( x ) ) { return 1 ; } else if ( second_row . Contains ( x ) ) { return 2 ; } else if ( third_row . Contains ( x ) ) { return 3 ; } else if ( fourth_row . Contains ( x ) ) { return 4 ; } return 0 ; } static bool checkValidity ( String str ) { char x = str [ 0 ] ; int row = checkQwertyRow ( x ) ; for ( int i = 0 ; i < str . Length ; i ++ ) { x = str [ i ] ; if ( row != checkQwertyRow ( x ) ) { return false ; } } return true ; } public static void Main ( String [ ] args ) { String str = " GeeksforGeeks " ; if ( checkValidity ( str ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check whether the triangle is valid or not if angles are given | C # program to check validity of any triangle ; Function to check if sum of the three angles is 180 or not ; check condition ; Driver Code ; function calling and print output"
Solution in C#:

using System ; class GFG { public static int Valid ( int a , int b , int c ) { if ( a + b + c == 180 && a != 0 && b != 0 && c != 0 ) return 1 ; else return 0 ; } public static void Main ( ) { int a = 60 , b = 40 , c = 80 ; if ( ( Valid ( a , b , c ) ) == 1 ) Console . WriteLine ( " Valid " ) ; else Console . WriteLine ( " Invalid " ) ; } }


"Check whether the two numbers differ at one bit position only | C # implementation to check whether the two numbers differ at one bit position only ; function to check if x is power of 2 ; First x in the below expression is for the case when x is 0 ; function to check whether the two numbers differ at one bit position only ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPowerOfTwo ( int x ) { return x != 0 && ( ( x & ( x - 1 ) ) == 0 ) ; } static bool differAtOneBitPos ( int a , int b ) { return isPowerOfTwo ( a ^ b ) ; } public static void Main ( ) { int a = 13 , b = 9 ; if ( differAtOneBitPos ( a , b ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check whether the vowels in a string are in alphabetical order or not | C # implementation of above approach ; Function that checks whether the vowel characters in a string are in alphabetical order or not ; ASCII Value 64 is less than all the alphabets so using it as a default value ; check if the vowels in the string are sorted or not ; if the vowel is smaller than the previous vowel ; store the vowel ; Driver code ; check whether the vowel characters in a string are in alphabetical order or not"
Solution in C#:

using System ; class GFG { public static bool areVowelsInOrder ( string s ) { int n = s . Length ; char c = ( char ) 64 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' a ' s [ i ] == ' e ' s [ i ] == ' i ' s [ i ] == ' o ' s [ i ] == ' u ' ) { if ( s [ i ] < c ) { return false ; } else { c = s [ i ] ; } } } return true ; } public static void Main ( string [ ] args ) { string s = " aabbbddeecc " ; if ( areVowelsInOrder ( s ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }


"Check whether there exists a triplet ( i , j , k ) such that arr [ i ] < arr [ k ] < arr [ j ] for i < j < k | C # program for the above approach ; Function to check if there exist triplet in the array such that i < j < k and arr [ i ] < arr [ k ] < arr [ j ] ; Initialize the heights of h1 and h2 to INT_MAX and INT_MIN respectively ; Store the current element as h1 ; If the element at top of stack is less than the current element then pop the stack top and keep updating the value of h3 ; Push the current element on the stack ; If current element is less than h3 , then we found such triplet and return true ; No triplet found , hence return false ; Driver code ; Given array ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static bool findTriplet ( int [ ] arr ) { int n = arr . Length ; Stack < int > st = new Stack < int > ( ) ; int h3 = int . MinValue ; int h1 = int . MaxValue ; for ( int i = n - 1 ; i >= 0 ; i -- ) { h1 = arr [ i ] ; while ( st . Count != 0 && st . Peek ( ) < arr [ i ] ) { h3 = st . Peek ( ) ; st . Pop ( ) ; } st . Push ( arr [ i ] ) ; if ( h1 < h3 ) { return true ; } } return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 7 , 5 , 6 } ; if ( findTriplet ( arr ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }


"Check whether triangle is valid or not if sides are given | C # program to check validity of any triangle ; Function to calculate for validity ; check condition ; Driver code ; function calling and print output"
Solution in C#:

using System ; class GFG { public static int checkValidity ( int a , int b , int c ) { if ( a + b <= c a + c <= b b + c <= a ) return 0 ; else return 1 ; } public static void Main ( ) { int a = 7 , b = 10 , c = 5 ; if ( ( checkValidity ( a , b , c ) ) == 1 ) Console . Write ( " Valid " ) ; else Console . Write ( " Invalid " ) ; } }


"Check whether triangle is valid or not if three points are given | C # implementation to check if three points form a triangle ; Function to check if three points make a triangle ; Calculation the area of triangle . We have skipped multiplication with 0.5 to avoid floating point computations ; Condition to check if area is not equal to 0 ; Driver code"
Solution in C#:

using System ; class GFG { static void checkTriangle ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { int a = x1 * ( y2 - y3 ) + x2 * ( y3 - y1 ) + x3 * ( y1 - y2 ) ; if ( a == 0 ) Console . WriteLine ( " No " ) ; else Console . WriteLine ( " Yes " ) ; } public static void Main ( ) { int x1 = 1 , y1 = 1 , x2 = 2 , y2 = 2 , x3 = 3 , y3 = 3 ; checkTriangle ( x1 , y1 , x2 , y2 , x3 , y3 ) ; } }


"Check whether two convex regular polygon have same center or not | C # implementation to check whether two convex polygons have same center ; Function to check whether two convex polygons have the same center or not ; Driver Code"
Solution in C#:

using System ; class GFG { static int check ( int n , int m ) { if ( m % n == 0 ) { Console . Write ( " YES " ) ; } else { Console . Write ( " NO " ) ; } return 0 ; } public static void Main ( String [ ] args ) { int n = 5 ; int m = 10 ; check ( n , m ) ; } }


"Check whether two numbers are in golden ratio | C # implementation to check whether two numbers are in golden ratio with each other ; Function to check that two numbers are in golden ratio ; Swapping the numbers such that A contains the maximum number between these numbers ; First Ratio ; Second Ratio ; Condition to check that two numbers are in golden ratio ; Driver code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool checkGoldenRatio ( float a , float b ) { if ( a <= b ) { float temp = a ; a = b ; b = temp ; } string ratio1 = String . Format ( " { 0:0.000 } " , a / b ) ; string ratio2 = String . Format ( " { 0:0.000 } " , ( a + b ) / a ) ; if ( ratio1 == ratio2 && ratio1 == "1.618" ) { Console . WriteLine ( " Yes " ) ; return true ; } else { Console . WriteLine ( " No " ) ; return false ; } } static void Main ( ) { float a = ( float ) 0.618 ; float b = 1 ; checkGoldenRatio ( a , b ) ; } }


"Check whether two numbers are in silver ratio | C # implementation to check whether two numbers are in silver ratio with each other ; Function to check that two numbers are in silver ratio ; Swapping the numbers such that A contains the maximum number between these numbers ; First Ratio ; Second Ratio ; Condition to check that two numbers are in silver ratio ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static bool checksilverRatio ( double a , double b ) { if ( a < b ) { a = a + b ; b = a - b ; a = a - b ; } double ratio1 = ( ( a / b ) * 1000 ) / 1000 ; double ratio2 = ( int ) ( ( ( 2 * a + b ) / a ) * 1000 ) ; ratio2 = ratio2 / 1000 ; if ( ratio1 == ratio2 && ( int ) ( ratio1 - 2.414 ) == 0 ) { Console . WriteLine ( " Yes " ) ; return true ; } else { Console . WriteLine ( " No " ) ; return false ; } } public static void Main ( ) { double a = 2.414 ; double b = 1 ; checksilverRatio ( a , b ) ; } }


"Check whether two points ( x1 , y1 ) and ( x2 , y2 ) lie on same side of a given line or not | C # program to check if two points lie on the same side or not ; Function to check if two points lie on the same side or not ; int fx1 ; Variable to store a * x1 + b * y1 - c int fx2 ; Variable to store a * x2 + b * y2 - c ; If fx1 and fx2 have same sign ; Driver code"
Solution in C#:

using System ; class GFG { static bool pointsAreOnSameSideOfLine ( int a , int b , int c , int x1 , int y1 , int x2 , int y2 ) { fx1 = a * x1 + b * y1 - c ; fx2 = a * x2 + b * y2 - c ; if ( ( fx1 * fx2 ) > 0 ) return true ; return false ; } public static void Main ( ) { int a = 1 , b = 1 , c = 1 ; int x1 = 1 , y1 = 1 ; int x2 = 2 , y2 = 1 ; if ( pointsAreOnSameSideOfLine ( a , b , c , x1 , y1 , x2 , y2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check whether two straight lines are orthogonal or not | C # implementation of above approach ; Function to check if two straight lines are orthogonal or not ; Both lines have infinite slope ; Only line 1 has infinite slope ; Only line 2 has infinite slope ; Find slopes of the lines ; Check if their product is - 1 ; Driver code"
Solution in C#:

using System ; class GFG { static bool checkOrtho ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 , int x4 , int y4 ) { int m1 , m2 ; if ( x2 - x1 == 0 && x4 - x3 == 0 ) return false ; else if ( x2 - x1 == 0 ) { m2 = ( y4 - y3 ) / ( x4 - x3 ) ; if ( m2 == 0 ) return true ; else return false ; } else if ( x4 - x3 == 0 ) { m1 = ( y2 - y1 ) / ( x2 - x1 ) ; if ( m1 == 0 ) return true ; else return false ; } else { m1 = ( y2 - y1 ) / ( x2 - x1 ) ; m2 = ( y4 - y3 ) / ( x4 - x3 ) ; if ( m1 * m2 == - 1 ) return true ; else return false ; } } public static void Main ( ) { int x1 = 0 , y1 = 4 , x2 = 0 , y2 = - 9 ; int x3 = 2 , y3 = 0 , x4 = - 1 , y4 = 0 ; if ( checkOrtho ( x1 , y1 , x2 , y2 , x3 , y3 , x4 , y4 ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check whether two straight lines are parallel or not | C # program to implement the above approach ; Function to check if two lines are parallel or not ; If slopes are equal ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static void parallel ( float a1 , float b1 , float c1 , float a2 , float b2 , float c2 ) { if ( ( - ( a1 / b1 ) ) == ( - ( a2 / b2 ) ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } public static void Main ( ) { float a1 = - 2 , b1 = 4 , c1 = 5 ; float a2 = - 6 , b2 = 12 , c2 = 6 ; parallel ( a1 , b1 , c1 , a2 , b2 , c2 ) ; } }


"Check whether two strings are anagram of each other | C # program to check if two strings are anagrams of each other ; function to check if two strings are anagrams of each other ; Create a count array and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; If both strings are of different Length . Removing this condition will make the program fail for strings like " aaca " and " aca " ; See if there is any non - zero value in count array ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int NO_OF_CHARS = 256 ; static bool areAnagram ( char [ ] str1 , char [ ] str2 ) { int [ ] count = new int [ NO_OF_CHARS ] ; int i ; for ( i = 0 ; i < str1 . Length ; i ++ ) { count [ str1 [ i ] - ' a ' ] ++ ; count [ str2 [ i ] - ' a ' ] -- ; } if ( str1 . Length != str2 . Length ) return false ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( count [ i ] != 0 ) { return false ; } return true ; } public static void Main ( String [ ] args ) { char [ ] str1 = " geeksforgeeks " . ToCharArray ( ) ; char [ ] str2 = " forgeeksgeeks " . ToCharArray ( ) ; if ( areAnagram ( str1 , str2 ) ) Console . Write ( " The ▁ two ▁ strings ▁ are ▁ " + " anagram ▁ of ▁ each ▁ other " ) ; else Console . Write ( " The ▁ two ▁ strings ▁ are ▁ " + " not ▁ anagram ▁ of ▁ each ▁ other " ) ; } }


"Check whether two strings are anagram of each other | C # program to check if two strings are anagrams of each other ; function to check whether two strings are anagram of each other ; Create 2 count arrays and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; If both strings are of different length . Removing this condition will make the program fail for strings like " aaca " and " aca " ; Compare count arrays ; Driver code ; Function Call"
Solution in C#:

using System ; public class GFG { static int NO_OF_CHARS = 256 ; static bool areAnagram ( char [ ] str1 , char [ ] str2 ) { int [ ] count1 = new int [ NO_OF_CHARS ] ; int [ ] count2 = new int [ NO_OF_CHARS ] ; int i ; for ( i = 0 ; i < str1 . Length && i < str2 . Length ; i ++ ) { count1 [ str1 [ i ] ] ++ ; count2 [ str2 [ i ] ] ++ ; } if ( str1 . Length != str2 . Length ) return false ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( count1 [ i ] != count2 [ i ] ) return false ; return true ; } public static void Main ( ) { char [ ] str1 = ( " geeksforgeeks " ) . ToCharArray ( ) ; char [ ] str2 = ( " forgeeksgeeks " ) . ToCharArray ( ) ; if ( areAnagram ( str1 , str2 ) ) Console . WriteLine ( " The ▁ two ▁ strings ▁ are " + " anagram ▁ of ▁ each ▁ other " ) ; else Console . WriteLine ( " The ▁ two ▁ strings ▁ are ▁ not " + " ▁ anagram ▁ of ▁ each ▁ other " ) ; } }


"Check whether two strings are equivalent or not according to given condition | C # Program to find whether two strings are equivalent or not according to given condition ; This function returns the least lexicogr - aphical String obtained from its two halves ; Base Case - If String size is 1 ; Divide the String into its two halves ; Form least lexicographical String ; Driver Code"
Solution in C#:

using System ; class GFG { static String leastLexiString ( String s ) { if ( s . Length == 1 ) return s ; String x = leastLexiString ( s . Substring ( 0 , s . Length / 2 ) ) ; String y = leastLexiString ( s . Substring ( s . Length / 2 ) ) ; return ( ( x + y ) . CompareTo ( y + x ) . ToString ( ) ) ; } static Boolean areEquivalent ( String a , String b ) { return ! ( leastLexiString ( a ) . Equals ( leastLexiString ( b ) ) ) ; } public static void Main ( String [ ] args ) { String a = " aaba " ; String b = " abaa " ; if ( areEquivalent ( a , b ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; a = " aabb " ; b = " abab " ; if ( areEquivalent ( a , b ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }


"Check whether two strings can be made equal by copying their characters with the adjacent ones | C # implementation of the above approach ; Function that returns true if both the strings can be made equal with the given operation ; Lengths of both the strings have to be equal ; To store the frequency of the characters of str1 ; For every character of str2 ; If current character of str2 also appears in str1 ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 26 ; static Boolean canBeMadeEqual ( String str1 , String str2 ) { int len1 = str1 . Length ; int len2 = str2 . Length ; if ( len1 == len2 ) { int [ ] freq = new int [ MAX ] ; for ( int i = 0 ; i < len1 ; i ++ ) { freq [ str1 [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < len2 ; i ++ ) { if ( freq [ str2 [ i ] - ' a ' ] > 0 ) return true ; } } return false ; } public static void Main ( String [ ] args ) { String str1 = " abc " , str2 = " defa " ; if ( canBeMadeEqual ( str1 , str2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check whether two strings can be made equal by increasing prefixes | C # implementation of above approach ; check whether the first string can be converted to the second string by increasing the ASCII value of prefix string of first string ; length of two strings ; If lengths are not equal ; store the difference of ASCII values ; difference of first element ; traverse through the string ; the ASCII value of the second string should be greater than or equal to first string , if it is violated return false . ; store the difference of ASCII values ; the difference of ASCII values should be in descending order ; if the difference array is not in descending order ; if all the ASCII values of characters of first string is less than or equal to the second string and the difference array is in descending order , return true ; Driver code ; create two strings ; check whether the first string can be converted to the second string"
Solution in C#:

using System ; class GFG { public static bool find ( string s1 , string s2 ) { int len = s1 . Length , len_1 = s2 . Length ; if ( len != len_1 ) return false ; int [ ] d = new int [ len ] ; d [ 0 ] = s2 [ 0 ] - s1 [ 0 ] ; for ( int i = 1 ; i < len ; i ++ ) { if ( s1 [ i ] > s2 [ i ] ) return false ; else { d [ i ] = s2 [ i ] - s1 [ i ] ; } } for ( int i = 0 ; i < len - 1 ; i ++ ) { if ( d [ i ] < d [ i + 1 ] ) return false ; } return true ; } public static void Main ( ) { string s1 = " abcd " , s2 = " bcdd " ; if ( find ( s1 , s2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check whether two strings contain same characters in same order | C # implementation of the approach ; String class has a constructor that allows us to specify size of String as first parameter and character to be filled in given size as second parameter . ; Function that returns true if the given Strings contain same characters in same order ; Get the first character of both Strings ; Now if there are adjacent similar character remove that character from s1 ; Now if there are adjacent similar character remove that character from s2 ; If both the strings are equal then return true ; Driver code"
Solution in C#:

using System ; public class temp { static String getString ( char x ) { String s = String . Join ( " " , x ) ; return s ; } static Boolean solve ( String s1 , String s2 ) { String a = getString ( s1 [ 0 ] ) , b = getString ( s2 [ 0 ] ) ; for ( int i = 1 ; i < s1 . Length ; i ++ ) if ( s1 [ i ] != s1 [ i - 1 ] ) { a += getString ( s1 [ i ] ) ; } for ( int i = 1 ; i < s2 . Length ; i ++ ) if ( s2 [ i ] != s2 [ i - 1 ] ) { b += getString ( s2 [ i ] ) ; } if ( a == b ) return true ; return false ; } public static void Main ( String [ ] args ) { String s1 = " Geeks " , s2 = " Geks " ; if ( solve ( s1 , s2 ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check whether two strings contain same characters in same order | C # implementation of the approach ; if length of the b = 0 then we return true ; if length of a = 0 that means b is not present in a so we return false ; Driver code"
Solution in C#:

using System ; public class temp { public static bool checkSequence ( String a , String b ) { if ( b . Length == 0 ) return true ; if ( a . Length == 0 ) return false ; if ( a [ 0 ] == b [ 0 ] ) return checkSequence ( a . Substring ( 1 ) , b . Substring ( 1 ) ) ; else return checkSequence ( a . Substring ( 1 ) , b ) ; } public static void Main ( String [ ] args ) { String s1 = " Geeks " , s2 = " Geks " ; if ( checkSequence ( s1 , s2 ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check whether we can sort two arrays by swapping A [ i ] and B [ i ] | C # implementation of above approach ; Function to check whether both the array can be sorted in ( strictly increasing ) ascending order ; Traverse through the array and find out the min and max variable at each position make one array of min variables and another of maximum variable ; Maximum and minimum variable ; Assign min value to B [ i ] and max value to A [ i ] ; Now check whether the array is sorted or not ; Driver code"
Solution in C#:

using System ; class GFG { static bool IsSorted ( int [ ] A , int [ ] B , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int x , y ; x = Math . Max ( A [ i ] , B [ i ] ) ; y = Math . Min ( A [ i ] , B [ i ] ) ; A [ i ] = x ; B [ i ] = y ; } for ( int i = 1 ; i < n ; i ++ ) { if ( A [ i ] <= A [ i - 1 ] B [ i ] <= B [ i - 1 ] ) return false ; } return true ; } public static void Main ( ) { int [ ] A = { 1 , 4 , 3 , 5 , 7 } ; int [ ] B = { 2 , 2 , 5 , 8 , 9 } ; int n = A . Length ; if ( IsSorted ( A , B , n ) == true ) { Console . Write ( " True " ) ; } else { Console . Write ( " False " ) ; } } }


"Chen Prime Number | C # program to check Chen Prime number ; Utility function to check if the given number is semi - prime or not ; Increment count of prime numbers ; If number is greater than 1 , add it to the count variable as it indicates the number remain is prime number ; Return '1' if count is equal to '2' else return '0' ; Function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check chen prime number ; Driver code"
Solution in C#:

using System ; class GFG { static bool isSemiPrime ( int num ) { int cnt = 0 ; for ( int i = 2 ; cnt < 2 && i * i <= num ; ++ i ) while ( num % i == 0 ) { num /= i ; ++ cnt ; } if ( num > 1 ) ++ cnt ; return cnt == 2 ? true : false ; } static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } static bool isChenPrime ( int n ) { if ( isPrime ( n ) && ( isSemiPrime ( n + 2 ) || isPrime ( n + 2 ) ) ) return true ; else return false ; } public static void Main ( ) { int n = 7 ; if ( isChenPrime ( n ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }


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

using System ; class GFG { static int chiliagonNum ( int n ) { return ( 998 * n * n - 996 * n ) / 2 ; } public static void Main ( ) { int n = 3 ; Console . Write ( "3rd ▁ chiliagon ▁ Number ▁ is ▁ = ▁ " + chiliagonNum ( n ) ) ; } }


"Chinese Remainder Theorem | Set 1 ( Introduction ) | C # program to demonstrate working of Chinise remainder Theorem ; k is size of num [ ] and rem [ ] . Returns the smallest number x such that : x % num [ 0 ] = rem [ 0 ] , x % num [ 1 ] = rem [ 1 ] , ... ... ... ... ... ... x % num [ k - 2 ] = rem [ k - 1 ] Assumption : Numbers in num [ ] are pairwise coprime ( gcd for every pair is 1 ) ; Initialize result ; As per the Chinese remainder theorem , this loop will always break . ; Check if remainder of x % num [ j ] is rem [ j ] or not ( for all j from 0 to k - 1 ) ; If all remainders matched , we found x ; Else try next number ; Driver code"
Solution in C#:

using System ; class GFG { static int findMinX ( int [ ] num , int [ ] rem , int k ) { int x = 1 ; while ( true ) { int j ; for ( j = 0 ; j < k ; j ++ ) if ( x % num [ j ] != rem [ j ] ) break ; if ( j == k ) return x ; x ++ ; } } public static void Main ( ) { int [ ] num = { 3 , 4 , 5 } ; int [ ] rem = { 2 , 3 , 1 } ; int k = num . Length ; Console . WriteLine ( " x ▁ is ▁ " + findMinX ( num , rem , k ) ) ; } }


"Chinese Remainder Theorem | Set 2 ( Inverse Modulo based Implementation ) | A C # program to demonstrate working of Chinese remainder Theorem ; Returns modulo inverse of ' a ' with respect to ' m ' using extended Euclid Algorithm . Refer below post for details : https : www . geeksforgeeks . org / multiplicative - inverse - under - modulo - m / ; Apply extended Euclid Algorithm ; q is quotient ; m is remainder now , process same as euclid 's algo ; Make x1 positive ; k is size of num [ ] and rem [ ] . Returns the smallest number x such that : x % num [ 0 ] = rem [ 0 ] , x % num [ 1 ] = rem [ 1 ] , ... ... ... ... ... ... x % num [ k - 2 ] = rem [ k - 1 ] Assumption : Numbers in num [ ] are pairwise coprime ( gcd for every pair is 1 ) ; Compute product of all numbers ; Initialize result ; Apply above formula ; Driver Code"
Solution in C#:

using System ; class GFG { static int inv ( int a , int m ) { int m0 = m , t , q ; int x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = a / m ; t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } static int findMinX ( int [ ] num , int [ ] rem , int k ) { int prod = 1 ; for ( int i = 0 ; i < k ; i ++ ) prod *= num [ i ] ; int result = 0 ; for ( int i = 0 ; i < k ; i ++ ) { int pp = prod / num [ i ] ; result += rem [ i ] * inv ( pp , num [ i ] ) * pp ; } return result % prod ; } static public void Main ( ) { int [ ] num = { 3 , 4 , 5 } ; int [ ] rem = { 2 , 3 , 1 } ; int k = num . Length ; Console . WriteLine ( " x ▁ is ▁ " + findMinX ( num , rem , k ) ) ; } }


"Chocolate Distribution Problem | C # Code For Chocolate Distribution Problem ; arr [ 0. . n - 1 ] represents sizes of packets . m is number of students . Returns minimum difference between maximum and minimum values of distribution . ; if there are no chocolates or number of students is 0 ; Sort the given packets ; Number of students cannot be more than number of packets ; Largest number of chocolates ; Find the subarray of size m such that difference between last ( maximum in case of sorted ) and first ( minimum in case of sorted ) elements of subarray is minimum . ; Driver program to test above function ; int m = 7 ; Number of students"
Solution in C#:

using System ; class GFG { static int findMinDiff ( int [ ] arr , int n , int m ) { if ( m == 0 n == 0 ) return 0 ; Array . Sort ( arr ) ; if ( n < m ) return - 1 ; int min_diff = int . MaxValue ; for ( int i = 0 ; i + m - 1 < n ; i ++ ) { int diff = arr [ i + m - 1 ] - arr [ i ] ; if ( diff < min_diff ) min_diff = diff ; } return min_diff ; } public static void Main ( ) { int [ ] arr = { 12 , 4 , 7 , 9 , 2 , 23 , 25 , 41 , 30 , 40 , 28 , 42 , 30 , 44 , 48 , 43 , 50 } ; int n = arr . Length ; Console . WriteLine ( " Minimum ▁ difference ▁ is ▁ " + findMinDiff ( arr , n , m ) ) ; } }


"Chocolate Distribution Problem | Set 2 | C # program for above approach ; Function to return minimum number of chocolates ; decreasing sequence ; add the chocolates received by that person ; end point of decreasing sequence ; val = 1 ; reset value at that index ; increasing sequence ; flat sequence ; add value of chocolates at position n - 1 ; helper function to get sum of decreasing sequence ; value obtained from decreasing sequence also the count of values in the sequence ; assigning max of values obtained from increasing and decreasing sequences ; sum of count - 1 values & peak value sum of natural numbers : ( n * ( n + 1 ) ) / 2 ; Driver code"
Solution in C#:

using System ; public class GFG { public static int minChocolates ( int [ ] a , int n ) { int i = 0 , j = 0 ; int res = 0 , val = 1 ; while ( j < n - 1 ) { if ( a [ j ] > a [ j + 1 ] ) { j += 1 ; continue ; } if ( i == j ) res += val ; else { res += get_sum ( val , i , j ) ; } if ( a [ j ] < a [ j + 1 ] ) val += 1 ; else val = 1 ; j += 1 ; i = j ; } if ( i == j ) res += val ; else res += get_sum ( val , i , j ) ; return res ; } public static int get_sum ( int peak , int start , int end ) { int count = end - start + 1 ; peak = ( peak > count ) ? peak : count ; int s = peak + ( ( ( count - 1 ) * count ) >> 1 ) ; return s ; } static public void Main ( ) { int [ ] a = { 5 , 5 , 4 , 3 , 2 , 1 } ; int n = a . Length ; Console . WriteLine ( " Minimum ▁ number ▁ of ▁ chocolates ▁ = ▁ " + minChocolates ( a , n ) ) ; } }


"Chocolate Distribution Problem | Set 2 | C # program for the above approach ; FUnction to print minimum number of candies required ; Distribute 1 chocolate to each ; Traverse from left to right ; Traverse from right to left ; Initialize sum ; Find total sum ; Return sum ; Driver Code ; Given array ; Size of the given array"
Solution in C#:

 using System ; public class GFG { static void minChocolates ( int [ ] A , int N ) { int [ ] B = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { B [ i ] = 1 ; } for ( int i = 1 ; i < N ; i ++ ) { if ( A [ i ] > A [ i - 1 ] ) B [ i ] = B [ i - 1 ] + 1 ; else B [ i ] = 1 ; } for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( A [ i ] > A [ i + 1 ] ) B [ i ] = Math . Max ( B [ i + 1 ] + 1 , B [ i ] ) ; else B [ i ] = Math . Max ( B [ i ] , 1 ) ; } int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += B [ i ] ; } Console . Write ( sum + "
 " ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 23 , 14 , 15 , 14 , 56 , 29 , 14 } ; int N = A . Length ; minChocolates ( A , N ) ; } }


"Cholesky Decomposition : Matrix Decomposition | C # program to decompose a matrix using Cholesky Decomposition ; Decomposing a matrix into Lower Triangular ; summation for diagonals ; Evaluating L ( i , j ) using L ( j , j ) ; Displaying Lower Triangular and its Transpose ; Lower Triangular ; Transpose of Lower Triangular ; Driver Code"
Solution in C#:

using System ; class GFG { static void Cholesky_Decomposition ( int [ , ] matrix , int n ) { int [ , ] lower = new int [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { int sum = 0 ; if ( j == i ) { for ( int k = 0 ; k < j ; k ++ ) sum += ( int ) Math . Pow ( lower [ j , k ] , 2 ) ; lower [ j , j ] = ( int ) Math . Sqrt ( matrix [ j , j ] - sum ) ; } else { for ( int k = 0 ; k < j ; k ++ ) sum += ( lower [ i , k ] * lower [ j , k ] ) ; lower [ i , j ] = ( matrix [ i , j ] - sum ) / lower [ j , j ] ; } } } Console . WriteLine ( " ▁ Lower ▁ Triangular 	 ▁ Transpose " ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) Console . Write ( lower [ i , j ] + " 	 " ) ; Console . Write ( " " ) ; for ( int j = 0 ; j < n ; j ++ ) Console . Write ( lower [ j , i ] + " 	 " ) ; Console . WriteLine ( ) ; } } static int Main ( ) { int n = 3 ; int [ , ] matrix = { { 4 , 12 , - 16 } , { 12 , 37 , - 43 } , { - 16 , - 43 , 98 } } ; Cholesky_Decomposition ( matrix , n ) ; return 0 ; } }


"Choose X such that ( A xor X ) + ( B xor X ) is minimized | C # implementation of above approach ; Finding X ; Finding Sum ; Driver Code"
Solution in C#:

using System ; class GFG { public static int findX ( int A , int B ) { return A & B ; } public static int findSum ( int A , int B ) { return A ^ B ; } public static void Main ( String [ ] args ) { int A = 2 , B = 3 ; Console . Write ( " X ▁ = ▁ " + findX ( A , B ) + " , ▁ Sum ▁ = ▁ " + findSum ( A , B ) ) ; } }


"Choose X such that ( A xor X ) + ( B xor X ) is minimized | C # implementation of the approach ; Function to return the integer X such that ( A xor X ) + ( B ^ X ) is minimized ; While either A or B is non - zero ; Position at which both A and B have a set bit ; Inserting a set bit in x ; Right shifting both numbers to traverse all the bits ; Driver code"
Solution in C#:

using System ; class GFG { static int findX ( int A , int B ) { int j = 0 , x = 0 ; while ( A != 0 B != 0 ) { if ( ( A % 2 == 1 ) && ( B % 2 == 1 ) ) { x += ( 1 << j ) ; } A >>= 1 ; B >>= 1 ; j += 1 ; } return x ; } public static void Main ( String [ ] args ) { int A = 2 , B = 3 ; int X = findX ( A , B ) ; Console . WriteLine ( " X ▁ = ▁ " + X + " , ▁ Sum ▁ = ▁ " + ( ( A ^ X ) + ( B ^ X ) ) ) ; } }


"Choose an integer K such that maximum of the xor values of K with all Array elements is minimized | C # implementation to find minimum possible value of the maximum xor in an array by choosing some integer ; Function to calculate minimum possible value of the maximum XOR in an array ; Base case ; Divide elements into two sections ; Traverse all elements of current section and divide in two groups ; Check if one of the sections is empty ; Explore both the possibilities using recursion ; Function to calculate minimum XOR value ; Start recursion from the most significant pos position ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int calculate ( List < int > section , int pos ) { if ( pos < 0 ) return 0 ; List < int > on_section = new List < int > ( ) , off_section = new List < int > ( ) ; foreach ( int el in section ) { if ( ( ( el >> pos ) & 1 ) == 0 ) off_section . Add ( el ) ; else on_section . Add ( el ) ; } if ( off_section . Count == 0 ) return calculate ( on_section , pos - 1 ) ; if ( on_section . Count == 0 ) return calculate ( off_section , pos - 1 ) ; return Math . Min ( calculate ( off_section , pos - 1 ) , calculate ( on_section , pos - 1 ) ) + ( 1 << pos ) ; } static int minXorValue ( int [ ] a , int n ) { List < int > section = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) section . Add ( a [ i ] ) ; return calculate ( section , 30 ) ; } public static void Main ( String [ ] args ) { int N = 4 ; int [ ] A = { 3 , 2 , 5 , 6 } ; Console . Write ( minXorValue ( A , N ) ) ; } }


"Choose k array elements such that difference of maximum and minimum is minimized | C # program to find minimum difference of maximum and minimum of K number . ; Return minimum difference of maximum and minimum of k elements of arr [ 0. . n - 1 ] . ; Sorting the array . ; Find minimum value among all K size subarray . ; Driver code"
Solution in C#:

using System ; class GFG { static int minDiff ( int [ ] arr , int n , int k ) { int result = int . MaxValue ; Array . Sort ( arr ) ; for ( int i = 0 ; i <= n - k ; i ++ ) result = Math . Min ( result , arr [ i + k - 1 ] - arr [ i ] ) ; return result ; } public static void Main ( ) { int [ ] arr = { 10 , 100 , 300 , 200 , 1000 , 20 , 30 } ; int n = arr . Length ; int k = 3 ; Console . WriteLine ( minDiff ( arr , n , k ) ) ; } }


"Choose n elements such that their mean is maximum | C # implementation of the approach ; Utility function to print the contents of an array ; Function to print the array with maximum mean ; Sort the original array ; Construct new array ; Print the resultant array ; Driver code"
Solution in C#:

using System ; class GfG { static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void printMaxMean ( int [ ] arr , int n ) { int [ ] newArr = new int [ n ] ; Array . Sort ( arr , 0 , 2 * n ) ; for ( int i = 0 ; i < n ; i ++ ) newArr [ i ] = arr [ i + n ] ; printArray ( newArr , n ) ; } public static void Main ( ) { int [ ] arr = { 4 , 8 , 3 , 1 , 3 , 7 , 0 , 4 } ; int n = arr . Length ; printMaxMean ( arr , n / 2 ) ; } }


"Choose points from two ranges such that no point lies in both the ranges | C # implementation of the approach ; Function to find the required points ; Driver code"
Solution in C#:

using System ; class GFG { static void findPoints ( int l1 , int r1 , int l2 , int r2 ) { int x = ( l1 != l2 ) ? Math . Min ( l1 , l2 ) : - 1 ; int y = ( r1 != r2 ) ? Math . Max ( r1 , r2 ) : - 1 ; Console . WriteLine ( x + " ▁ " + y ) ; } public static void Main ( ) { int l1 = 5 , r1 = 10 , l2 = 1 , r2 = 7 ; findPoints ( l1 , r1 , l2 , r2 ) ; } }


"Choose two elements from the given array such that their sum is not present in any of the arrays | C # implementation of the approach ; find maximum element in an array ; Function to find the numbers from the given arrays such that their sum is not present in any of the given array ; Find the maximum element from both the arrays ; Driver code"
Solution in C#:

using System ; class GFG { static int max_element ( int [ ] a , int n ) { int m = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) m = Math . Max ( m , a [ i ] ) ; return m ; } static void findNum ( int [ ] a , int n , int [ ] b , int m ) { int x = max_element ( a , n ) ; int y = max_element ( b , m ) ; Console . Write ( x + " ▁ " + y ) ; } public static void Main ( ) { int [ ] a = { 3 , 2 , 2 } ; int n = a . Length ; int [ ] b = { 1 , 5 , 7 , 7 , 9 } ; int m = b . Length ; findNum ( a , n , b , m ) ; } }


"Circle and Lattice Points | C # program to find countLattice points on a circle ; Function to count Lattice points on a circle ; Initialize result as 4 for ( r , 0 ) , ( - r . 0 ) , ( 0 , r ) and ( 0 , - r ) ; Check every value that can be potential x ; Find a potential y ; checking whether square root is an integer or not . Count increments by 4 for four different quadrant values ; Driver code"
Solution in C#:

using System ; class GFG { static int countLattice ( int r ) { if ( r <= 0 ) return 0 ; int result = 4 ; for ( int x = 1 ; x < r ; x ++ ) { int ySquare = r * r - x * x ; int y = ( int ) Math . Sqrt ( ySquare ) ; if ( y * y == ySquare ) result += 4 ; } return result ; } public static void Main ( ) { int r = 5 ; Console . Write ( countLattice ( r ) ) ; } }


"Circular Convolution using Matrix Method | C # program to compute circular convolution of two arrays ; Function to find circular convolution ; Finding the maximum size between the two input sequence sizes ; Copying elements of x to row_vec and padding zeros if size of x < maxSize ; Copying elements of h to col_vec and padding zeros if size of h is less than maxSize ; Generating 2D matrix of circularly shifted elements ; Computing result by matrix multiplication and printing results ; Driver program"
Solution in C#:

using System ; class GFG { readonly static int MAX_SIZE = 10 ; static void convolution ( int [ ] x , int [ ] h , int n , int m ) { int [ ] row_vec = new int [ MAX_SIZE ] ; int [ ] col_vec = new int [ MAX_SIZE ] ; int [ ] out_ = new int [ MAX_SIZE ] ; int [ , ] circular_shift_mat = new int [ MAX_SIZE , MAX_SIZE ] ; int maxSize = n > m ? n : m ; for ( int i = 0 ; i < maxSize ; i ++ ) { if ( i >= n ) { row_vec [ i ] = 0 ; } else { row_vec [ i ] = x [ i ] ; } } for ( int i = 0 ; i < maxSize ; i ++ ) { if ( i >= m ) { col_vec [ i ] = 0 ; } else { col_vec [ i ] = h [ i ] ; } } int k = 0 , d = 0 ; for ( int i = 0 ; i < maxSize ; i ++ ) { int curIndex = k - d ; for ( int j = 0 ; j < maxSize ; j ++ ) { circular_shift_mat [ j , i ] = row_vec [ curIndex % maxSize ] ; curIndex ++ ; } k = maxSize ; d ++ ; } for ( int i = 0 ; i < maxSize ; i ++ ) { for ( int j = 0 ; j < maxSize ; j ++ ) { out_ [ i ] += circular_shift_mat [ i , j ] * col_vec [ j ] ; } Console . Write ( out_ [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] x = { 5 , 7 , 3 , 2 } ; int n = x . Length ; int [ ] h = { 1 , 5 } ; int m = h . Length ; convolution ( x , h , n , m ) ; } }


"Circular Matrix ( Construct a matrix with numbers 1 to m * n in spiral way ) | C # program to fill a matrix with values from 1 to n * n in spiral fashion . ; Fills a [ m , n ] with values from 1 to m * n in spiral fashion . ; Initialize value to be filled in matrix ; k - starting row index m - ending row index l - starting column index n - ending column index ; Print the first row from the remaining rows ; Print the last column from the remaining columns ; Print the last row from the remaining rows ; Print the first column from the remaining columns ; Driver program to test above functions"
Solution in C#:

 using System ; class GFG { static int MAX = 100 ; static void spiralFill ( int m , int n , int [ , ] a ) { int val = 1 ; int k = 0 , l = 0 ; while ( k < m && l < n ) { for ( int i = l ; i < n ; ++ i ) { a [ k , i ] = val ++ ; } k ++ ; for ( int i = k ; i < m ; ++ i ) { a [ i , n - 1 ] = val ++ ; } n -- ; if ( k < m ) { for ( int i = n - 1 ; i >= l ; -- i ) { a [ m - 1 , i ] = val ++ ; } m -- ; } if ( l < n ) { for ( int i = m - 1 ; i >= k ; -- i ) { a [ i , l ] = val ++ ; } l ++ ; } } } public static void Main ( ) { int m = 4 , n = 4 ; int [ , ] a = new int [ MAX , MAX ] ; spiralFill ( m , n , a ) ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { Console . Write ( a [ i , j ] + " ▁ " ) ; } Console . Write ( "
 " ) ; } } }


"Circular Singly Linked List | Insertion |  ; Creating a node dynamically ; Assigning the data ; Adjusting the links"
Solution in C#:

static Node addBegin ( Node last , int data ) { if ( last == null ) return addToEmpty ( last , data ) ; Node temp = new Node ( ) ; temp . data = data ; temp . next = last . next ; last . next = temp ; return last ; }


"Circular Singly Linked List | Insertion |  ; This function is only for empty list ; Creating a node dynamically . ; Assigning the data . ; Note : list was empty . We link single node to itself ."
Solution in C#:

static Node addToEmpty ( Node last , int data ) { if ( last != null ) return last ; Node temp = new Node ( ) ; temp . data = data ; last = temp ; temp . next = last ; return last ; }


"Circumradius of a Cyclic Quadrilateral using the length of Sides | C # program to find circumradius of a cyclic quadrilateral using sides ; Function to return the circumradius of a cyclic quadrilateral using sides ; Find semiperimeter ; Calculate the radius ; Driver Code ; Function call ; Print the radius"
Solution in C#:

using System ; class GFG { static double Circumradius ( int a , int b , int c , int d ) { double s = ( a + b + c + d ) / 2.0 ; double radius = Math . Sqrt ( ( ( a * b ) + ( c * d ) ) * ( ( a * c ) + ( b * d ) ) * ( ( a * d ) + ( b * c ) ) / ( ( s - a ) * ( s - b ) * ( s - c ) * ( s - d ) ) ) ; return radius / 4 ; } public static void Main ( String [ ] args ) { int A = 3 ; int B = 4 ; int C = 5 ; int D = 6 ; double ans = Circumradius ( A , B , C , D ) ; Console . Write ( " { 0 : F2 } " , ans ) ; } }


"Circumradius of the rectangle | C # Program to find the radius of the circumcircle of the given rectangle ; Function to find the radius of the circumcircle ; the sides cannot be negative ; Radius of the circumcircle ; Return the radius ; Driver code ; Get the sides of the triangle ; Find the radius of the circumcircle"
Solution in C#:

using System ; class GFG { static float findRadiusOfcircumcircle ( float l , float b ) { if ( l < 0 b < 0 ) return - 1 ; float radius = ( float ) Math . Sqrt ( Math . Pow ( l , 2 ) + Math . Pow ( b , 2 ) ) / 2 ; return radius ; } public static void Main ( ) { float l = 4 , b = 3 ; Console . WriteLine ( findRadiusOfcircumcircle ( l , b ) ) ; } }


"Climb n | C # program to count total number of ways to reach n - th stair with all jumps allowed ; Function to calculate leaps ; Driver code"
Solution in C#:

using System ; class GFG { static int calculateLeaps ( int n ) { if ( n == 0 n == 1 ) { return 1 ; } else { int leaps = 0 ; for ( int i = 0 ; i < n ; i ++ ) leaps += calculateLeaps ( i ) ; return leaps ; } } public static void Main ( ) { Console . WriteLine ( calculateLeaps ( 4 ) ) ; } }


"Clockwise rotation of Linked List | C # implementation of the approach ; Link list node ; A utility function to push a node ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; A utility function to print linked list ; Function that rotates the given linked list clockwise by k and returns the updated head pointer ; If the linked list is empty ; len is used to store length of the linked list , tmp will point to the last node after this loop ; If k is greater than the size of the linked list ; Subtract from length to convert it into left rotation ; If no rotation needed then return the head node ; current will either point to kth or null after this loop ; If current is null then k is equal to the count of nodes in the list Don 't change the list in this case ; current points to the kth node ; Change next of last node to previous head ; Change head to ( k + 1 ) th node ; Change next of kth node to null ; Return the updated head pointer ; Driver code ; The constructed linked list is : 1.2 . 3.4 . 5 ; Rotate the linked list ; Print the rotated linked list"
Solution in C#:

using System ; class GFG { public class Node { public int data ; public Node next ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } static void printList ( Node node ) { while ( node != null ) { Console . Write ( node . data + " ▁ - > ▁ " ) ; node = node . next ; } Console . Write ( " null " ) ; } static Node rightRotate ( Node head , int k ) { if ( head == null ) return head ; Node tmp = head ; int len = 1 ; while ( tmp . next != null ) { tmp = tmp . next ; len ++ ; } if ( k > len ) k = k % len ; k = len - k ; if ( k == 0 k == len ) return head ; Node current = head ; int cnt = 1 ; while ( cnt < k && current != null ) { current = current . next ; cnt ++ ; } if ( current == null ) return head ; Node kthnode = current ; tmp . next = head ; head = kthnode . next ; kthnode . next = null ; return head ; } public static void Main ( String [ ] args ) { Node head = null ; head = push ( head , 5 ) ; head = push ( head , 4 ) ; head = push ( head , 3 ) ; head = push ( head , 2 ) ; head = push ( head , 1 ) ; int k = 2 ; Node updated_head = rightRotate ( head , k ) ; printList ( updated_head ) ; } }


"Closest ( or Next ) smaller and greater numbers with same number of set bits | C # Implementation of getNext with Same number of bits 1 's is below ; Function to find next smallest number bigger than n ; Compute c0 and c1 ; If there is no bigger number with the same no . of 1 's ; Driver Code ; input 1 ; input 2"
Solution in C#:

using System ; class GFG { static int getNext ( int n ) { int c = n ; int c0 = 0 ; int c1 = 0 ; while ( ( ( c & 1 ) == 0 ) && ( c != 0 ) ) { c0 ++ ; c >>= 1 ; } while ( ( c & 1 ) == 1 ) { c1 ++ ; c >>= 1 ; } if ( c0 + c1 == 31 c0 + c1 == 0 ) return - 1 ; return n + ( 1 << c0 ) + ( 1 << ( c1 - 1 ) ) - 1 ; } static public void Main ( ) { int n = 5 ; Console . WriteLine ( getNext ( n ) ) ; n = 8 ; Console . WriteLine ( getNext ( n ) ) ; } }


"Closest leaf to a given node in Binary Tree | C # program to find closest leaf to given node x in a tree ; A binary tree node ;  ; This function finds closest leaf to root . This distance is stored at * minDist . ; base case ; If this is a leaf node , then check if it is closer than the closest so far ; Recur for left and right subtrees ; This function finds if there is closer leaf to x through parent node . ; Base cases ; Search x in left subtree of root ; If left subtree has x ; Find closest leaf in right subtree ; Search x in right subtree of root ; If right subtree has x ; Find closest leaf in left subtree ; Returns minimum distance of a leaf from given node x ; Initialize result ( minimum distance from a leaf ) ; Find closest leaf down to x ; See if there is a closer leaf through parent ; Driver program ; Let us create Binary Tree shown in above example"
Solution in C#:

using System ; public class Node { public int key ; public Node left , right ; public Node ( int key ) { this . key = key ; left = right = null ; } } public class Distance { public int minDis = int . MaxValue ; } public class BinaryTree { public Node root ; public virtual void findLeafDown ( Node root , int lev , Distance minDist ) { if ( root == null ) { return ; } if ( root . left == null && root . right == null ) { if ( lev < ( minDist . minDis ) ) { minDist . minDis = lev ; } return ; } findLeafDown ( root . left , lev + 1 , minDist ) ; findLeafDown ( root . right , lev + 1 , minDist ) ; } public virtual int findThroughParent ( Node root , Node x , Distance minDist ) { if ( root == null ) { return - 1 ; } if ( root == x ) { return 0 ; } int l = findThroughParent ( root . left , x , minDist ) ; if ( l != - 1 ) { findLeafDown ( root . right , l + 2 , minDist ) ; return l + 1 ; } int r = findThroughParent ( root . right , x , minDist ) ; if ( r != - 1 ) { findLeafDown ( root . left , r + 2 , minDist ) ; return r + 1 ; } return - 1 ; } public virtual int minimumDistance ( Node root , Node x ) { Distance d = new Distance ( ) ; findLeafDown ( x , 0 , d ) ; findThroughParent ( root , x , d ) ; return d . minDis ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 12 ) ; tree . root . right = new Node ( 13 ) ; tree . root . right . left = new Node ( 14 ) ; tree . root . right . right = new Node ( 15 ) ; tree . root . right . left . left = new Node ( 21 ) ; tree . root . right . left . right = new Node ( 22 ) ; tree . root . right . right . left = new Node ( 23 ) ; tree . root . right . right . right = new Node ( 24 ) ; tree . root . right . left . left . left = new Node ( 1 ) ; tree . root . right . left . left . right = new Node ( 2 ) ; tree . root . right . left . right . left = new Node ( 3 ) ; tree . root . right . left . right . right = new Node ( 4 ) ; tree . root . right . right . left . left = new Node ( 5 )...


"Closest numbers from a list of unsorted integers | C # program to find minimum difference an unsorted array . ; Returns minimum difference between any two pair in arr [ 0. . n - 1 ] ; Sort array elements ; Compare differences of adjacent pairs to find the minimum difference . ; Traverse array again and print all pairs with difference as minDiff . ; Driver code"
Solution in C#:

using System ; class GFG { static void printMinDiffPairs ( int [ ] arr , int n ) { if ( n <= 1 ) return ; Array . Sort ( arr ) ; int minDiff = arr [ 1 ] - arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) minDiff = Math . Min ( minDiff , arr [ i ] - arr [ i - 1 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( ( arr [ i ] - arr [ i - 1 ] ) == minDiff ) { Console . Write ( " ▁ ( " + arr [ i - 1 ] + " , ▁ " + arr [ i ] + " ) , ▁ " ) ; } } } public static void Main ( ) { int [ ] arr = { 5 , 3 , 2 , 4 , 1 } ; int n = arr . Length ; printMinDiffPairs ( arr , n ) ; } }


"Closest perfect square and its distance | C # program to find the closest perfect square taking minimum steps to reach from a number ; Function to check if a number is perfect square or not ; Function to find the closest perfect square taking minimum steps to reach from a number ; Variables to store first perfect square number above and below N ; Finding first perfect square number greater than N ; Finding first perfect square number less than N ; Variables to store the differences ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPerfect ( int N ) { if ( ( Math . Sqrt ( N ) - Math . Floor ( Math . Sqrt ( N ) ) ) != 0 ) return false ; return true ; } static void getClosestPerfectSquare ( int N ) { if ( isPerfect ( N ) ) { Console . WriteLine ( N + " ▁ " + "0" ) ; return ; } int aboveN = - 1 , belowN = - 1 ; int n1 ; n1 = N + 1 ; while ( true ) { if ( isPerfect ( n1 ) ) { aboveN = n1 ; break ; } else n1 ++ ; } n1 = N - 1 ; while ( true ) { if ( isPerfect ( n1 ) ) { belowN = n1 ; break ; } else n1 -- ; } int diff1 = aboveN - N ; int diff2 = N - belowN ; if ( diff1 > diff2 ) Console . WriteLine ( belowN + " ▁ " + diff2 ) ; else Console . WriteLine ( aboveN + " ▁ " + diff1 ) ; } public static void Main ( ) { int N = 1500 ; getClosestPerfectSquare ( N ) ; } }


"Clustering / Partitioning an array such that sum of square differences is minimum | C # program to find minimum cost k partitions of array . ; Initialize answer as infinite . ; function to generate all possible answers . and compute minimum of all costs . i -- > is index of previous partition par -- > is current number of partitions a [ ] and n -- > Input array and its size current_ans -- > Cost of partitions made so far . ; If number of partitions is more than k ; If we have mad k partitions and have reached last element ; 1 ) Partition array at different points 2 ) For every point , increase count of partitions , " par " by 1. 3 ) Before recursive call , add cost of the partition to current_ans ; Driver code"
Solution in C#:

using System ; class GFG { static int inf = 1000000000 ; static int ans = inf ; static void solve ( int i , int par , int [ ] a , int n , int k , int current_ans ) { if ( par > k ) return ; if ( par == k && i == n - 1 ) { ans = Math . Min ( ans , current_ans ) ; return ; } for ( int j = i + 1 ; j < n ; j ++ ) solve ( j , par + 1 , a , n , k , current_ans + ( a [ j ] - a [ i + 1 ] ) * ( a [ j ] - a [ i + 1 ] ) ) ; } public static void Main ( ) { int k = 2 ; int [ ] a = { 1 , 5 , 8 , 10 } ; int n = a . Length ; solve ( - 1 , 0 , a , n , k , 0 ) ; Console . Write ( ans ) ; } }


"Clustering / Partitioning an array such that sum of square differences is minimum | C # program to find minimum cost k partitions of array . ; Returns minimum cost of partitioning a [ ] in k clusters . ; Create a dp [ ] [ ] table and initialize all values as infinite . dp [ i ] [ j ] is going to store optimal partition cost for arr [ 0. . i - 1 ] and j partitions ; Fill dp [ ] [ ] in bottom up manner ; Current ending position ( After i - th iteration result for a [ 0. . i - 1 ] is computed . ; j is number of partitions ; Picking previous partition for current i . ; Driver code"
Solution in C#:

using System ; class GFG { static int inf = 1000000000 ; static int minCost ( int [ ] a , int n , int k ) { int [ , ] dp = new int [ n + 1 , k + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= k ; j ++ ) dp [ i , j ] = inf ; dp [ 0 , 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= k ; j ++ ) for ( int m = i - 1 ; m >= 0 ; m -- ) dp [ i , j ] = Math . Min ( dp [ i , j ] , dp [ m , j - 1 ] + ( a [ i - 1 ] - a [ m ] ) * ( a [ i - 1 ] - a [ m ] ) ) ; return dp [ n , k ] ; } public static void Main ( ) { int k = 2 ; int [ ] a = { 1 , 5 , 8 , 10 } ; int n = a . Length ; Console . Write ( minCost ( a , n , k ) ) ; } }


"Co | C # program to represent a number as sum of a co - prime pair such that difference between them is minimum ; Utility function ; function to check if pair is co - prime or not ; function to find and print co - prime pair ; Driver code"
Solution in C#:

using System ; class GFG { static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static bool coprime ( int a , int b ) { return ( __gcd ( a , b ) == 1 ) ; } static void pairSum ( int n ) { int mid = n / 2 ; for ( int i = mid ; i >= 1 ; i -- ) { if ( coprime ( i , n - i ) == true ) { Console . Write ( i + " ▁ " + ( n - i ) ) ; break ; } } } public static void Main ( ) { int n = 11 ; pairSum ( n ) ; } }


"Cocktail Sort | C # program for implementation of Cocktail Sort ; Sorts arrar a [ 0. . n - 1 ] using Cocktail sort ; reset the swapped flag on entering the loop , because it might be true from a previous iteration . ; loop from bottom to top same as the bubble sort ; if nothing moved , then array is sorted . ; otherwise , reset the swapped flag so that it can be used in the next stage ; move the end point back by one , because item at the end is in its rightful spot ; from top to bottom , doing the same comparison as in the previous stage ; increase the starting point , because the last stage would have moved the next smallest number to its rightful spot . ; Prints the array ; Driver code"
Solution in C#:

using System ; class GFG { static void cocktailSort ( int [ ] a ) { bool swapped = true ; int start = 0 ; int end = a . Length ; while ( swapped == true ) { swapped = false ; for ( int i = start ; i < end - 1 ; ++ i ) { if ( a [ i ] > a [ i + 1 ] ) { int temp = a [ i ] ; a [ i ] = a [ i + 1 ] ; a [ i + 1 ] = temp ; swapped = true ; } } if ( swapped == false ) break ; swapped = false ; end = end - 1 ; for ( int i = end - 1 ; i >= start ; i -- ) { if ( a [ i ] > a [ i + 1 ] ) { int temp = a [ i ] ; a [ i ] = a [ i + 1 ] ; a [ i + 1 ] = temp ; swapped = true ; } } start = start + 1 ; } } static void printArray ( int [ ] a ) { int n = a . Length ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( ) { int [ ] a = { 5 , 1 , 4 , 2 , 8 , 0 , 2 } ; cocktailSort ( a ) ; Console . WriteLine ( " Sorted ▁ array ▁ " ) ; printArray ( a ) ; } }


"Coin game of two corners ( Greedy Approach ) | C # program to find coins to be picked to make sure that we never loose . ; Returns optimal value possible that a player can collect from an array of coins of size n . Note than n must be even ; Find sum of odd positioned coins ; Find sum of even positioned coins ; Print even or odd coins depending upon which sum is greater . ; Driver Code"
Solution in C#:

 using System ; class GFG { static void printCoins ( int [ ] arr , int n ) { int oddSum = 0 ; for ( int i = 0 ; i < n ; i += 2 ) oddSum += arr [ i ] ; int evenSum = 0 ; for ( int i = 1 ; i < n ; i += 2 ) evenSum += arr [ i ] ; int start = ( ( oddSum > evenSum ) ? 0 : 1 ) ; for ( int i = start ; i < n ; i += 2 ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr1 = { 8 , 15 , 3 , 7 } ; int n = arr1 . Length ; printCoins ( arr1 , n ) ; Console . Write ( "
 " ) ; int [ ] arr2 = { 2 , 2 , 2 , 2 } ; n = arr2 . Length ; printCoins ( arr2 , n ) ; Console . Write ( "
 " ) ; int [ ] arr3 = { 20 , 30 , 2 , 2 , 2 , 10 } ; n = arr3 . Length ; printCoins ( arr3 , n ) ; } }


"Coin game winner where every player has three choices | C # program to find winner of game if player can pick 1 , x , y coins ; To find winner of game ; To store results ; Initial values ; Computing other values . ; If A losses any of i - 1 or i - x or i - y game then he will definitely win game i ; Else A loses game . ; If dp [ n ] is true then A will game otherwise he losses ; Driver program to test findWinner ( ) ;"
Solution in C#:

using System ; public class GFG { static bool findWinner ( int x , int y , int n ) { bool [ ] dp = new bool [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) dp [ i ] = false ; dp [ 0 ] = false ; dp [ 1 ] = true ; for ( int i = 2 ; i <= n ; i ++ ) { if ( i - 1 >= 0 && dp [ i - 1 ] == false ) dp [ i ] = true ; else if ( i - x >= 0 && dp [ i - x ] == false ) dp [ i ] = true ; else if ( i - y >= 0 && dp [ i - y ] == false ) dp [ i ] = true ; else dp [ i ] = false ; } return dp [ n ] ; } public static void Main ( ) { int x = 3 , y = 4 , n = 5 ; if ( findWinner ( x , y , n ) == true ) Console . WriteLine ( ' A ' ) ; else Console . WriteLine ( ' B ' ) ; } }


"Collect all coins in minimum number of steps | C # Code to Collect all coins in minimum number of steps ; recursive method to collect coins from height array l to r , with height h already collected ; if l is more than r , no steps needed ; loop over heights to get minimum height index ; choose minimum from , 1 ) collecting coins using all vertical lines ( total r - l ) 2 ) collecting coins using lower horizontal lines and recursively on left and right segments ; method returns minimum number of step to collect coin from stack , with height in height [ ] array ; Driver program to test above function"
Solution in C#:

using System ; class GFG { public static int minStepsRecur ( int [ ] height , int l , int r , int h ) { if ( l >= r ) return 0 ; int m = l ; for ( int i = l ; i < r ; i ++ ) if ( height [ i ] < height [ m ] ) m = i ; return Math . Min ( r - l , minStepsRecur ( height , l , m , height [ m ] ) + minStepsRecur ( height , m + 1 , r , height [ m ] ) + height [ m ] - h ) ; } public static int minSteps ( int [ ] height , int N ) { return minStepsRecur ( height , 0 , N , 0 ) ; } public static void Main ( ) { int [ ] height = { 2 , 1 , 2 , 5 , 1 } ; int N = height . Length ; Console . Write ( minSteps ( height , N ) ) ; } }


"Collect maximum points in a grid using two traversals | A Memoization based program to find maximum collection using two traversals of a grid ; checks whether a given input is valid or not ; Driver function to collect Math . max value ; if P1 or P2 is at an invalid cell ; if both traversals reach their destinations ; If both traversals are at last row but not at their destination ; If subproblem is already solved ; Initialize answer for this subproblem ; this variable is used to store gain of current cell ( s ) ; Recur for all possible cases , then store and return the one with max value ; This is mainly a wrapper over recursive function getMaxUtil ( ) . This function creates a table for memoization and calls getMaxUtil ( ) ; Create a memoization table and initialize all entries as - 1 ; Calculation maximum value using memoization based function getMaxUtil ( ) ; Driver code"
Solution in C#:

using System ; class GFG { static readonly int R = 5 ; static readonly int C = 4 ; static bool isValid ( int x , int y1 , int y2 ) { return ( x >= 0 && x < R && y1 >= 0 && y1 < C && y2 >= 0 && y2 < C ) ; } static int getMaxUtil ( int [ , ] arr , int [ , , ] mem , int x , int y1 , int y2 ) { if ( ! isValid ( x , y1 , y2 ) ) return int . MinValue ; if ( x == R - 1 && y1 == 0 && y2 == C - 1 ) return ( y1 == y2 ) ? arr [ x , y1 ] : arr [ x , y1 ] + arr [ x , y2 ] ; if ( x == R - 1 ) return int . MinValue ; if ( mem [ x , y1 , y2 ] != - 1 ) return mem [ x , y1 , y2 ] ; int ans = int . MinValue ; int temp = ( y1 == y2 ) ? arr [ x , y1 ] : arr [ x , y1 ] + arr [ x , y2 ] ; ans = Math . Max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 , y2 - 1 ) ) ; ans = Math . Max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 , y2 + 1 ) ) ; ans = Math . Max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 , y2 ) ) ; ans = Math . Max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 - 1 , y2 ) ) ; ans = Math . Max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 - 1 , y2 - 1 ) ) ; ans = Math . Max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 - 1 , y2 + 1 ) ) ; ans = Math . Max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 + 1 , y2 ) ) ; ans = Math . Max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 + 1 , y2 - 1 ) ) ; ans = Math . Max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 + 1 , y2 + 1 ) ) ; return ( mem [ x , y1 , y2 ] = ans ) ; } static int geMaxCollection ( int [ , ] arr ) { int [ , , ] mem = new int [ R , C , C ] ; for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { for ( int l = 0 ; l < C ; l ++ ) mem [ i , j , l ] = - 1 ; } } return getMaxUtil ( arr , mem , 0 , 0 , C - 1 ) ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 3 , 6 , 8 , 2 } , { 5 , 2 , 4 , 3 } , { 1 , 1 , 20 , 10 } , { 1 , 1 , 20 , 10 } , { 1 , 1 , 20 , 10 } , } ; Console . Write ( " Maximum ▁ collection ▁ is ▁ " + geMaxCollection ( arr ) ) ; } }


"Color N boxes using M colors such that K boxes have different color from the box on its left | C # Program to Paint N boxes using M colors such that K boxes have color different from color of box on its left ; This function returns the required number of ways where idx is the current index and diff is number of boxes having different color from box on its left ; Base Case ; If already computed ; Either paint with same color as previous one ; Or paint with remaining ( M - 1 ) colors ; Driver code ; Multiply M since first box can be painted with any of the M colors and start solving from 2 nd box"
Solution in C#:

using System ; class GFG { static int M = 1001 ; static int MOD = 998244353 ; static int [ , ] dp = new int [ M , M ] ; static int solve ( int idx , int diff , int N , int M , int K ) { if ( idx > N ) { if ( diff == K ) return 1 ; return 0 ; } if ( dp [ idx , diff ] != - 1 ) return dp [ idx , diff ] ; int ans = solve ( idx + 1 , diff , N , M , K ) ; ans += ( M - 1 ) * solve ( idx + 1 , diff + 1 , N , M , K ) ; return dp [ idx , diff ] = ans % MOD ; } public static void Main ( ) { int N = 3 , M = 3 , K = 0 ; for ( int i = 0 ; i <= M ; i ++ ) for ( int j = 0 ; j <= M ; j ++ ) dp [ i , j ] = - 1 ; Console . WriteLine ( ( M * solve ( 2 , 0 , N , M , K ) ) ) ; } }


"Color a grid such that all same color cells are connected either horizontally or vertically | C # program to color a grid such that all same color cells are connected either horizontally or vertically ; Current color ; Final grid ; If even row ; Traverse from left to right ; If color has been exhausted , move to the next color ; Color the grid at this position ; Reduce the color count ; Traverse from right to left for odd rows ; Print the grid ; Driver Code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static void solve ( List < int > arr , int r , int c ) { int idx = 1 ; int [ , ] dp = new int [ r , c ] ; for ( int i = 0 ; i < r ; i ++ ) { if ( i % 2 == 0 ) { for ( int j = 0 ; j < c ; j ++ ) { if ( arr [ idx - 1 ] == 0 ) idx ++ ; dp [ i , j ] = idx ; arr [ idx - 1 ] = arr [ idx - 1 ] - 1 ; } } else { for ( int j = c - 1 ; j >= 0 ; j -- ) { if ( arr [ idx - 1 ] == 0 ) idx ++ ; dp [ i , j ] = idx ; arr [ idx - 1 ] = arr [ idx - 1 ] - 1 ; } } } for ( int i = 0 ; i < r ; ++ i ) { for ( int j = 0 ; j < c ; ++ j ) { Console . Write ( dp [ i , j ] + " ▁ " ) ; } Console . Write ( '
 ' ) ; } } public static void Main ( string [ ] args ) { int r = 3 , c = 5 ; List < int > arr = new List < int > ( ) ; arr . Add ( 1 ) ; arr . Add ( 2 ) ; arr . Add ( 3 ) ; arr . Add ( 4 ) ; arr . Add ( 5 ) ; solve ( arr , r , c ) ; } }


"Comb Sort | C # program for implementation of Comb Sort ; To find gap between elements ; Shrink gap by Shrink factor ; Function to sort arr [ ] using Comb Sort ; initialize gap ; Initialize swapped as true to make sure that loop runs ; Keep running while gap is more than 1 and last iteration caused a swap ; Find next gap ; Initialize swapped as false so that we can check if swap happened or not ; Compare all elements with current gap ; Swap arr [ i ] and arr [ i + gap ] ; Set swapped ; Driver method"
Solution in C#:

using System ; class GFG { static int getNextGap ( int gap ) { gap = ( gap * 10 ) / 13 ; if ( gap < 1 ) return 1 ; return gap ; } static void sort ( int [ ] arr ) { int n = arr . Length ; int gap = n ; bool swapped = true ; while ( gap != 1 swapped == true ) { gap = getNextGap ( gap ) ; swapped = false ; for ( int i = 0 ; i < n - gap ; i ++ ) { if ( arr [ i ] > arr [ i + gap ] ) { int temp = arr [ i ] ; arr [ i ] = arr [ i + gap ] ; arr [ i + gap ] = temp ; swapped = true ; } } } } public static void Main ( ) { int [ ] arr = { 8 , 4 , 1 , 56 , 3 , - 44 , 23 , - 6 , 28 , 0 } ; sort ( arr ) ; Console . WriteLine ( " sorted ▁ array " ) ; for ( int i = 0 ; i < arr . Length ; ++ i ) Console . Write ( arr [ i ] + " ▁ " ) ; } }


"Combinations where every element appears twice and distance between appearances is equal to the value | C # program to find all combinations where every element appears twice and distance between appearances is equal to the value ; Find all combinations that satisfies given constraints ; If all elements are filled , print the solution ; Try all possible combinations for element elem ; If position i and ( i + elem + 1 ) are not occupied in the vector ; Place elem at position i and ( i + elem + 1 ) ; Recurse for next element ; Backtrack ( remove elem from position i and ( i + elem + 1 ) ) ; Create a vector of double the size of given number with ; All its elements initialized by 1 ; Start from element 1 ; Driver Code ; Given number"
Solution in C#:

using System ; using System . Collections . Generic ; class Test { static void allCombinationsRec ( List < int > arr , int elem , int n ) { if ( elem > n ) { foreach ( int i in arr ) Console . Write ( i + " ▁ " ) ; Console . WriteLine ( ) ; return ; } for ( int i = 0 ; i < 2 * n ; i ++ ) { if ( arr [ i ] == - 1 && ( i + elem + 1 ) < 2 * n && arr [ i + elem + 1 ] == - 1 ) { arr [ i ] = elem ; arr [ i + elem + 1 ] = elem ; allCombinationsRec ( arr , elem + 1 , n ) ; arr [ i ] = - 1 ; arr [ i + elem + 1 ] = - 1 ; } } } static void allCombinations ( int n ) { List < int > arr = new List < int > ( ) ; for ( int i = 0 ; i < 2 * n ; i ++ ) { arr . Add ( - 1 ) ; } int elem = 1 ; allCombinationsRec ( arr , elem , n ) ; } public static void Main ( string [ ] args ) { int n = 3 ; allCombinations ( n ) ; } }


"Combinatorics on ordered trees | C # code to find the number of ordered trees with given number of edges and leaves ; Function returns value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to calculate the number of trees with exactly k leaves . ; Function to calculate total number of nodes of degree d in these trees . ; Function to calculate the number of trees in which the root has degree r . ; Driver program to test above functions ; Number of trees having 3 edges and exactly 2 leaves ; Number of nodes of degree 3 in a tree having 4 edges ; Number of trees having 3 edges where root has degree 2"
Solution in C#:

using System ; class GFG { static int binomialCoeff ( int n , int k ) { int [ , ] C = new int [ n + 1 , k + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . Min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i , j ] = 1 ; else C [ i , j ] = C [ i - 1 , j - 1 ] + C [ i - 1 , j ] ; } } return C [ n , k ] ; } static int k_Leaves ( int n , int k ) { int ans = ( binomialCoeff ( n , k ) * binomialCoeff ( n , k - 1 ) ) / n ; Console . WriteLine ( " Number ▁ of ▁ trees ▁ " + " having ▁ 4 ▁ edges ▁ and ▁ exactly ▁ 2 ▁ " + " leaves ▁ : ▁ " + ans ) ; return 0 ; } static int numberOfNodes ( int n , int d ) { int ans = binomialCoeff ( 2 * n - 1 - d , n - 1 ) ; Console . WriteLine ( " Number ▁ of ▁ nodes ▁ " + " of ▁ degree ▁ 1 ▁ in ▁ a ▁ tree ▁ having ▁ 4 ▁ " + " edges ▁ : ▁ " + ans ) ; return 0 ; } static int rootDegreeR ( int n , int r ) { int ans = r * binomialCoeff ( 2 * n - 1 - r , n - 1 ) ; ans = ans / n ; Console . WriteLine ( " Number ▁ of ▁ trees ▁ " + " having ▁ 4 ▁ edges ▁ where ▁ root ▁ has " + " ▁ degree ▁ 2 ▁ : ▁ " + ans ) ; return 0 ; } public static void Main ( ) { k_Leaves ( 3 , 2 ) ; numberOfNodes ( 3 , 1 ) ; rootDegreeR ( 3 , 2 ) ; } }


"Common Divisors of Two Numbers | C # implementation of program ; Map to store the count of each prime factor of a ; Function that calculate the count of each prime factor of a number ; Function to calculate all common divisors of two given numbers a , b -- > input integer numbers ; Find count of each prime factor of a ; Stores number of common divisors ; Find the count of prime factors of b using distinct prime factors of a ; Prime factor of common divisor has minimum cnt of both a and b ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static Dictionary < int , int > ma = new Dictionary < int , int > ( ) ; static void primeFactorize ( int a ) { for ( int i = 2 ; i * i <= a ; i += 2 ) { int cnt = 0 ; while ( a % i == 0 ) { cnt ++ ; a /= i ; } ma . Add ( i , cnt ) ; } if ( a > 1 ) ma . Add ( a , 1 ) ; } static int commDiv ( int a , int b ) { primeFactorize ( a ) ; int res = 1 ; foreach ( KeyValuePair < int , int > m in ma ) { int cnt = 0 ; int key = m . Key ; int value = m . Value ; while ( b % key == 0 ) { b /= key ; cnt ++ ; } res *= ( Math . Min ( cnt , value ) + 1 ) ; } return res ; } static void Main ( ) { int a = 12 , b = 24 ; Console . WriteLine ( commDiv ( a , b ) ) ; } }


"Common Divisors of Two Numbers | C # implementation of program ; method to calculate gcd of two numbers ; method to calculate all common divisors of two given numbers a , b -- > input integer numbers ; find gcd of a , b ; Count divisors of n . ; if ' i ' is factor of n ; check if divisors are equal ; Driver method"
Solution in C#:

using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int commDiv ( int a , int b ) { int n = gcd ( a , b ) ; int result = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) result += 1 ; else result += 2 ; } } return result ; } public static void Main ( String [ ] args ) { int a = 12 , b = 24 ; Console . Write ( commDiv ( a , b ) ) ; } }


"Common characters in n strings | C # Program to find all the common characters in n strings ; primary array for common characters we assume all characters are seen before . ; for each string ; secondary array for common characters Initially marked false ; for every character of ith string ; if character is present in all strings before , mark it . ; Copy whole secondary array into primary ; Displaying common characters ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX_CHAR = 26 ; public static void commonCharacters ( String [ ] str , int n ) { Boolean [ ] prim = new Boolean [ MAX_CHAR ] ; for ( int i = 0 ; i < prim . Length ; i ++ ) prim [ i ] = true ; for ( int i = 0 ; i < n ; i ++ ) { Boolean [ ] sec = new Boolean [ MAX_CHAR ] ; for ( int s = 0 ; s < sec . Length ; s ++ ) sec [ s ] = false ; for ( int j = 0 ; j < str [ i ] . Length ; j ++ ) { if ( prim [ str [ i ] [ j ] - ' a ' ] ) sec [ str [ i ] [ j ] - ' a ' ] = true ; } Array . Copy ( sec , 0 , prim , 0 , MAX_CHAR ) ; } for ( int i = 0 ; i < 26 ; i ++ ) if ( prim [ i ] ) { Console . Write ( ( char ) ( i + 97 ) ) ; Console . Write ( " ▁ " ) ; } } public static void Main ( String [ ] args ) { String [ ] str = { " geeksforgeeks " , " gemkstones " , " acknowledges " , " aguelikes " } ; int n = str . Length ; commonCharacters ( str , n ) ; } }


"Common divisors of N numbers | C # program to find all common divisors of N numbers ; Function to calculate gcd of two numbers ; Function to print all the common divisors ; Variable to find the gcd of N numbers ; Set to store all the common divisors ; Finding GCD of the given N numbers ; Finding divisors of the HCF of n numbers ; Print all the divisors ; Driver 's Code ; Function to print all the common divisors"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void printAllDivisors ( int [ ] arr , int N ) { int g = arr [ 0 ] ; HashSet < int > divisors = new HashSet < int > ( ) ; for ( int i = 1 ; i < N ; i ++ ) { g = gcd ( arr [ i ] , g ) ; } for ( int i = 1 ; i * i <= g ; i ++ ) { if ( g % i == 0 ) { divisors . Add ( i ) ; if ( g / i != i ) divisors . Add ( g / i ) ; } } foreach ( int it in divisors ) Console . Write ( it + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 90 , 12 , 18 , 30 , 24 } ; int n = arr . Length ; printAllDivisors ( arr , n ) ; } }


"Compare numbers represented by Linked Lists | C # implementation of the approach ; Structure for a linked list node ; A helper function to remove zeros from the start of the linked list ; A helper function to find the length of linked list ; Given a reference ( pointer to pointer ) to the head of a list and an int , push a new node on the front of the list . ; Allocate node ; Set the data ; Link the old list after the new node ; Set the head to point to the new node ; Function to compare the numbers represented as linked lists ; Remover leading zeroes from the linked lists ; Since the number represented by a has a greater length , it will be greater ; If the lengths of two numbers are equal we have to check their magnitudes ; If we reach here , then a and b are not null and their data is same , so move to next nodes in both lists ; If linked lists are identical , then we need to return zero ; Driver code ; The constructed linked lists are : a : 5 -> 6 -> 7 b : 2 -> 3 -> 3"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node next ; } ; static Node removeLeadingZeros ( Node a ) { if ( a != null && a . data == 0 ) return removeLeadingZeros ( a . next ) ; else return a ; } static int getSize ( Node a ) { int sz = 0 ; while ( a != null ) { a = a . next ; sz ++ ; } return sz ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static int compare ( Node a , Node b ) { a = removeLeadingZeros ( a ) ; b = removeLeadingZeros ( b ) ; int lenA = getSize ( a ) ; int lenB = getSize ( b ) ; if ( lenA > lenB ) { return 1 ; } else if ( lenB > lenA ) { return - 1 ; } while ( a != null && b != null ) { if ( a . data > b . data ) return 1 ; else if ( a . data < b . data ) return - 1 ; a = a . next ; b = b . next ; } return 0 ; } public static void Main ( String [ ] args ) { Node a = null ; a = push ( a , 7 ) ; a = push ( a , 6 ) ; a = push ( a , 5 ) ; Node b = null ; b = push ( b , 3 ) ; b = push ( b , 3 ) ; b = push ( b , 2 ) ; Console . WriteLine ( compare ( a , b ) ) ; } }


"Compare sum of first N | C # implementation of the approach ; Function that returns true if sum of first n - 1 elements of the array is equal to the last element ; Find the sum of first n - 1 elements of the array ; If sum equals to the last element ; Driver code"
Solution in C#:

using System ; class GFG { static bool isSumEqual ( int [ ] ar , int n ) { int sum = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) sum += ar [ i ] ; if ( sum == ar [ n - 1 ] ) return true ; return false ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 10 } ; int n = arr . Length ; if ( isSumEqual ( arr , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Compare two Version numbers | C # program to compare two version number ; Method to compare two versions . Returns 1 if v2 is smaller , - 1 if v1 is smaller , 0 if equal ; vnum stores each numeric part of version ; loop until both string are processed ; storing numeric part of version 1 in vnum1 ; storing numeric part of version 2 in vnum2 ; if equal , reset variables and go for next numeric part ; Driver code"
Solution in C#:

using System ; class GFG { static int versionCompare ( string v1 , string v2 ) { int vnum1 = 0 , vnum2 = 0 ; for ( int i = 0 , j = 0 ; ( i < v1 . Length < v2 . Length ) ; ) { while ( i < v1 . Length && v1 [ i ] != ' . ' ) { vnum1 = vnum1 * 10 + ( v1 [ i ] - '0' ) ; i ++ ; } while ( j < v2 . Length && v2 [ j ] != ' . ' ) { vnum2 = vnum2 * 10 + ( v2 [ j ] - '0' ) ; j ++ ; } if ( vnum1 > vnum2 ) return 1 ; if ( vnum2 > vnum1 ) return - 1 ; vnum1 = vnum2 = 0 ; i ++ ; j ++ ; } return 0 ; } static void Main ( ) { string version1 = "1.0.3" ; string version2 = "1.0.7" ; if ( versionCompare ( version1 , version2 ) < 0 ) Console . WriteLine ( version1 + " ▁ is ▁ smaller " ) ; else if ( versionCompare ( version1 , version2 ) > 0 ) Console . WriteLine ( version2 + " ▁ is ▁ smaller " ) ; else Console . WriteLine ( " Both ▁ version ▁ are ▁ equal " ) ; } }


"Compare two integers without using any Comparison operator | C # program to compare two integers without any comparison operator . ; function return true if A ^ B > 0 else false ; Driver Code"
Solution in C#:

using System ; class GFG { static bool EqualNumber ( int A , int B ) { if ( ( A ^ B ) > 0 ) return true ; else return false ; } public static void Main ( ) { int A = 5 , B = 6 ; if ( ! EqualNumber ( A , B ) == false ) Console . WriteLine ( "0" ) ; else Console . WriteLine ( "1" ) ; } }


"Comparing leading zeros in binary representations of two numbers | C # program to find the number with more leading zeroes . ; Function to compare the no . of leading zeros ; if both have same no . of leading zeros ; if y has more leading zeros ; Driver Code"
Solution in C#:

 using System ; class GFG { static void LeadingZeros ( int x , int y ) { if ( ( x ^ y ) <= ( x & y ) ) Console . WriteLine ( "
 Equal " ) ; else if ( ( x & ( ~ y ) ) > y ) Console . WriteLine ( y ) ; else Console . WriteLine ( x ) ; } static public void Main ( ) { int x = 10 , y = 16 ; LeadingZeros ( x , y ) ; } }


"Complement of a number with any base b | C # program to find complement of a number with any base b ; Function to find ( b - 1 ) 's complement ; Calculate number of digits in the given number ; Largest digit in the number system with base b ; Largest number in the number system with base b ; return Complement ; Function to find b 's complement ; b ' s ▁ complement ▁ = ▁ ( b - 1 ) ' s complement + 1 ; Driver code"
Solution in C#:

class GFG { static int prevComplement ( int n , int b ) { int maxDigit , maxNum = 0 , digits = 0 , num = n ; while ( n != 0 ) { digits ++ ; n = n / 10 ; } maxDigit = b - 1 ; while ( ( digits -- ) > 0 ) { maxNum = maxNum * 10 + maxDigit ; } return maxNum - num ; } static int complement ( int n , int b ) { return prevComplement ( n , b ) + 1 ; } public static void Main ( ) { System . Console . WriteLine ( prevComplement ( 25 , 7 ) ) ; System . Console . WriteLine ( complement ( 25 , 7 ) ) ; } }


"Composite numbers with digit sum 1 | C # implementation of the above approach ; Function that returns true if number n is a composite number ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if the eventual digit sum of number nm is 1 ; Loop till the sum is not single digit number ; Intitialize the sum as zero ; Find the sum of digits ; If sum is eventually 1 ; Function to print the required numbers from the given range ; If i is one of the required numbers ; Driver code"
Solution in C#:

using System ; class GFG { static bool isComposite ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } static bool isDigitSumOne ( int nm ) { while ( nm > 9 ) { int sum_digit = 0 ; while ( nm > 0 ) { int digit = nm % 10 ; sum_digit = sum_digit + digit ; nm = nm / 10 ; } nm = sum_digit ; } if ( nm == 1 ) return true ; else return false ; } static void printValidNums ( int l , int r ) { for ( int i = l ; i <= r ; i ++ ) { if ( isComposite ( i ) && isDigitSumOne ( i ) ) Console . Write ( i + " ▁ " ) ; } } static public void Main ( ) { int l = 10 , r = 100 ; printValidNums ( l , r ) ; } }


"Compress the array into Ranges | C # program to compress the array ranges ; Function to compress the array ranges ; start iteration from the ith array element ; loop until arr [ i + 1 ] == arr [ i ] and increment j ; if the program do not enter into the above while loop this means that ( i + 1 ) th element is not consecutive to i th element ; increment i for next iteration ; print the consecutive range found ; move i jump directly to j + 1 ; Driver code"
Solution in C#:

using System ; class GFG { static void compressArr ( int [ ] arr , int n ) { int i = 0 , j = 0 ; Array . Sort ( arr ) ; while ( i < n ) { j = i ; while ( ( j + 1 < n ) && ( arr [ j + 1 ] == arr [ j ] + 1 ) ) { j ++ ; } if ( i == j ) { Console . Write ( arr [ i ] + " ▁ " ) ; i ++ ; } else { Console . Write ( arr [ i ] + " - " + arr [ j ] + " ▁ " ) ; i = j + 1 ; } } } public static void Main ( ) { int n = 7 ; int [ ] arr = { 1 , 3 , 4 , 5 , 6 , 9 , 10 } ; compressArr ( arr , n ) ; } }


"Compute average of two numbers without overflow | C # code to compute average of two numbers ; Function to compute average of two numbers ; Driver code ; Assigning maximum integer value ; Average of two equal numbers is the same number ; Function to get the average of 2 numbers"
Solution in C#:

using System ; public class GFG { static int compute_average ( int a , int b ) { return ( a + b ) / 2 ; } static public void Main ( ) { int a = int . MaxValue ; int b = int . MaxValue ; Console . WriteLine ( " Actual ▁ average ▁ : ▁ " + int . MaxValue ) ; Console . WriteLine ( " Computed ▁ average ▁ : ▁ " + compute_average ( a , b ) ) ; } }


"Compute n ! under modulo p | C # program Returns n % p using Sieve of Eratosthenes ; Returns largest power of p that divides n ! ; Initialize result ; Calculate x = n / p + n / ( p ^ 2 ) + n / ( p ^ 3 ) + ... . ; Utility function to do modular exponentiation . It returns ( x ^ y ) % p ; Initialize result Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Returns n ! % p ; Use Sieve of Eratosthenes to find all primes smaller than n ; Consider all primes found by Sieve ; Find the largest power of prime ' i ' that divides n ; Multiply result with ( i ^ k ) % p ; Driver method"
Solution in C#:

using System ; public class GFG { static int largestPower ( int n , int p ) { int x = 0 ; while ( n > 0 ) { n /= p ; x += n ; } return x ; } static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) { res = ( res * x ) % p ; } x = ( x * x ) % p ; } return res ; } static int modFact ( int n , int p ) { if ( n >= p ) { return 0 ; } int res = 1 ; bool [ ] isPrime = new bool [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) isPrime [ i ] = true ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( isPrime [ i ] ) { for ( int j = 2 * i ; j <= n ; j += i ) { isPrime [ j ] = false ; } } } for ( int i = 2 ; i <= n ; i ++ ) { if ( isPrime [ i ] ) { int k = largestPower ( n , i ) ; res = ( res * power ( i , k , p ) ) % p ; } } return res ; } static public void Main ( ) { int n = 25 , p = 29 ; Console . WriteLine ( modFact ( n , p ) ) ; } }


"Compute n ! under modulo p | C # program to comput n ! % p using Wilson 's Theorem ; Utility function to do modular exponentiation . It returns ( x ^ y ) % p ; int res = 1 ; Initialize result x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Function to find modular inverse of a under modulo p using Fermat 's method. Assumption: p is prime ; Returns n ! % p using Wilson 's Theorem ; n ! % p is 0 if n >= p ; Initialize result as ( p - 1 ) ! which is - 1 or ( p - 1 ) ; Multiply modulo inverse of all numbers from ( n + 1 ) to p ; Driver method"
Solution in C#:

using System ; class GFG { public int power ( int x , int y , int p ) { while ( y > 0 ) { if ( ( y & 1 ) > 0 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } public int modInverse ( int a , int p ) { return power ( a , p - 2 , p ) ; } public int modFact ( int n , int p ) { if ( p <= n ) return 0 ; int res = ( p - 1 ) ; for ( int i = n + 1 ; i < p ; i ++ ) res = ( res * modInverse ( i , p ) ) % p ; return res ; } public static void Main ( ) { GFG g = new GFG ( ) ; int n = 25 , p = 29 ; Console . WriteLine ( g . modFact ( n , p ) ) ; } }


"Compute n ! under modulo p | Simple method to compute n ! % p ; Returns value of n ! % p ; Driver program"
Solution in C#:

using System ; class GFG { static int modFact ( int n , int p ) { if ( n >= p ) return 0 ; int result = 1 ; for ( int i = 1 ; i <= n ; i ++ ) result = ( result * i ) % p ; return result ; } static void Main ( ) { int n = 25 , p = 29 ; Console . Write ( modFact ( n , p ) ) ; } }


"Compute nCr % p | Set 1 ( Introduction and Dynamic Programming Solution ) | A Dynamic Programming based solution to compute nCr % p ; Returns nCr % p ; Optimization for the cases when r is large ; The array C is going to store last row of pascal triangle at the end . And last entry of last row is nCr ; Top row of Pascal Triangle ; One by constructs remaining rows of Pascal Triangle from top to bottom ; Fill entries of current row using previous row values ; nCj = ( n - 1 ) Cj + ( n - 1 ) C ( j - 1 ) ; ; Driver program"
Solution in C#:

using System ; class GFG { static int nCrModp ( int n , int r , int p ) { if ( r > n - r ) r = n - r ; int [ ] C = new int [ r + 1 ] ; for ( int i = 0 ; i < r + 1 ; i ++ ) C [ i ] = 0 ; C [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . Min ( i , r ) ; j > 0 ; j -- ) C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p ; } return C [ r ] ; } public static void Main ( ) { int n = 10 , r = 2 , p = 13 ; Console . Write ( " Value ▁ of ▁ nCr ▁ % ▁ p ▁ is ▁ " + nCrModp ( n , r , p ) ) ; } }


"Compute nCr % p | Set 2 ( Lucas Theorem ) | A Lucas Theorem based solution to compute nCr % p ; Returns nCr % p . In this Lucas Theorem based program , this function is only called for n < p and r < p . ; The array C is going to store last row of pascal triangle at the end . And last entry of last row is nCr ; One by constructs remaining rows of Pascal Triangle from top to bottom ; Fill entries of current row using previous row values ; nCj = ( n - 1 ) Cj + ( n - 1 ) C ( j - 1 ) ; ; Lucas Theorem based function that returns nCr % p . This function works like decimal to binary conversion recursive function . First we compute last digits of n and r in base p , then recur for remaining digits ; Base case ; Compute last digits of n and r in base p ; Compute result for last digits computed above , and for remaining digits . Multiply the two results and compute the result of multiplication in modulo p . return ( nCrModpLucas ( n / p , r / p , p ) * Last digits of n and r nCrModpDP ( ni , ri , p ) ) % p ; Remaining digits ; Driver Code"
Solution in C#:

using System ; class GFG { static int nCrModpDP ( int n , int r , int p ) { int [ ] C = new int [ r + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . Min ( i , r ) ; j > 0 ; j -- ) C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p ; } return C [ r ] ; } static int nCrModpLucas ( int n , int r , int p ) { if ( r == 0 ) return 1 ; int ni = n % p ; int ri = r % p ; } public static void Main ( ) { int n = 1000 , r = 900 , p = 13 ; Console . Write ( " Value ▁ of ▁ nCr ▁ % ▁ p ▁ is ▁ " + nCrModpLucas ( n , r , p ) ) ; } }


"Compute power of power k times % m | C # program for computing x ^ x ^ x ^ x . . % m ; Create an array to store phi or totient values ; Function to calculate Euler Totient values ; indicates not evaluated yet and initializes for product formula . ; Compute other Phi values ; If phi [ p ] is not computed already , then number p is prime ; Phi of a prime number p is always equal to p - 1. ; Update phi values of all multiples of p ; Add contribution of p to its multiple i by multiplying with ( 1 - 1 / p ) ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Function to calculate ( x ^ x ^ x ^ x ... k times ) % m ; to store different mod values ; run loop in reverse to calculate result ; Driver Code ; compute euler totient function values ; Calling function to compute answer"
Solution in C#:

using System ; class GFG { static int N = 1000000 ; static long [ ] phi = new long [ N + 5 ] ; static void computeTotient ( ) { for ( int i = 1 ; i <= N ; i ++ ) phi [ i ] = i ; for ( int p = 2 ; p <= N ; p ++ ) { if ( phi [ p ] == p ) { phi [ p ] = p - 1 ; for ( int i = 2 * p ; i <= N ; i += p ) { phi [ i ] = ( phi [ i ] / p ) * ( p - 1 ) ; } } } } static long power ( long x , long y , long p ) { while ( y > 0 ) { if ( ( y & 1 ) > 0 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static long calculate ( long x , long k , long mod ) { long [ ] arr = new long [ N ] ; long count = 0 ; while ( mod > 1 ) { arr [ ( int ) count ++ ] = mod ; mod = phi [ ( int ) mod ] ; } long result = 1 ; long loop = count + 1 ; arr [ ( int ) count ] = 1 ; for ( int i = ( int ) Math . Min ( k , loop ) - 1 ; i >= 0 ; i -- ) result = power ( x , result , arr [ i ] ) ; return result ; } static public void Main ( ) { computeTotient ( ) ; long x = 3 , k = 2 , m = 3 ; Console . WriteLine ( calculate ( x , k , m ) ) ; } }


"Compute power of power k times % m | C # program for computing x ^ x ^ x ^ x . . % m ; Function to compute the given value ; compute power k times ; Driver Code ; Calling function"
Solution in C#:

using System ; class GFG { static int calculate ( int x , int k , int m ) { int result = x ; k -- ; while ( k -- > 0 ) { result = ( int ) Math . Pow ( result , x ) ; if ( result > m ) result %= m ; } return result ; } static public void Main ( ) { int x = 5 , k = 2 , m = 3 ; Console . WriteLine ( calculate ( x , k , m ) ) ; } }


"Compute sum of digits in all numbers from 1 to n | A Simple C # program to compute sum of digits in numbers from 1 to n ; Returns sum of all digits in numbers from 1 to n ; initialize result ; One by one compute sum of digits in every number from 1 to n ; A utility function to compute sum of digits in a given number x ; Driver Program"
Solution in C#:

using System ; public class GFG { static int sumOfDigitsFrom1ToN ( int n ) { int result = 0 ; for ( int x = 1 ; x <= n ; x ++ ) result += sumOfDigits ( x ) ; return result ; } static int sumOfDigits ( int x ) { int sum = 0 ; while ( x != 0 ) { sum += x % 10 ; x = x / 10 ; } return sum ; } public static void Main ( ) { int n = 328 ; Console . WriteLine ( " Sum ▁ of ▁ digits " + " ▁ in ▁ numbers ▁ from ▁ 1 ▁ to ▁ " + n + " ▁ is ▁ " + sumOfDigitsFrom1ToN ( n ) ) ; } }