"Find the average of first N natural numbers | C # Program to find the Average of first n natural numbers ; Return the average of first n natural numbers ; Driven Program"
Solution in C#:

using System ; class GFG { static float avgOfFirstN ( int n ) { return ( float ) ( 1 + n ) / 2 ; } public static void Main ( ) { int n = 20 ; Console . WriteLine ( avgOfFirstN ( n ) ) ; } }


"Find the average of k digits from the beginning and l digits from the end of the given number | C # implementation of the approach ; Function to return the count of digits in num ; Function to return the sum of first n digits of num ; Remove the unnecessary digits ; Function to return the sum of the last n digits of num ; If the average can 't be calculated without  using the same digit more than once ; Sum of the last l digits of n ; Sum of the first k digits of n ( totalDigits - k ) must be removed from the end of the number to get the remaining k digits from the beginning ; Return the average ; Driver code"
Solution in C#:

using System ; class GFG { public static int countDigits ( int num ) { int cnt = 0 ; while ( num > 0 ) { cnt ++ ; num /= 10 ; } return cnt ; } public static int sumFromStart ( int num , int n , int rem ) { num /= ( ( int ) Math . Pow ( 10 , rem ) ) ; int sum = 0 ; while ( num > 0 ) { sum += ( num % 10 ) ; num /= 10 ; } return sum ; } public static int sumFromEnd ( int num , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ( num % 10 ) ; num /= 10 ; } return sum ; } public static float getAverage ( int n , int k , int l ) { int totalDigits = countDigits ( n ) ; if ( totalDigits < ( k + l ) ) return - 1 ; int sum1 = sumFromEnd ( n , l ) ; int sum2 = sumFromStart ( n , k , totalDigits - k ) ; return ( ( float ) ( sum1 + sum2 ) / ( float ) ( k + l ) ) ; } public static void Main ( String [ ] args ) { int n = 123456 , k = 2 , l = 3 ; Console . WriteLine ( getAverage ( n , k , l ) ) ; } }


"Find the center of the circle using endpoints of diameter | C # program to find the center of the circle ; function to find the center of the circle ; Driver Program to test above function"
Solution in C#:

using System ; class GFG { static void center ( int x1 , int x2 , int y1 , int y2 ) { Console . WriteLine ( ( float ) ( x1 + x2 ) / 2 + " , ▁ " + ( float ) ( y1 + y2 ) / 2 ) ; } public static void Main ( ) { int x1 = - 9 , y1 = 3 , x2 = 5 , y2 = - 7 ; center ( x1 , x2 , y1 , y2 ) ; } }


"Find the character in first string that is present at minimum index in second string | C # implementation to find the character in first string that is present at minimum index in second string ; method to find the minimum index character ; map to store the first index of each character of ' str ' ; to store the index of character having minimum index ; lengths of the two strings ; store the first index of each character of ' str ' ; traverse the string ' patt ' ; if patt [ i ] is found in ' um ' , check if it has the minimum index or not , accordingly update ' minIndex ' ; print the minimum index character ; if no character of ' patt ' is present in ' str ' ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printMinIndexChar ( String str , String patt ) { Dictionary < char , int > hm = new Dictionary < char , int > ( ) ; int minIndex = int . MaxValue ; int m = str . Length ; int n = patt . Length ; for ( int i = 0 ; i < m ; i ++ ) if ( ! hm . ContainsKey ( str [ i ] ) ) hm . Add ( str [ i ] , i ) ; for ( int i = 0 ; i < n ; i ++ ) if ( hm . ContainsKey ( patt [ i ] ) && hm [ patt [ i ] ] < minIndex ) minIndex = hm [ patt [ i ] ] ; if ( minIndex != int . MaxValue ) Console . WriteLine ( " Minimum ▁ Index ▁ Character ▁ = ▁ " + str [ minIndex ] ) ; else Console . WriteLine ( " No ▁ character ▁ present " ) ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; String patt = " set " ; printMinIndexChar ( str , patt ) ; } }


"Find the character in first string that is present at minimum index in second string | C # implementation to find the character in first string that is present at minimum index in second string ; method to find the minimum index character ; to store the index of character having minimum index ; lengths of the two strings ; traverse ' patt ' ; for each character of ' patt ' traverse ' str ' ; if patt . charAt ( i ) is found in ' str ' , check if it has the minimum index or not . If yes , then update ' minIndex ' and break ; print the minimum index character ; if no character of ' patt ' is present in ' str ' ; Driver Methoda"
Solution in C#:

using System ; class GFG { static void printMinIndexChar ( String str , String patt ) { int minIndex = int . MaxValue ; int m = str . Length ; int n = patt . Length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( patt [ i ] == str [ j ] && j < minIndex ) { minIndex = j ; break ; } } } if ( minIndex != int . MaxValue ) Console . WriteLine ( " Minimum ▁ Index ▁ Character ▁ = ▁ " + str [ minIndex ] ) ; else Console . WriteLine ( " No ▁ character ▁ present " ) ; } public static void Main ( ) { String str = " geeksforgeeks " ; String patt = " set " ; printMinIndexChar ( str , patt ) ; } }


"Find the character made by adding all the characters of the given string | C # implementation of the approach ; Function to return the required character ; To store the sum of the characters of the given string ; Add the current character to the sum ; Return the required character ; Driver code"
Solution in C#:

using System ; class GFG { static char getChar ( String str ) { int sum = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { sum += ( str [ i ] - ' a ' + 1 ) ; } if ( sum % 26 == 0 ) return ' z ' ; else { sum = sum % 26 ; return ( char ) ( ' a ' + sum - 1 ) ; } } public static void Main ( String [ ] args ) { String str = " gfg " ; Console . WriteLine ( getChar ( str ) ) ; } }


"Find the closest Fraction to given fraction having minimum absolute difference | C # code for the above approach ; Function to find the absolute value of x ; Function to find the fraction with minimum absolute difference ; Initialize the answer variables ; Iterate over the range ; Nearest fraction ; x / y - d / i < x / y - A / B ( B * x - y * A ) * ( i * y ) > ( i * x - y * d ) * ( B * y ) ; Check for d + 1 ; Print the answer ; Driver Code"
Solution in C#:

using System ; public class GFG { static long ABS ( long x ) { return Math . Max ( x , - x ) ; } static void findFraction ( long x , long y , long n ) { long A = - 1 , B = - 1 ; for ( long i = 1 ; i <= n ; i ++ ) { long d = ( i * x ) / y ; if ( d >= 0 && ( A == - 1 || ABS ( B * x - y * A ) * ABS ( i * y ) > ABS ( i * x - y * d ) * ABS ( B * y ) ) ) A = d ; B = i ; d ++ ; if ( d >= 0 && ( A == - 1 || ABS ( B * x - y * A ) * ABS ( i * y ) > ABS ( i * x - y * d ) * ABS ( B * y ) ) ) A = d ; B = i ; } A -- ; B -- ; Console . Write ( A + " / " + B ) ; } static public void Main ( ) { long x = 3 ; long y = 7 ; long n = 6 ; findFraction ( x , y , n ) ; } }


"Find the closest and smaller tidy number | C # program to find closest tidy number smaller than the given number ; check whether string violates tidy property ; if string violates tidy property , then decrease the value stored at that index by 1 and replace all the value stored right to that index by 9 ; Driver code ; num will store closest tidy number"
Solution in C#:

using System ; class GFG { static String tidyNum ( String str1 , int len ) { char [ ] str = str1 . ToCharArray ( ) ; for ( int i = len - 2 ; i >= 0 ; i -- ) { if ( str [ i ] > str [ i + 1 ] ) { str [ i ] -- ; for ( int j = i + 1 ; j < len ; j ++ ) str [ j ] = '9' ; } } string s = new string ( str ) ; return s ; } static void Main ( ) { String str = "11333445538" ; int len = str . Length ; Console . WriteLine ( tidyNum ( str , len ) ) ; } }


"Find the closest element in Binary Search Tree | Recursive C # program to find key closest to k in given Binary Search Tree . ; A binary tree node has key , pointer to left child and a pointer to right child ; Utility that allocates a new node with the given key and null left and right pointers . ; Function to find node with minimum absolute difference with given K min_diff - . minimum difference till now min_diff_key - . node having minimum absolute difference with K ; If k itself is present ; update min_diff and min_diff_key by checking current node value ; if k is less than ptr . key then move in left subtree else in right subtree ; Wrapper over maxDiffUtil ( ) ; Initialize minimum difference ; Find value of min_diff_key ( Closest key in tree with k ) ; Driver program to run the case"
Solution in C#:

using System ; public class solution { public static int min_diff , min_diff_key ; public class Node { public int key ; public Node left , right ; } public static Node newnode ( int key ) { Node node = new Node ( ) ; node . key = key ; node . left = node . right = null ; return ( node ) ; } public static void maxDiffUtil ( Node ptr , int k ) { if ( ptr == null ) { return ; } if ( ptr . key == k ) { min_diff_key = k ; return ; } if ( min_diff > Math . Abs ( ptr . key - k ) ) { min_diff = Math . Abs ( ptr . key - k ) ; min_diff_key = ptr . key ; } if ( k < ptr . key ) { maxDiffUtil ( ptr . left , k ) ; } else { maxDiffUtil ( ptr . right , k ) ; } } public static int maxDiff ( Node root , int k ) { min_diff = 999999999 ; min_diff_key = - 1 ; maxDiffUtil ( root , k ) ; return min_diff_key ; } public static void Main ( string [ ] args ) { Node root = newnode ( 9 ) ; root . left = newnode ( 4 ) ; root . right = newnode ( 17 ) ; root . left . left = newnode ( 3 ) ; root . left . right = newnode ( 6 ) ; root . left . right . left = newnode ( 5 ) ; root . left . right . right = newnode ( 7 ) ; root . right . right = newnode ( 22 ) ; root . right . right . left = newnode ( 20 ) ; int k = 18 ; Console . WriteLine ( maxDiff ( root , k ) ) ; } }


"Find the closest leaf in a Binary Tree | C # program to find closest leaf of a given key in Binary Tree ; Class containing left and right child of current node and key value ; A utility function to find minimum of x and y ; A utility function to find distance of closest leaf of the tree rooted under given root ; Base cases ; Return minimum of left and right , plus one ; Returns distance of the cloest leaf to a given key ' k ' . The array ancestors is used to keep track of ancestors of current node and ' index ' is used to keep track of curremt index in ' ancestors [ ] ' ; Base case ; If key found ; Find the cloest leaf under the subtree rooted with given key ; Traverse all ancestors and update result if any parent node gives smaller distance ; If key node found , store current node and recur for left and right childrens ; The main function that returns distance of the closest key to ' k ' . It mainly uses recursive function findClosestUtil ( ) to find the closes distance . ; Create an array to store ancestors Assumptiom : Maximum height of tree is 100 ; Driver program to test for above functions ; Let us construct the BST shown in the above figure"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { public Node root ; public virtual int getMin ( int x , int y ) { return ( x < y ) ? x : y ; } public virtual int closestDown ( Node node ) { if ( node == null ) { return int . MaxValue ; } if ( node . left == null && node . right == null ) { return 0 ; } return 1 + getMin ( closestDown ( node . left ) , closestDown ( node . right ) ) ; } public virtual int findClosestUtil ( Node node , char k , Node [ ] ancestors , int index ) { if ( node == null ) { return int . MaxValue ; } if ( ( char ) node . data == k ) { int res = closestDown ( node ) ; for ( int i = index - 1 ; i >= 0 ; i -- ) { res = getMin ( res , index - i + closestDown ( ancestors [ i ] ) ) ; } return res ; } ancestors [ index ] = node ; return getMin ( findClosestUtil ( node . left , k , ancestors , index + 1 ) , findClosestUtil ( node . right , k , ancestors , index + 1 ) ) ; } public virtual int findClosest ( Node node , char k ) { Node [ ] ancestors = new Node [ 100 ] ; return findClosestUtil ( node , k , ancestors , 0 ) ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( ' A ' ) ; tree . root . left = new Node ( ' B ' ) ; tree . root . right = new Node ( ' C ' ) ; tree . root . right . left = new Node ( ' E ' ) ; tree . root . right . right = new Node ( ' F ' ) ; tree . root . right . left . left = new Node ( ' G ' ) ; tree . root . right . left . left . left = new Node ( ' I ' ) ; tree . root . right . left . left . right = new Node ( ' J ' ) ; tree . root . right . right . right = new Node ( ' H ' ) ; tree . root . right . right . right . left = new Node ( ' H ' ) ; char k = ' H ' ; Console . WriteLine ( " Distance ▁ of ▁ the ▁ closest ▁ key ▁ from ▁ " + k + " ▁ is ▁ " + tree . findClosest ( tree . root , k ) ) ; k = ' C ' ; Console . WriteLine ( " Distance ▁ of ▁ the ▁ closest ▁ key ▁ from ▁ " + k + " ▁ is ▁ "...


"Find the closest pair from two sorted arrays | C # program to find closest pair in an array ; ar1 [ 0. . m - 1 ] and ar2 [ 0. . n - 1 ] are two given sorted arrays / and x is given number . This function prints the pair from both arrays such that the sum of the pair is closest to x . ; Initialize the diff between pair sum and x . ; res_l and res_r are result indexes from ar1 [ ] and ar2 [ ] respectively ; Start from left side of ar1 [ ] and right side of ar2 [ ] ; If this pair is closer to x than the previously found closest , then update res_l , res_r and diff ; If sum of this pair is more than x , move to smaller side ; move to the greater side ; Print the result ; Driver program to test above functions"
Solution in C#:

using System ; class GFG { static void printClosest ( int [ ] ar1 , int [ ] ar2 , int m , int n , int x ) { int diff = int . MaxValue ; int res_l = 0 , res_r = 0 ; int l = 0 , r = n - 1 ; while ( l < m && r >= 0 ) { if ( Math . Abs ( ar1 [ l ] + ar2 [ r ] - x ) < diff ) { res_l = l ; res_r = r ; diff = Math . Abs ( ar1 [ l ] + ar2 [ r ] - x ) ; } if ( ar1 [ l ] + ar2 [ r ] > x ) r -- ; else l ++ ; } Console . Write ( " The ▁ closest ▁ pair ▁ is ▁ [ " + ar1 [ res_l ] + " , ▁ " + ar2 [ res_r ] + " ] " ) ; } public static void Main ( ) { int [ ] ar1 = { 1 , 4 , 5 , 7 } ; int [ ] ar2 = { 10 , 20 , 30 , 40 } ; int m = ar1 . Length ; int n = ar2 . Length ; int x = 38 ; printClosest ( ar1 , ar2 , m , n , x ) ; } }


"Find the color of given node in an infinite binary tree | C # program to find color of the node ; Function to find color of the node ; Maximum is to store maximum color ; Loop to check all the parent values to get maximum color ; Find the number into map and get maximum color ; Take the maximum color and assign into maximum variable ; Find parent index ; Return maximum color ; Function to build hash map with color ; To store color of each node ; For each number add a color number ; Assigning color ; Return hash map ; Driver code ; Build mapWithColor ; Print the maximum color"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int findColor ( Dictionary < int , int > mapWithColor , int query ) { int maximum = 0 ; while ( query >= 1 ) { if ( mapWithColor . ContainsKey ( query ) ) { maximum = Math . Max ( maximum , mapWithColor [ query ] ) ; } if ( query % 2 == 1 ) query = ( query - 1 ) / 2 ; else query = query / 2 ; } return maximum ; } static Dictionary < int , int > buildMapWithColor ( int [ ] arr , int n ) { Dictionary < int , int > mapWithColor = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mapWithColor . ContainsKey ( arr [ i ] ) ) mapWithColor [ arr [ i ] ] = i + 1 ; else mapWithColor . Add ( arr [ i ] , i + 1 ) ; } return mapWithColor ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 , 3 } ; int n = arr . Length ; int k = 7 ; Dictionary < int , int > mapWithColor = buildMapWithColor ( arr , n ) ; Console . Write ( findColor ( mapWithColor , k ) ) ; } }


"Find the concentration of a solution using given Mass and Volume | C # program to find concentration of a solution using given Mass and Volume ; Function to calculate concentration from the given mass of solute and volume of a solution ; Driver code"
Solution in C#:

using System ; class GFG { static double get_concentration ( double mass , double volume ) { if ( volume == 0 ) return - 1 ; else return ( mass / volume ) * 1000 ; } public static void Main ( ) { double mass , volume ; mass = 100.00 ; volume = 500.00 ; Console . Write ( get_concentration ( mass , volume ) ) ; } }


"Find the conjugate of a Complex number | C # implementation to find the conjugate of a complex number ; Function to find conjugate of a complex number ; Store index of ' + ' ; Store index of ' - ' ; print the result ; Driver code ; Initialise the complex number"
Solution in C#:

using System ; class GFG { static void solve ( String s ) { String z = s ; int l = s . Length ; int i ; String str ; if ( s . IndexOf ( ' + ' ) != - 1 ) { i = s . IndexOf ( ' + ' ) ; str = s . Replace ( ' + ' , ' - ' ) ; } else { i = s . IndexOf ( ' - ' ) ; str = s . Replace ( ' - ' , ' + ' ) ; } Console . WriteLine ( " Conjugate ▁ of ▁ " + z + " ▁ = ▁ " + str ) ; } public static void Main ( String [ ] args ) { String s = "3-4i " ; solve ( s ) ; } }


"Find the coordinates of a triangle whose Area = ( S / 2 ) | C # implementation of the above approach ; Function to find the triangle with area = ( S / 2 ) ; Fix the two pairs of coordinates ; Find ( X3 , Y3 ) with integer coordinates ; Driver code"
Solution in C#:

using System ; class GFG { static readonly long MAX = 1000000000 ; static void findTriangle ( long S ) { long X1 = 0 , Y1 = 0 ; long X2 = MAX , Y2 = 1 ; long X3 = ( MAX - S % MAX ) % MAX ; long Y3 = ( S + X3 ) / MAX ; Console . WriteLine ( " ( " + X1 + " , ▁ " + Y1 + " ) " ) ; Console . WriteLine ( " ( " + X2 + " , ▁ " + Y2 + " ) " ) ; Console . WriteLine ( " ( " + X3 + " , ▁ " + Y3 + " ) " ) ; } public static void Main ( String [ ] args ) { long S = 4 ; findTriangle ( S ) ; } }


"Find the coordinates of the fourth vertex of a rectangle with given 3 vertices | C # implementation of the approach ; Function that return the coordinates of the fourth vertex of the rectangle ; Save the coordinates of the given vertices of the rectangle ; 1 - based indexing ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GfG { static Pair < int , int > findFourthVertex ( int n , int m , String [ ] s ) { Dictionary < int , int > row = new Dictionary < int , int > ( ) ; Dictionary < int , int > col = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( s [ i ] [ j ] == ' * ' ) { if ( row . ContainsKey ( i ) ) { row [ i ] = row [ i ] + 1 ; } else { row . Add ( i , 1 ) ; } if ( col . ContainsKey ( j ) ) { col [ j ] = col [ j ] + 1 ; } else { col . Add ( j , 1 ) ; } } } } int x = 0 , y = 0 ; foreach ( KeyValuePair < int , int > entry in row ) { if ( entry . Value == 1 ) x = entry . Key ; } foreach ( KeyValuePair < int , int > entry in col ) { if ( entry . Value == 1 ) y = entry . Key ; } Pair < int , int > ans = new Pair < int , int > ( x + 1 , y + 1 ) ; return ans ; } public static void Main ( String [ ] args ) { String [ ] s = { " * . * " , " * . . " , " . . . " } ; int n = s . Length ; int m = s [ 0 ] . Length ; Pair < int , int > rs = findFourthVertex ( n , m , s ) ; Console . WriteLine ( rs . first + " ▁ " + rs . second ) ; } } class Pair < A , B > { public A first ; public B second ; public Pair ( A first , B second ) { this . first = first ; this . second = second ; } }


"Find the count of M character words which have at least one character repeated | C # implementation of the approach ; Function to return the factorial of a number ; Function to return the value of nPr ; Function to return the total number of M length words which have at least a single character repeated more than once ; Driver code"
Solution in C#:

using System ; class GFG { static int fact ( int n ) { if ( n <= 1 ) return 1 ; return n * fact ( n - 1 ) ; } static int nPr ( int n , int r ) { return fact ( n ) / fact ( n - r ) ; } static int countWords ( int N , int M ) { return ( int ) Math . Pow ( N , M ) - nPr ( N , M ) ; } static public void Main ( ) { int N = 10 , M = 5 ; Console . Write ( countWords ( N , M ) ) ; } }


"Find the count of Smith Brothers Pairs in a given Array | C # program for the above approach ; array to store all prime less than and equal to MAX ; utility function for sieve of sundaram ; Main logic of Sundaram . ; Since 2 is a prime number ; only primes are selected ; Function to check whether a number is a smith number . ; Find sum the digits of prime factors of n ; add its digits of prime factors to pDigitSum . ; one prime factor is still to be summed up ; Now sum the digits of the original number ; return the answer ; Function to check if X and Y are a Smith Brother Pair ; Function to find pairs from array ; Iterate through all pairs ; Increment count if there is a smith brother pair ; Driver code ; Preprocessing ; Input ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MAX = 10000 ; static List < int > primes = new List < int > ( ) ; static void sieveSundaram ( ) { int [ ] marked = new int [ MAX / 2 + 100 ] ; Array . Clear ( marked , 0 , MAX / 2 + 100 ) ; for ( int i = 1 ; i <= ( int ) ( Math . Sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked [ j ] = 1 ; primes . Add ( 2 ) ; for ( int i = 1 ; i <= ( int ) MAX / 2 ; i ++ ) if ( marked [ i ] == 0 ) primes . Add ( 2 * i + 1 ) ; } static bool isSmith ( int n ) { int original_no = n ; int pDigitSum = 0 ; for ( int i = 0 ; primes [ i ] <= n / 2 ; i ++ ) { while ( n % primes [ i ] == 0 ) { int p = primes [ i ] ; n = n / p ; while ( p > 0 ) { pDigitSum += ( p % 10 ) ; p = p / 10 ; } } } if ( n != 1 && n != original_no ) { while ( n > 0 ) { pDigitSum = pDigitSum + n % 10 ; n = n / 10 ; } } int sumDigits = 0 ; while ( original_no > 0 ) { sumDigits = sumDigits + original_no % 10 ; original_no = original_no / 10 ; } return ( pDigitSum == sumDigits ) ; } static bool isSmithBrotherPair ( int X , int Y ) { return isSmith ( X ) && isSmith ( Y ) && Math . Abs ( X - Y ) == 1 ; } static int countSmithBrotherPairs ( int [ ] A , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N ; j ++ ) { if ( isSmithBrotherPair ( A [ i ] , A [ j ] ) ) count ++ ; } return count ; } public static void Main ( ) { sieveSundaram ( ) ; int [ ] A = { 728 , 729 , 28 , 2964 , 2965 } ; int N = A . Length ; Console . Write ( countSmithBrotherPairs ( A , N ) ) ; } }


"Find the count of Strictly decreasing Subarrays | C # program to count number of strictly decreasing subarrays in O ( n ) time . ; Function to count the number of strictly decreasing subarrays ; Initialize length of current decreasing subarray ; Traverse through the array ; If arr [ i + 1 ] is less than arr [ i ] , then increment length ; Else Update count and reset length ; If last length is more than 1 ; Driver code"
Solution in C#:

using System ; class GFG { static int countDecreasing ( int [ ] A , int n ) { int len = 1 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { if ( A [ i + 1 ] < A [ i ] ) len ++ ; else { cnt += ( ( ( len - 1 ) * len ) / 2 ) ; len = 1 ; } } if ( len > 1 ) cnt += ( ( ( len - 1 ) * len ) / 2 ) ; return cnt ; } static void Main ( ) { int [ ] A = { 100 , 3 , 1 , 13 } ; int n = A . Length ; Console . WriteLine ( countDecreasing ( A , n ) ) ; } }


"Find the count of distinct numbers in a range | C # Program to find the distinct elements in a range ; Function to perform queries in a range ; No overlap ; Totally Overlap ; Partial Overlap ; Finding the Answer for the left Child ; Finding the Answer for the right Child ; Combining the BitMasks ; Function to perform update operation in the Segment seg ; Forming the BitMask ; Updating the left Child ; Updating the right Child ; Updating the BitMask ; Building the Segment Tree ; Building the Initial BitMask ; Building the left seg tree ; Building the right seg tree ; Forming the BitMask ; Utility Function to answer the queries ; Counting the set bits which denote the distinct elements ; Updating the value ; Driver Code"
Solution in C#:

using System ; class GFG { static long query ( int start , int end , int left , int right , int node , long [ ] seg ) { if ( end < left start > right ) { return 0 ; } else if ( start >= left && end <= right ) { return seg [ node ] ; } else { int mid = ( start + end ) / 2 ; long leftChild = query ( start , mid , left , right , 2 * node , seg ) ; long rightChild = query ( mid + 1 , end , left , right , 2 * node + 1 , seg ) ; return ( leftChild rightChild ) ; } } static void update ( int left , int right , int index , int Value , int node , int [ ] ar , long [ ] seg ) { if ( left == right ) { ar [ index ] = Value ; seg [ node ] = ( 1L << Value ) ; return ; } int mid = ( left + right ) / 2 ; if ( index > mid ) { update ( mid + 1 , right , index , Value , 2 * node + 1 , ar , seg ) ; } else { update ( left , mid , index , Value , 2 * node , ar , seg ) ; } seg [ node ] = ( seg [ 2 * node ] seg [ 2 * node + 1 ] ) ; } static void build ( int left , int right , int node , int [ ] ar , long [ ] seg ) { if ( left == right ) { seg [ node ] = ( 1L << ar [ left ] ) ; return ; } int mid = ( left + right ) / 2 ; build ( left , mid , 2 * node , ar , seg ) ; build ( mid + 1 , right , 2 * node + 1 , ar , seg ) ; seg [ node ] = ( seg [ 2 * node ] seg [ 2 * node + 1 ] ) ; } static void getDistinctCount ( int [ , ] queries , int [ ] ar , long [ ] seg , int n ) { for ( int i = 0 ; i < queries . GetLength ( 0 ) ; i ++ ) { int op = queries [ i , 0 ] ; if ( op == 2 ) { int l = queries [ i , 1 ] , r = queries [ i , 2 ] ; long tempMask = query ( 0 , n - 1 , l - 1 , r - 1 , 1 , seg ) ; int countOfBits = 0 ; for ( int s = 63 ; s >= 0 ; s -- ) { if ( ( tempMask & ( 1L << s ) ) > 0 ) { countOfBits ++ ; } } Console . WriteLine ( countOfBits ) ; } else { int index = queries [ i , 1 ] ; int val = queries [ i , 2 ] ; update ( 0 , n - 1 , index - 1 , val , 1 , ar , seg ) ; } } } public static void Main ( String [ ] args ) { int n = 7 ; int [ ] ar = { 1 , 2 , 1 , 3 , 1 , 2 , 1 } ; long [ ] seg = new long [ 4 * n ] ; build ( 0 , n - 1 , 1 , ar ,...


"Find the count of even odd pairs in a given Array | C # program to count the pairs in array of the form ( even , odd ) ; Function to count the pairs in array of the form ( even , odd ) ; Check if number is even or not ; Driver code"
Solution in C#:

using System ; class GFG { static int findCount ( int [ ] arr , int n ) { int count = 0 , ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { count ++ ; } else { ans = ans + count ; } } return ans ; } public static void Main ( ) { int [ ] a = { 5 , 4 , 1 , 2 , 3 } ; int n = a . Length ; Console . WriteLine ( findCount ( a , n ) ) ; } }


"Find the count of even odd pairs in a given Array | C # program to count the pairs in array of the form ( even , odd ) ; Function to count the pairs in array of the form ( even , odd ) ; Variable to store count of such pairs ; Iterate through all pairs ; Increment count if condition is satisfied ; Driver code"
Solution in C#:

using System ; class GFG { static int findCount ( int [ ] arr , int n ) { int res = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( ( arr [ i ] % 2 == 0 ) && ( arr [ j ] % 2 == 1 ) ) { res ++ ; } return res ; } public static void Main ( String [ ] args ) { int [ ] a = { 5 , 4 , 1 , 2 , 3 } ; int n = a . Length ; Console . Write ( findCount ( a , n ) ) ; } }


"Find the count of mountains in a given Matrix | C # program find the count of mountains in a given Matrix ; Function to count number of mountains in a given matrix of size n ; form another matrix with one extra layer of border elements . Border elements will contain Integer . MIN_VALUE value . ; For border elements , set value as Integer . MIN_VALUE ; For rest elements , just copy it into new matrix ; Check for mountains in the modified matrix ; check for all directions ; Driver code"
Solution in C#:


"Find the count of natural Hexadecimal numbers of size N | C # implementation of the approach ; Function to return the count of n - digit natural hexadecimal numbers ; Driver code"
Solution in C#:

using System ; class GFG { static int count ( int n ) { return ( int ) ( 15 * Math . Pow ( 16 , n - 1 ) ) ; } public static void Main ( String [ ] args ) { int n = 2 ; Console . WriteLine ( count ( n ) ) ; } }


"Find the count of numbers that can be formed using digits 3 , 4 only and having length at max N . | C # program to find the count of numbers that can be formed using digits 3 , 4 only and having length at max N . ; Function to find the count of numbers that can be formed using digits 3 , 4 only and having length at max N . ; Driver code"
Solution in C#:

using System ; class GFG { static long numbers ( int n ) { return ( long ) ( Math . Pow ( 2 , n + 1 ) ) - 2 ; } static void Main ( ) { int n = 2 ; Console . WriteLine ( numbers ( n ) ) ; } }


"Find the count of palindromic sub | C # program to find the count of palindromic sub - string of a string in it 's ascending form ; function to return count of palindromic sub - string ; calculate frequency ; calculate count of palindromic sub - string ; return result ; driver program"
Solution in C#:

using System ; public class GFG { readonly static int MAX_CHAR = 26 ; static int countPalindrome ( String str ) { int n = str . Length ; int sum = 0 ; int [ ] hashTable = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) { hashTable [ str [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( hashTable [ i ] != 0 ) { sum += ( hashTable [ i ] * ( hashTable [ i ] + 1 ) / 2 ) ; } } return sum ; } public static void Main ( ) { String str = " ananananddd " ; Console . Write ( countPalindrome ( str ) ) ; } }


"Find the count of sub | C # implementation of the approach ; Function to return the count of required occurrence ; To store the count of occurrences ; Check first four characters from ith position ; Variables for counting the required characters ; Check the four contiguous characters which can be reordered to form ' clap ' ; If all four contiguous characters are present then increment cnt variable ; Driver code"
Solution in C#:

using System ; class GFG { static int countOcc ( string s ) { int cnt = 0 ; for ( int i = 0 ; i < s . Length - 3 ; i ++ ) { int c = 0 , l = 0 , a = 0 , p = 0 ; for ( int j = i ; j < i + 4 ; j ++ ) { switch ( s [ j ] ) { case ' c ' : c ++ ; break ; case ' l ' : l ++ ; break ; case ' a ' : a ++ ; break ; case ' p ' : p ++ ; break ; } } if ( c == 1 && l == 1 && a == 1 && p == 1 ) cnt ++ ; } return cnt ; } public static void Main ( ) { string s = " clapc " ; Console . Write ( countOcc ( s . ToLower ( ) ) ) ; } }


"Find the count of subsequences where each element is divisible by K | C # implementation of the approach ; Function to return the count of all valid subsequences ; To store the count of elements which are divisible by k ; If current element is divisible by k then increment the count ; Total ( 2 ^ n - 1 ) non - empty subsequences are possible with n element ; Driver code"
Solution in C#:

using System ; class GFG { static int countSubSeq ( int [ ] arr , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { count ++ ; } } return ( int ) ( Math . Pow ( 2 , count ) - 1 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 6 } ; int n = arr . Length ; int k = 3 ; Console . WriteLine ( countSubSeq ( arr , n , k ) ) ; } }


"Find the count of substrings in alphabetic order | C # to find the number of substrings in alphabetical order ; Function to find number of substrings ; Iterate over string length ; if any two chars are in alphabetic order ; find next char not in order ; return the result ; Driver function"
Solution in C#:

using System ; public class Solution { public static int findSubstringCount ( string str ) { int result = 0 ; int n = str . Length ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( ( char ) ( str [ i ] + 1 ) == str [ i + 1 ] ) { result ++ ; while ( ( char ) ( str [ i ] + 1 ) == str [ i + 1 ] ) { i ++ ; } } } return result ; } public static void Main ( string [ ] args ) { string str = " alphabet " ; Console . WriteLine ( findSubstringCount ( str ) ) ; } }


"Find the count of unvisited indices in an infinite array | C # implementation of the above approach ; Function to return the count of unvisited indices starting from the index 0 ; Largest index that cannot be visited ; Push the index to the queue ; To store the required count ; Current index that cannot be visited ; Increment the count for the current index ; ( curr - m ) and ( curr - n ) are also unreachable if they are valid indices ; Return the required count ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int countUnvisited ( int n , int m ) { int X = ( m * n ) - m - n ; Queue < int > queue = new Queue < int > ( ) ; queue . Enqueue ( X ) ; int count = 0 ; while ( queue . Count != 0 ) { int curr = queue . Dequeue ( ) ; count ++ ; if ( curr - m > 0 ) queue . Enqueue ( curr - m ) ; if ( curr - n > 0 ) queue . Enqueue ( curr - n ) ; } return count ; } public static void Main ( String [ ] args ) { int n = 2 , m = 5 ; Console . WriteLine ( countUnvisited ( n , m ) ) ; } }


"Find the date after next half year from a given date | C # program for the above approach ; Function to find the date after the next half - year ; Stores the number of days in the months of a leap year ; List of months ; Days in half of a year ; Index of current month ; Starting day ; Decrement the value of cnt by 1 ; Increment cur_date ; If cnt is equal to 0 , then break out of the loop ; Update cur_month ; Update cur_date ; Print the resultant date ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static void getDate ( int d , string m ) { int [ ] days = { 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ; string [ ] month = { " January " , " February " , " March " , " April " , " May " , " June " , " July " , " August " , " September " , " October " , " November " , " December " } ; int cnt = 183 ; int cur_month = 0 ; for ( int i = 0 ; i < 12 ; i ++ ) if ( m == month [ i ] ) cur_month = i ; int cur_date = d ; while ( true ) { while ( cnt > 0 && cur_date <= days [ cur_month ] ) { cnt -= 1 ; cur_date += 1 ; } if ( cnt == 0 ) break ; cur_month = ( cur_month + 1 ) % 12 ; cur_date = 1 ; } Console . WriteLine ( cur_date + " ▁ " + month [ cur_month ] ) ; } public static void Main ( ) { int D = 15 ; string M = " January " ; getDate ( D , M ) ; } }


"Find the deleted value from the array when average of original elements is given | C # implementation of above approach ; Function to return the missing element ; Find the sum of the array elements ; The numerator and the denominator of the equation ; If not divisible then X is not an integer it is a floating point number ; Return X ; Driver code"
Solution in C#:

using System ; class GFG { static int findMissing ( int [ ] arr , int n , int k , int avg ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } int num = ( avg * ( n + k ) ) - sum ; int den = k ; if ( num % den != 0 ) return - 1 ; return ( int ) ( num / den ) ; } public static void Main ( String [ ] args ) { int k = 3 , avg = 4 ; int [ ] arr = { 2 , 7 , 3 } ; int n = arr . Length ; Console . WriteLine ( findMissing ( arr , n , k , avg ) ) ; } }


"Find the diagonal of the Cube | C # program to find length of the diagonal of the cube ; Function to find length of diagonal of cube ; Formula to Find length of diagonal of cube ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static float diagonal_length ( float a ) { float L ; L = a * ( float ) Math . Sqrt ( 3 ) ; return L ; } public static void Main ( ) { float a = 5 ; Console . Write ( diagonal_length ( a ) ) ; } }


"Find the distance covered to collect items at equal distances | C # program to calculate the distance for given problem ; function to calculate the distance ; Driver program"
Solution in C#:

using System ; class GFG { public static int find_distance ( int n ) { return n * ( ( 3 * n ) + 7 ) ; } public static void Main ( ) { int n = 5 ; Console . Write ( find_distance ( n ) ) ; } }


"Find the duplicate characters in a string in O ( 1 ) space | C # program for the above approach ; Function to find duplicate characters in string without using any additional data structure ; Check if ( i + ' a ' ) is present in str at least once or not . ; Check if ( i + ' a ' ) is present in str at least twice or not . ; Iterate over the characters of the string str ; If str [ i ] has already occurred in str ; Set ( str [ i ] - ' a ' ) - th bit of second ; Set ( str [ i ] - ' a ' ) - th bit of second ; Iterate over the range [ 0 , 25 ] ; If i - th bit of both first and second is Set ; Driver Code"
Solution in C#:

using System ; class GFG { static void findDuplicate ( string str , int N ) { int first = 0 ; int second = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( first & ( 1 << ( str [ i ] - ' a ' ) ) ) != 0 ) { second = second | ( 1 << ( str [ i ] - ' a ' ) ) ; } else { first = first | ( 1 << ( str [ i ] - ' a ' ) ) ; } } for ( int i = 0 ; i < 26 ; i ++ ) { if ( ( ( first & ( 1 << i ) ) & ( second & ( 1 << i ) ) ) != 0 ) { Console . Write ( ( char ) ( i + ' a ' ) + " ▁ " ) ; } } } static public void Main ( ) { string str = " geeksforgeeks " ; int N = str . Length ; findDuplicate ( str , N ) ; } }


"Find the element at R ' th ▁ row ▁ and ▁ C ' th column in given a 2D pattern | C # implementation to compute the R ' th ▁ row ▁ and ▁ C ' th column of the given pattern ; Function to compute the R ' th ▁ row ▁ and ▁ C ' th column of the given pattern ; First element of a given row ; Element in the given column ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int findValue ( int R , int C ) { int k = ( R * ( R - 1 ) ) / 2 + 1 ; int diff = R + 1 ; for ( int i = 1 ; i < C ; i ++ ) { k = ( k + diff ) ; diff ++ ; } return k ; } public static void Main ( ) { int R = 4 ; int C = 4 ; int k = findValue ( R , C ) ; Console . Write ( k ) ; } }


"Find the element at specified index in a Spiral Matrix | C # program for the above approach ; Function to the find element at ( i , j ) index ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int findInGrid ( int i , int j ) { if ( i == j ) return ( i * i - ( i - 1 ) ) ; else if ( i > j ) { if ( i % 2 == 0 ) return i * i - ( j - 1 ) ; else return ( i - 1 ) * ( i - 1 ) + 1 + ( j - 1 ) ; } else { if ( j % 2 == 0 ) return ( j - 1 ) * ( j - 1 ) + 1 + ( i - 1 ) ; else return j * j - ( i - 1 ) ; } } static public void Main ( ) { int i = 3 , j = 4 ; Console . WriteLine ( findInGrid ( i , j ) ) ; } }


"Find the element at the specified index of a Spirally Filled Matrix | C # program to find the element at given position in spirally filled matrix ; Function to return the element at ( x , y ) ; If y is greater ; If y is odd ; If y is even ; If x is even ; If x is odd ; Driver code"
Solution in C#:

using System ; class GFG { static int SpiralElement ( int x , int y ) { int r ; if ( x < y ) { if ( y % 2 == 1 ) { r = y * y ; return ( r - x + 1 ) ; } else { r = ( y - 1 ) * ( y - 1 ) ; return ( r + x ) ; } } else { if ( x % 2 == 0 ) { r = x * x ; return ( r - y + 1 ) ; } else { r = ( x - 1 ) * ( x - 1 ) ; return ( r + y ) ; } } } static public void Main ( ) { int x = 2 , y = 3 ; Console . WriteLine ( SpiralElement ( x , y ) ) ; } }


"Find the element having maximum set bits in the given range for Q queries | C # implementation to find maximum set bits value in a range ; Structure to store two values in one node ; Function that returns the count of set bits in a number ; Parity will store the count of set bits ; Function to build the segment tree ; Condition to check if there is only one element in the array ; If there are more than one elements , then recur for left and right subtrees ; Condition to check the maximum set bits is greater in two subtrees ; Condition when maximum set bits are equal in both subtrees ; Function to do the range query in the segment tree ; If segment of this node is outside the given range , then return the minimum value . ; If segment of this node is a part of given range , then return the node of the segment ; If left segment of this node falls out of range , then recur in the right side of the tree ; If right segment of this node falls out of range , then recur in the left side of the tree ; If a part of this segment overlaps with the given range ; Returns the value ; Driver code ; Calculates the length of array ; Build Segment Tree ; Find the max set bits value between 1 st and 4 th index of array ; Find the max set bits value between 0 th and 2 nd index of array"
Solution in C#:

using System ; class GFG { class Node { public int value ; public int max_set_bits ; } ; static Node [ ] tree = new Node [ 4 * 10000 ] ; static int setBits ( int x ) { int parity = 0 ; while ( x != 0 ) { if ( x % 2 == 1 ) parity ++ ; x = x >> 1 ; } return parity ; } static void buildSegmentTree ( int [ ] a , int index , int beg , int end ) { if ( beg == end ) { tree [ index ] . value = a [ beg ] ; tree [ index ] . max_set_bits = setBits ( a [ beg ] ) ; } else { int mid = ( beg + end ) / 2 ; buildSegmentTree ( a , 2 * index + 1 , beg , mid ) ; buildSegmentTree ( a , 2 * index + 2 , mid + 1 , end ) ; if ( tree [ 2 * index + 1 ] . max_set_bits > tree [ 2 * index + 2 ] . max_set_bits ) { tree [ index ] . max_set_bits = tree [ 2 * index + 1 ] . max_set_bits ; tree [ index ] . value = tree [ 2 * index + 1 ] . value ; } else if ( tree [ 2 * index + 2 ] . max_set_bits > tree [ 2 * index + 1 ] . max_set_bits ) { tree [ index ] . max_set_bits = tree [ 2 * index + 2 ] . max_set_bits ; tree [ index ] . value = tree [ 2 * index + 2 ] . value ; } else { tree [ index ] . max_set_bits = tree [ 2 * index + 2 ] . max_set_bits ; tree [ index ] . value = Math . Max ( tree [ 2 * index + 2 ] . value , tree [ 2 * index + 1 ] . value ) ; } } } static Node query ( int index , int beg , int end , int l , int r ) { Node result = new Node ( ) ; result . value = result . max_set_bits = - 1 ; if ( beg > r end < l ) return result ; if ( beg >= l && end <= r ) return tree [ index ] ; int mid = ( beg + end ) / 2 ; if ( l > mid ) return query ( 2 * index + 2 , mid + 1 , end , l , r ) ; if ( r <= mid ) return query ( 2 * index + 1 , beg , mid , l , r ) ; Node left = query ( 2 * index + 1 , beg , mid , l , r ) ; Node right = query ( 2 * index + 2 , mid + 1 , end , l , r ) ; if ( left . max_set_bits > right . max_set_bits ) { result . max_set_bits = left . max_set_bits ; result . value = left . value ; } else if ( right . max_set_bits > left . max_set_bits ) { result . max_set_bits = right . max_set_bits ; result . value = right . value ; } else...


"Find the element in a linked list with frequency at least N / 3 | C # program to find an element with frequency of at least N / 3 in a linked list ; Structure of a node for the linked list ; Utility function to create a node ; Function to find and return the element with frequency of at least N / 3 ; Candidates for being the required majority element ; Store the frequencies of the respective candidates ; Iterate all nodes ; Increase frequency of candidate s ; Increase frequency of candidate t ; Set the new sting as candidate for majority ; Set the new sting as second candidate for majority ; Decrease the frequency ; Check the frequency of two final selected candidate linklist ; Increase the frequency of first candidate ; Increase the frequency of second candidate ; Return the string with higher frequency ; Driver Code"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { class node { public string i ; public node next = null ; } ; static node newnode ( string s ) { node temp = new node ( ) ; temp . i = s ; temp . next = null ; return temp ; } static string Majority_in_linklist ( node head ) { string s = " " ; string t = " " ; int p = 0 , q = 0 ; node ptr = null ; while ( head != null ) { if ( s . Equals ( head . i ) ) { p = p + 1 ; } else { if ( t . Equals ( head . i ) ) { q = q + 1 ; } else { if ( p == 0 ) { s = head . i ; p = 1 ; } else { if ( q == 0 ) { t = head . i ; q = 1 ; } else { p = p - 1 ; q = q - 1 ; } } } } head = head . next ; } head = ptr ; p = 0 ; q = 0 ; while ( head != null ) { if ( s . Equals ( head . i ) ) { p = 1 ; } else { if ( t . Equals ( head . i ) ) { q = 1 ; } } head = head . next ; } if ( p > q ) { return s ; } else { return t ; } } public static void Main ( string [ ] arg ) { node head = newnode ( " geeks " ) ; head . next = newnode ( " geeks " ) ; head . next . next = newnode ( " abcd " ) ; head . next . next . next = newnode ( " game " ) ; head . next . next . next . next = newnode ( " game " ) ; head . next . next . next . next . next = newnode ( " knight " ) ; head . next . next . next . next . next . next = newnode ( " harry " ) ; head . next . next . next . next . next . next . next = newnode ( " geeks " ) ; Console . Write ( Majority_in_linklist ( head ) ) ; } }


"Find the element that appears once in an array where every other element appears twice |  ; singleelement function ; Driver code"
Solution in C#:

using System ; using System . Collections ; class GFG { static int singleelement ( int [ ] arr , int n ) { int low = 0 , high = n - 2 ; int mid ; while ( low <= high ) { mid = ( low + high ) / 2 ; if ( arr [ mid ] == arr [ mid ^ 1 ] ) { low = mid + 1 ; } else { high = mid - 1 ; } } return arr [ low ] ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 5 , 4 , 5 , 3 , 4 } ; int size = 7 ; Array . Sort ( arr ) ; Console . WriteLine ( singleelement ( arr , size ) ) ; } }


"Find the element that appears once in an array where every other element appears twice | C # program to find element that appears once ; function which find number ; applying the formula . ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int singleNumber ( int [ ] nums , int n ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; long sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! m . ContainsKey ( nums [ i ] ) ) { sum1 += nums [ i ] ; m . Add ( nums [ i ] , 1 ) ; } sum2 += nums [ i ] ; } return ( int ) ( 2 * ( sum1 ) - sum2 ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 3 , 5 , 4 , 5 , 3 , 4 } ; int n = 7 ; Console . WriteLine ( singleNumber ( a , n ) ) ; int [ ] b = { 15 , 18 , 16 , 18 , 16 , 15 , 89 } ; Console . WriteLine ( singleNumber ( b , n ) ) ; } }


"Find the element that appears once in an array where every other element appears twice | C # program to find the array element that appears only once ; Return the maximum Sum of difference between consecutive elements . ; Do XOR of all elements and return ; Driver code"
Solution in C#:

using System ; class GFG { static int findSingle ( int [ ] ar , int ar_size ) { int res = ar [ 0 ] ; for ( int i = 1 ; i < ar_size ; i ++ ) res = res ^ ar [ i ] ; return res ; } public static void Main ( ) { int [ ] ar = { 2 , 3 , 5 , 4 , 5 , 3 , 4 } ; int n = ar . Length ; Console . Write ( " Element ▁ occurring ▁ once ▁ is ▁ " + findSingle ( ar , n ) + " ▁ " ) ; } }


"Find the element that appears once | C # code to find the element that occur only once ; Initialize result ; Iterate through every bit ; Find sum of set bits at ith position in all array elements ; The bits with sum not multiple of 3 , are the bits of element with single occurrence . ; Driver Code"
Solution in C#:

using System ; class GFG { static int INT_SIZE = 32 ; static int getSingle ( int [ ] arr , int n ) { int result = 0 ; int x , sum ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) { sum = 0 ; x = ( 1 << i ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & x ) == 0 ) sum ++ ; } if ( ( sum % 3 ) != 0 ) result |= x ; } return result ; } public static void Main ( ) { int [ ] arr = { 12 , 1 , 12 , 3 , 12 , 1 , 1 , 2 , 3 , 2 , 2 , 3 , 7 } ; int n = arr . Length ; Console . WriteLine ( " The ▁ element ▁ with ▁ single ▁ " + " occurrence ▁ is ▁ " + getSingle ( arr , n ) ) ; } }


"Find the element that appears once | C # code to find the element that occur only once ; Method to find the element that occur only once ; " one ▁ & ▁ arr [ i ] " gives the bits that are there in both ' ones ' and new element from arr [ ] . We add these bits to ' twos ' using bitwise OR ; " one ▁ & ▁ arr [ i ] " gives the bits that are there in both ' ones ' and new element from arr [ ] . We add these bits to ' twos ' using bitwise OR ; The common bits are those bits which appear third time So these bits should not be there in both ' ones ' and ' twos ' . common_bit_mask contains all these bits as 0 , so that the bits can be removed from ' ones ' and ' twos ' ; Remove common bits ( the bits that appear third time ) from ' ones ' ; Remove common bits ( the bits that appear third time ) from ' twos ' ; Driver code"
Solution in C#:

using System ; class GFG { static int getSingle ( int [ ] arr , int n ) { int ones = 0 , twos = 0 ; int common_bit_mask ; for ( int i = 0 ; i < n ; i ++ ) { twos = twos | ( ones & arr [ i ] ) ; ones = ones ^ arr [ i ] ; common_bit_mask = ~ ( ones & twos ) ; ones &= common_bit_mask ; twos &= common_bit_mask ; } return ones ; } public static void Main ( ) { int [ ] arr = { 3 , 3 , 2 , 3 } ; int n = arr . Length ; Console . WriteLine ( " The ▁ element ▁ with ▁ single " + " occurrence ▁ is ▁ " + getSingle ( arr , n ) ) ; } }


"Find the element whose multiplication with | C # program to find minimum index such that sum becomes 0 when the element is multiplied by - 1 ; Function to find minimum index such that sum becomes 0 when the element is multiplied by - 1 ; Find array sum ; Find element with value equal to sum / 2 ; when sum is equal to 2 * element then this is our required element ; Driver code"
Solution in C#:

using System ; class GFG { static int minIndex ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( 2 * arr [ i ] == sum ) return ( i + 1 ) ; } return - 1 ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , - 5 , 3 , 4 } ; int n = arr . Length ; Console . Write ( minIndex ( arr , n ) ) ; } }


"Find the equal pairs of subsequence of S and subsequence of T | C # implementation of the approach ; Function to return the pairs of subsequences from S [ ] and subsequences from T [ ] such that both have the same content ; Create dp array ; Base values ; Base values ; Keep previous dp value ; If both elements are same ; Return the required answer ; Driver code"
Solution in C#:

using System ; class GFG { static int subsequence ( int [ ] S , int [ ] T , int n , int m ) { int [ , ] dp = new int [ n + 1 , m + 1 ] ; int mod = 1000000007 ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i , 0 ] = 1 ; for ( int j = 0 ; j <= m ; j ++ ) dp [ 0 , j ] = 1 ; for ( int i = 1 ; i <= n ; ++ i ) { for ( int j = 1 ; j <= m ; ++ j ) { dp [ i , j ] = dp [ i - 1 , j ] + dp [ i , j - 1 ] - dp [ i - 1 , j - 1 ] ; if ( S [ i - 1 ] == T [ j - 1 ] ) dp [ i , j ] += dp [ i - 1 , j - 1 ] ; dp [ i , j ] += mod ; dp [ i , j ] %= mod ; } } return dp [ n , m ] ; } public static void Main ( ) { int [ ] S = { 1 , 1 } ; int n = S . Length ; int [ ] T = { 1 , 1 } ; int m = T . Length ; Console . WriteLine ( subsequence ( S , T , n , m ) ) ; } }


"Find the equation of plane which passes through two points and parallel to a given axis | C # implementation to find the equation of plane which passes through two points and parallel to a given axis ; Find direction vector of points ( x1 , y1 , z1 ) and ( x2 , y2 , z2 ) ; Values that are calculated and simplified from the cross product ; Print the equation of plane ; Driver code ; Point A ; Point B ; Given axis ; Function Call"
Solution in C#:

using System ; class GFG { static void findEquation ( int x1 , int y1 , int z1 , int x2 , int y2 , int z2 , int d , int e , int f ) { double a = x2 - x1 ; double b = y2 - y1 ; double c = z2 - z1 ; int A = ( int ) ( b * f - c * e ) ; int B = ( int ) ( a * f - c * d ) ; int C = ( int ) ( a * e - b * d ) ; int D = - ( int ) ( A * d - B * e + C * f ) ; Console . Write ( A + " x ▁ + ▁ " + B + " y ▁ + ▁ " + C + " z ▁ + ▁ " + D + " = ▁ 0 ▁ " ) ; } public static void Main ( ) { int x1 = 2 , y1 = 3 , z1 = 5 ; int x2 = 6 , y2 = 7 , z2 = 8 ; int a = 11 , b = 23 , c = 10 ; findEquation ( x1 , y1 , z1 , x2 , y2 , z2 , a , b , c ) ; } }


"Find the farthest smaller number in the right side | C # implementation of the approach ; Function to find the farthest smaller number in the right side ; To store minimum element in the range i to n ; If current element in the suffix_min is less than a [ i ] then move right ; Print the required answer ; Driver code"
Solution in C#:

using System ; class GFG { static void farthest_min ( int [ ] a , int n ) { int [ ] suffix_min = new int [ n ] ; suffix_min [ n - 1 ] = a [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { suffix_min [ i ] = Math . Min ( suffix_min [ i + 1 ] , a [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { int low = i + 1 , high = n - 1 , ans = - 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( suffix_min [ mid ] < a [ i ] ) { ans = mid ; low = mid + 1 ; } else high = mid - 1 ; } Console . Write ( ans + " ▁ " ) ; } } public static void Main ( ) { int [ ] a = { 3 , 1 , 5 , 2 , 4 } ; int n = a . Length ; farthest_min ( a , n ) ; } }


"Find the final X and Y when they are Altering under given condition | C # implementation of the approach ; Function to get final value of X and Y ; Following the sequence but by replacing minus with modulo ; Step 1 ; Step 2 ; Step 3 ; Otherwise terminate ; Driver Code ; Get the initial X and Y values ; Find the result"
Solution in C#:

using System ; class GFG { static void alter ( long x , long y ) { while ( true ) { if ( x == 0 y == 0 ) break ; if ( x >= 2 * y ) x = x % ( 2 * y ) ; else if ( y >= 2 * x ) y = y % ( 2 * x ) ; else break ; } Console . WriteLine ( " X ▁ = ▁ " + x + " , ▁ " + " Y ▁ = ▁ " + y ) ; } public static void Main ( ) { long x = 12 , y = 5 ; alter ( x , y ) ; } }


"Find the final co | C # implementation of the above approach ; Function to print the readonly position of the point after traversing through the given directions ; Traversing through the given directions ; If its north or south the point will move left or right ; If its west or east the point will move upwards or downwards ; Returning the readonly position ; Driver Code"
Solution in C#:

using System ; class GFG { static void finalCoordinates ( int SX , int SY , char [ ] D ) { for ( int i = 0 ; i < D . Length ; i ++ ) { if ( D [ i ] == ' N ' ) { SY += 1 ; } else if ( D [ i ] == ' S ' ) { SY -= 1 ; } else if ( D [ i ] == ' E ' ) { SX += 1 ; } else { SX -= 1 ; } } String ans = ' ( ' + String . Join ( " " , SX ) + ' , ' + String . Join ( " " , SY ) + ' ) ' ; Console . Write ( ans ) ; } public static void Main ( String [ ] args ) { int SX = 2 , SY = 2 ; String D = " NSSE " ; finalCoordinates ( SX , SY , D . ToCharArray ( ) ) ; } }


"Find the final number obtained after performing the given operation | C # implementation of the approach ; Function to return the readonly number obtained after performing the given operation ; Find the gcd of the array elements ; Driver code"
Solution in C#:

using System ; class GFG { static int finalNum ( int [ ] arr , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { result = __gcd ( result , arr [ i ] ) ; } return result ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 9 , 6 , 36 } ; int n = arr . Length ; Console . Write ( finalNum ( arr , n ) ) ; } }


"Find the final radiations of each Radiated Stations | C # implementation of the approach ; Function to print the final radiations ; Function to create the array of the resultant radiations ; Resultant radiations ; Declaring index counter for left and right radiation ; Effective radiation for left and right case ; Radiation for i - th station ; Radiation increment for left stations ; Radiation increment for right stations ; Print the resultant radiation for each of the stations ; Driver code ; 1 - based indexing"
Solution in C#:

using System ; class GFG { static void print ( int [ ] rStation , int n ) { for ( int i = 1 ; i <= n ; i ++ ) Console . Write ( rStation [ i ] + " ▁ " ) ; Console . WriteLine ( " " ) ; } static void radiated_Station ( int [ ] station , int n ) { int [ ] rStation = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { int li = i - 1 , ri = i + 1 ; int lRad = station [ i ] - 1 , rRad = station [ i ] - 1 ; rStation [ i ] += station [ i ] ; while ( li >= 1 && lRad >= 1 ) { rStation [ li -- ] += lRad -- ; } while ( ri <= n && rRad >= 1 ) { rStation [ ri ++ ] += rRad -- ; } } print ( rStation , n ) ; } public static void Main ( String [ ] args ) { int [ ] station = { 0 , 7 , 9 , 12 , 2 , 5 } ; int n = station . Length - 1 ; radiated_Station ( station , n ) ; } }


"Find the final sequence of the array after performing given operations | C # implementation of the approach ; Driver Code"
Solution in C#:

using System ; class GFG { static int [ ] solve ( int [ ] arr , int n ) { int [ ] b = new int [ n ] ; int p = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { b [ p ] = arr [ i -- ] ; if ( i >= 0 ) b [ n - 1 - p ] = arr [ i ] ; p ++ ; } return b ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; int [ ] b = solve ( arr , n ) ; Console . WriteLine ( " [ " + String . Join ( " , " , b ) + " ] " ) ; } }


"Find the final sequence of the array after performing given operations | C # implementation of the approach ; Function that generates the array b [ ] when n is even ; Fill the first half of the final array with reversed sequence ; Fill the second half ; Function that generates the array b [ ] when n is odd ; Fill the first half of the final array with reversed sequence ; Fill the second half ; Function to find the final array b [ ] after n operations of given type ; Create the array b ; If the array size is even ; Print the final array elements ; Driver code"
Solution in C#:

using System ; class GFG { static void solveEven ( int n , int [ ] arr , int [ ] b ) { int left = n - 1 ; for ( int i = 0 ; i < ( n / 2 ) ; ++ i ) { b [ i ] = arr [ left ] ; left = left - 2 ; if ( left < 0 ) break ; } int right = 0 ; for ( int i = n / 2 ; i <= n - 1 ; ++ i ) { b [ i ] = arr [ right ] ; right = right + 2 ; if ( right > n - 2 ) break ; } } static void solveOdd ( int n , int [ ] arr , int [ ] b ) { int left = n - 1 ; for ( int i = 0 ; i < ( n / 2 ) + 1 ; ++ i ) { b [ i ] = arr [ left ] ; left = left - 2 ; if ( left < 0 ) break ; } int right = 1 ; for ( int i = ( n / 2 ) + 1 ; i <= n - 1 ; ++ i ) { b [ i ] = arr [ right ] ; right = right + 2 ; if ( right > n - 2 ) break ; } } static void solve ( int n , int [ ] arr ) { int [ ] b = new int [ n ] ; if ( n % 2 == 0 ) solveEven ( n , arr , b ) ; else solveOdd ( n , arr , b ) ; for ( int i = 0 ; i <= n - 1 ; ++ i ) { Console . Write ( b [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; solve ( n , arr ) ; } }


"Find the first , second and third minimum elements in an array | C # program to find the first , second and third minimum element in an array ; Check if current element is less than firstmin , then update first , second and third ; Check if current element is less than secmin then update second and third ; Check if current element is less than then update third ; Driver code"
Solution in C#:

using System ; class GFG { static void Print3Smallest ( int [ ] array , int n ) { int firstmin = int . MaxValue ; int secmin = int . MaxValue ; int thirdmin = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( array [ i ] < firstmin ) { thirdmin = secmin ; secmin = firstmin ; firstmin = array [ i ] ; } else if ( array [ i ] < secmin ) { thirdmin = secmin ; secmin = array [ i ] ; } else if ( array [ i ] < thirdmin ) thirdmin = array [ i ] ; } Console . WriteLine ( " First ▁ min ▁ = ▁ " + firstmin ) ; Console . WriteLine ( " Second ▁ min ▁ = ▁ " + secmin ) ; Console . WriteLine ( " Third ▁ min ▁ = ▁ " + thirdmin ) ; } static void Main ( ) { int [ ] array = new int [ ] { 4 , 9 , 1 , 32 , 12 } ; int n = array . Length ; Print3Smallest ( array , n ) ; } }


"Find the first N integers such that the sum of their digits is equal to 10 | C # implementation of the approach ; Function to return the sum of digits of n ; Add the last digit to the sum ; Remove last digit ; Return the sum of digits ; Function to print the first n numbers whose sum of digits is 10 ; First number of the series is 19 ; If the sum of digits of the current number is equal to 10 ; Print the number ; Add 9 to the previous number ; Driver code"
Solution in C#:

using System ; class GFG { static int sum ( int n ) { int sum = 0 ; while ( n > 0 ) { sum = sum + n % 10 ; n = n / 10 ; } return sum ; } static void firstN ( int n ) { int num = 19 , cnt = 1 ; while ( cnt != n ) { if ( sum ( num ) == 10 ) { Console . Write ( num + " ▁ " ) ; cnt ++ ; } num += 9 ; } } public static void Main ( String [ ] args ) { int n = 10 ; firstN ( n ) ; } }


"Find the first and last M digits from K | C # program to implement the above approach ; Function to find a ^ b modulo M ; Function to find the first and last M digits from N ^ K ; Calculate Last M digits ; Calculate First M digits ; Extract the number after decimal ; Find 10 ^ y ; Move the Decimal Point M - 1 digits forward ; Print the result ; Driver Code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static long modPower ( long a , long b , long M ) { long res = 1 ; while ( b > 0 ) { if ( b % 2 == 1 ) res = res * a % M ; a = a * a % M ; b >>= 1 ; } return res ; } static void findFirstAndLastM ( long N , long K , long M ) { long lastM = modPower ( N , K , ( 1L ) * ( long ) Math . Pow ( 10 , M ) ) ; long firstM ; double y = ( double ) K * Math . Log10 ( N * 1.0 ) ; y = y - ( long ) y ; double temp = Math . Pow ( 10.0 , y ) ; firstM = ( long ) ( temp * ( 1L ) * Math . Pow ( 10 , ( M - 1 ) ) ) ; Console . Write ( firstM + " ▁ " + lastM + "
 " ) ; } public static void Main ( String [ ] args ) { long N = 12 , K = 12 , M = 4 ; findFirstAndLastM ( N , K , M ) ; } }


"Find the first day of a given year from a base year having first day as Monday | C # program to implement the above approach ; Function to find the day of 1 st January of Y year ; Count years between years Y and B ; Count leap years ; Non leap years ; Total number of days in the years lying between the years Y and B ; Actual day ; Driver code"
Solution in C#:

using System ; class GFG { static void findDay ( int Y , int B ) { int lyear , rest , totaldays , day ; Y = ( Y - 1 ) - B ; lyear = Y / 4 ; rest = Y - lyear ; totaldays = ( rest * 365 ) + ( lyear * 366 ) + 1 ; day = ( totaldays % 7 ) ; if ( day == 0 ) Console . WriteLine ( " Monday " ) ; else if ( day == 1 ) Console . WriteLine ( " Tuesday " ) ; else if ( day == 2 ) Console . WriteLine ( " Wednesday " ) ; else if ( day == 3 ) Console . WriteLine ( " Thursday " ) ; else if ( day == 4 ) Console . WriteLine ( " Friday " ) ; else if ( day == 5 ) Console . WriteLine ( " Saturday " ) ; else if ( day == 6 ) Console . WriteLine ( " Sunday " ) ; else Console . WriteLine ( " INPUT ▁ YEAR ▁ IS ▁ WRONG ! " ) ; } static void Main ( ) { int Y = 2020 , B = 1900 ; findDay ( Y , B ) ; } }


"Find the first maximum length even word from a string | C # program to find maximum length even word ; Function to find maximum length even word ; To store length of current word . ; To store length of maximum length word . ; To store starting index of maximum length word . ; If current character is space then word has ended . Check if it is even length word or not . If yes then compare length with maximum length found so far . ; Set currlen to zero for next word . ; Update length of current word . ; Check length of last word . ; If no even length word is present then return - 1. ; Driver code"
Solution in C#:

using System ; class GFG { static String findMaxLenEven ( string str ) { int n = str . Length ; int i = 0 ; int currlen = 0 ; int maxlen = 0 ; int st = - 1 ; while ( i < n ) { if ( str [ i ] == ' ▁ ' ) { if ( currlen % 2 == 0 ) { if ( maxlen < currlen ) { maxlen = currlen ; st = i - currlen ; } } currlen = 0 ; } else { currlen ++ ; } i ++ ; } if ( currlen % 2 == 0 ) { if ( maxlen < currlen ) { maxlen = currlen ; st = i - currlen ; } } if ( st == - 1 ) return " - 1" ; return str . Substring ( st , maxlen ) ; } public static void Main ( ) { string str = " this ▁ is ▁ a ▁ test ▁ string " ; Console . WriteLine ( findMaxLenEven ( str ) ) ; } }


"Find the first natural number whose factorial is divisible by x | A simple C # program to find first natural number whose factorial divides x ; Returns first number whose factorial divides x . ; int i = 1 ; Result ; Driver code"
Solution in C#:

using System ; class GFG { static int firstFactorialDivisibleNumber ( int x ) { int fact = 1 ; for ( i = 1 ; i < x ; i ++ ) { fact = fact * i ; if ( fact % x == 0 ) break ; } return i ; } public static void Main ( ) { int x = 16 ; Console . Write ( firstFactorialDivisibleNumber ( x ) ) ; } }


"Find the first natural number whose factorial is divisible by x | C # program for finding the Special Factorial Number ; function for calculating factorial ; function for check Special_Factorial_Number ; call fact function and the Modulo with k and check if condition is TRUE then return i ; driver function ; taking input"
Solution in C#:

using System ; public class GFG { static int fact ( int n ) { int num = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { num = num * i ; } return num ; } static int Special_Factorial_Number ( int k ) { for ( int i = 1 ; i <= k ; i ++ ) { if ( fact ( i ) % k == 0 ) { return i ; } } return 0 ; } public static void Main ( ) { int k = 16 ; Console . WriteLine ( Special_Factorial_Number ( k ) ) ; } }


"Find the first natural number whose factorial is divisible by x | Efficient C # program to find first natural number whose factorial divides x . ; GCD function to compute the greatest divisor among a and b ; Returns first number whose factorial divides x . ; int i = 1 ; Result ; Remove common factors ; We found first i . ; Driver code"
Solution in C#:

using System ; class GFG { static int gcd ( int a , int b ) { if ( ( a % b ) == 0 ) return b ; return gcd ( b , a % b ) ; } static int firstFactorialDivisibleNumber ( int x ) { int new_x = x ; for ( i = 1 ; i < x ; i ++ ) { new_x /= gcd ( i , new_x ) ; if ( new_x == 1 ) break ; } return i ; } public static void Main ( ) { int x = 16 ; Console . Write ( firstFactorialDivisibleNumber ( x ) ) ; } }


"Find the foot of perpendicular of a point in a 3 D plane | C # program to find foot of perpendicular of a point in a 3 D plane . ; Function to find foot of perpendicular ; Driver Code ; function call"
Solution in C#:

using System ; using System . Globalization ; class GFG { static void foot ( float a , float b , float c , float d , float x1 , float y1 , float z1 ) { float k = ( - a * x1 - b * y1 - c * z1 - d ) / ( float ) ( a * a + b * b + c * c ) ; float x2 = a * k + x1 ; float y2 = b * k + y1 ; float z2 = c * k + z1 ; NumberFormatInfo form = new NumberFormatInfo ( ) ; form . NumberDecimalSeparator = " . " ; Console . Write ( " ▁ x2 ▁ = ▁ " + x2 . ToString ( form ) ) ; Console . Write ( " ▁ y2 ▁ = ▁ " + y2 . ToString ( form ) ) ; Console . Write ( " ▁ z2 ▁ = ▁ " + z2 . ToString ( form ) ) ; } public static void Main ( String [ ] arr ) { float a = 1 ; float b = - 2 ; float c = 0 ; float d = 0 ; float x1 = - 1 ; float y1 = 3 ; float z1 = 4 ; foot ( a , b , c , d , x1 , y1 , z1 ) ; } }


"Find the fractional ( or n / k | C # program to find fractional node in a linked list ; Linked list node ; Constructor ; Function to find fractional node in the linked list ; Corner cases ; Traverse the given list ; For every k nodes , we move fractionalNode one step ahead . ; First time we see a multiple of k ; A utility function to print a linked list ; Driver code"
Solution in C#:

using System ; public class FractionalNodell { public class Node { public int data ; public Node next ; public Node ( int data ) { this . data = data ; } } static Node fractionalNodes ( Node head , int k ) { if ( k <= 0 head == null ) return null ; Node fractionalNode = null ; int i = 0 ; for ( Node temp = head ; temp != null ; temp = temp . next ) { if ( i % k == 0 ) { if ( fractionalNode == null ) fractionalNode = head ; else fractionalNode = fractionalNode . next ; } i ++ ; } return fractionalNode ; } static void printList ( Node node ) { while ( node != null ) { Console . Write ( node . data + " ▁ " ) ; node = node . next ; } Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { Node head = new Node ( 1 ) ; head . next = new Node ( 2 ) ; head . next . next = new Node ( 3 ) ; head . next . next . next = new Node ( 4 ) ; head . next . next . next . next = new Node ( 5 ) ; int k = 2 ; Console . Write ( " List ▁ is ▁ " ) ; printList ( head ) ; Node answer = fractionalNodes ( head , k ) ; Console . WriteLine ( " Fractional ▁ node ▁ is ▁ " + answer . data ) ; } }


"Find the frequency of each element in a sorted array | C # program for the above approach ; Function to print the frequency of each element of the sorted array ; Stores the frequency of an element ; Traverse the array arr [ ] ; If the current element is equal to the previous element ; Increment the freq by 1 ; Otherwise , ; Update freq ; Print the frequency of the last element ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; public class GFG { static void printFreq ( int [ ] arr , int N ) { int freq = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) { freq ++ ; } else { Console . WriteLine ( " Frequency ▁ of ▁ " + arr [ i - 1 ] + " ▁ is : ▁ " + freq ) ; freq = 1 ; } } Console . WriteLine ( " Frequency ▁ of ▁ " + arr [ N - 1 ] + " ▁ is : ▁ " + freq ) ; } static public void Main ( ) { int [ ] arr = { 1 , 1 , 1 , 2 , 3 , 3 , 5 , 5 , 8 , 8 , 8 , 9 , 9 , 10 } ; int N = arr . Length ; printFreq ( arr , N ) ; } }


"Find the good permutation of first N natural numbers | C # implementation of the approach ; Function to print the good permutation of first N natural numbers ; If n is odd ; Otherwise ; Driver code"
Solution in C#:

using System ; class GFG { static int printPermutation ( int n ) { if ( n % 2 != 0 ) { Console . WriteLine ( " - 1" ) ; } else for ( int i = 1 ; i <= n / 2 ; i ++ ) { Console . WriteLine ( 2 * i + " ▁ " + ( ( 2 * i ) - 1 ) + " ▁ " ) ; } return n ; } public static void Main ( String [ ] args ) { int n = 4 ; printPermutation ( n ) ; } }


"Find the greater number closest to N having at most one non | C # program to implement the above approach ; Function to calculate X ^ n in log ( n ) ; Stores the value of X ^ n ; If N is odd ; Function to find the closest number > N having at most 1 non - zero digit ; Stores the count of digits in N ; Stores the power of 10 ^ ( n - 1 ) ; Stores the last ( n - 1 ) digits ; Store the answer ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static int power ( int X , int n ) { int res = 1 ; while ( n != 0 ) { if ( ( n & 1 ) != 0 ) res = res * X ; X = X * X ; n = n >> 1 ; } return res ; } static int closestgtNum ( int N ) { int n = ( int ) Math . Log10 ( N ) + 1 ; int P = power ( 10 , n - 1 ) ; int Y = N % P ; int res = N + ( P - Y ) ; return res ; } public static void Main ( ) { int N = 120 ; Console . Write ( closestgtNum ( N ) ) ; } }


"Find the greater number closest to N having at most one non | C # program to implement the above approach ; Function to get closest greater number with at most non zero digit ; Stores the closest greater number with at most one non - zero digit ; Stores length of str ; Append 10 to the end of resultant string ; Append n - 1 times '0' to the end of resultant string ; Driver code"
Solution in C#:

using System ; class GFG { public static string closestgtNum ( string str ) { string res = " " ; int n = str . Length ; if ( str [ 0 ] < '9' ) { res = res + ( char ) ( str [ 0 ] + 1 ) ; } else { res = res + '1' ; res = res + '0' ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { res = res + '0' ; } return res ; } static void Main ( ) { string str = "120" ; Console . WriteLine ( closestgtNum ( str ) ) ; } }


"Find the height of a right | C # implementation of the approach ; Function to return the height of the right - angled triangle whose area is X times its base ; Driver code"
Solution in C#:

using System ; class Gfg { static int getHeight ( int X ) { return ( 2 * X ) ; } public static void Main ( ) { int X = 35 ; Console . WriteLine ( getHeight ( X ) ) ; } }


"Find the highest occurring digit in prime numbers in a range | C # program to find the highest occurring digit in prime numbers in a range L to R . ; Sieve of Eratosthenes ; Returns maximum occurring digits in primes from l to r . ; Finding the prime number up to R . ; Initialise frequency of all digit to 0. ; For all number between L to R , check if prime or not . If prime , incrementing the frequency of digits present in the prime number . ; int p = i ; If i is prime ; Finding digit with highest frequency . ; Driver code"
Solution in C#:

using System ; class GFG { static void sieve ( bool [ ] prime , int n ) { for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == false ) for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = true ; } } static int maxDigitInPrimes ( int L , int R ) { bool [ ] prime = new bool [ R + 1 ] ; for ( int i = 0 ; i < R + 1 ; i ++ ) prime [ i ] = false ; sieve ( prime , R ) ; int [ ] freq = new int [ 10 ] ; for ( int i = L ; i <= R ; i ++ ) { if ( ! prime [ i ] ) { while ( p > 0 ) { freq [ p % 10 ] ++ ; p /= 10 ; } } } int max = freq [ 0 ] , ans = 0 ; for ( int j = 1 ; j < 10 ; j ++ ) { if ( max <= freq [ j ] ) { max = freq [ j ] ; ans = j ; } } return ( max != 0 ) ? ans : - 1 ; } public static void Main ( ) { int L = 1 , R = 20 ; Console . Write ( maxDigitInPrimes ( L , R ) ) ; } }


"Find the index having sum of elements on its left equal to reverse of the sum of elements on its right | C # Program to implement the above approach ; Function to check if a number is equal to the reverse of digits of other number ; Stores reverse of digits of rightSum ; Stores rightSum ; Calculate reverse of digits of temp ; Update rev ; Update temp ; If reverse of digits of rightSum equal to leftSum ; Function to find the index that satisfies the condition ; Stores sum of array elements on right side of each index ; Stores sum of array elements on left side of each index ; Traverse the array ; Update rightSum ; Traverse the array ; Update rightSum ; If leftSum equal to reverse of digits of rightSum ; Update leftSum ; Driver code"
Solution in C#:

using System ; class GFG { static bool checkReverse ( int leftSum , int rightSum ) { int rev = 0 ; int temp = rightSum ; while ( temp != 0 ) { rev = ( rev * 10 ) + ( temp % 10 ) ; temp /= 10 ; } if ( rev == leftSum ) { return true ; } return false ; } static int findIndex ( int [ ] arr , int N ) { int rightSum = 0 ; int leftSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { rightSum += arr [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { rightSum -= arr [ i ] ; if ( checkReverse ( leftSum , rightSum ) ) { return i ; } leftSum += arr [ i ] ; } return - 1 ; } static void Main ( ) { int [ ] arr = { 5 , 7 , 3 , 6 , 4 , 9 , 2 } ; int N = arr . Length ; Console . Write ( findIndex ( arr , N ) ) ; } }


"Find the index in a circular array from which prefix sum is always non | C # program for the above approach ; Function to find the starting index of the given circular array s . t . prefix sum array is non negative ; Stores the sum of the array ; Stores the starting index ; Stores the minimum prefix sum of A [ 0. . i ] ; Traverse the array arr [ ] ; Update the value of sum ; If sum is less than min ; Update the min as the value of prefix sum ; Update in ; Otherwise , no such index is possible ; Driver Code"
Solution in C#:

using System ; class GFG { static int startingPoint ( int [ ] A , int N ) { int sum = 0 ; int ind = 0 ; int min = Int32 . MaxValue ; for ( int i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; if ( sum < min ) { min = sum ; ind = i + 1 ; } } if ( sum < 0 ) { return - 1 ; } return ind % N ; } public static void Main ( ) { int [ ] arr = { 3 , - 6 , 7 , - 4 , - 4 , 6 , - 1 } ; int N = arr . Length ; Console . Write ( startingPoint ( arr , N ) ) ; } }


"Find the index of the left pointer after possible moves in the array | C # program to find the index of the left pointer ; Function that returns the index of the left pointer ; there 's only one element in the array ; initially both are at end ; Driver code"
Solution in C#:

using System ; class GFG { static int getIndex ( int [ ] a , int n ) { if ( n == 1 ) return 0 ; int ptrL = 0 , ptrR = n - 1 , sumL = a [ 0 ] , sumR = a [ n - 1 ] ; while ( ptrR - ptrL > 1 ) { if ( sumL < sumR ) { ptrL ++ ; sumL += a [ ptrL ] ; } else if ( sumL > sumR ) { ptrR -- ; sumR += a [ ptrR ] ; } else { break ; } } return ptrL ; } public static void Main ( ) { int [ ] a = { 2 , 7 , 9 , 8 , 7 } ; int n = a . Length ; Console . WriteLine ( getIndex ( a , n ) ) ; } }


"Find the index of the smallest element to be removed to make sum of array divisible by K | C # program to implement the above approach ; Function to find index of the smallest array element required to be removed to make sum divisible by K ; Stores sum of array elements ; Stores index of the smallest element removed from the array to make sum divisible by K ; Stores the smallest element removed from the array to make sum divisible by K ; Traverse the array , arr [ ] ; Update sum ; Traverse the array arr [ ] ; Calculate remaining sum ; If sum is divisible by K ; If res == - 1 or mini is greater than arr [ i ] ; Update res and mini ; Driver code"
Solution in C#:

using System ; class GFG { static int findIndex ( int [ ] arr , int n , int K ) { int sum = 0 ; int res = - 1 ; int mini = ( int ) 1e9 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { int temp = sum - arr [ i ] ; if ( temp % K == 0 ) { if ( res == - 1 mini > arr [ i ] ) { res = i + 1 ; mini = arr [ i ] ; } } } return res ; } static void Main ( ) { int [ ] arr = { 14 , 7 , 8 , 2 , 4 } ; int K = 7 ; int N = arr . Length ; Console . WriteLine ( findIndex ( arr , N , K ) ) ; } }


"Find the index which is the last to be reduced to zero after performing a given operation | C # implementation of the approach ; Function that returns the index which will be the last to become zero after performing given operation ; Initialize the result ; Finding the ceil value of each index ; Finding the index with maximum ceil value ; Driver code"
Solution in C#:

using System ; class GFG { static int findIndex ( int [ ] a , int n , int k ) { int index = - 1 , max_ceil = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = ( a [ i ] + k - 1 ) / k ; } for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= max_ceil ) { max_ceil = a [ i ] ; index = i ; } } return index ; } static public void Main ( ) { int [ ] arr = { 31 , 12 , 25 , 27 , 32 , 19 } ; int K = 5 ; int N = arr . Length ; Console . WriteLine ( findIndex ( arr , N , K ) ) ; } }


"Find the index with minimum score from a given array | C # program for the above approach ; Function to find the index with minimum score ; Stores the score of current index ; Traverse the array in reverse ; Update minimum score ; Print the index with minimum score ; Driver Code"
Solution in C#:

using System ; class GFG { static void Min_Score_Index ( int N , int [ ] A ) { int [ ] Score = new int [ N ] ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( A [ i ] + i < N ) Score [ i ] = A [ i ] * Score [ A [ i ] + i ] ; else Score [ i ] = A [ i ] ; } int min_value = Int32 . MaxValue ; foreach ( int i in Score ) { min_value = Math . Min ( i , min_value ) ; } int ind = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( Score [ i ] == min_value ) ind = i ; } Console . WriteLine ( ind ) ; } public static void Main ( ) { int N = 5 ; int [ ] A = { 1 , 2 , 3 , 4 , 5 } ; Min_Score_Index ( N , A ) ; } }


"Find the integer points ( x , y ) with Manhattan distance atleast N | C # code to Find the integer points ( x , y ) with Manhattan distance atleast N ; Function to find all possible point ; Find all 4 corners of the square whose side length is n ; If n is even then the middle point of the square will be an integer , so we will take that point ; Driver Code ; Printing all possible points"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static List < pair > FindPoints ( int n ) { List < pair > v = new List < pair > ( ) ; v . Add ( new pair ( 0 , 0 ) ) ; v . Add ( new pair ( 0 , n ) ) ; v . Add ( new pair ( n , 0 ) ) ; v . Add ( new pair ( n , n ) ) ; if ( n % 2 == 0 ) v . Add ( new pair ( n / 2 , n / 2 ) ) ; return v ; } public static void Main ( String [ ] args ) { int N = 8 ; List < pair > v = FindPoints ( N ) ; foreach ( pair i in v ) { Console . Write ( " ( " + i . first + " , ▁ " + i . second + " ) ▁ " ) ; } } }


"Find the integers that doesnot ends with T1 or T2 when squared and added X | C # program to find the integers that ends with either T1 or T2 when squared and added X ; Function to print the elements Not ending with T1 or T2 ; Flag to check if none of the elements Do not end with t1 or t2 ; Traverse through all the elements ; Temporary variable to store the value ; If the last digit is neither t1 nor t2 then ; Print the number ; Set the flag as False ; If none of the elements meets the specification ; Driver Code ; Test case 1 ; Call the function ; Test case 2 ; Call the function"
Solution in C#:

using System ; class GFG { static void findIntegers ( int n , int [ ] a , int x , int t1 , int t2 ) { bool flag = true ; for ( int i = 0 ; i < n ; i ++ ) { int temp = ( int ) Math . Pow ( a [ i ] , 2 ) + x ; if ( temp % 10 != t1 && temp % 10 != t2 ) { Console . Write ( temp + " ▁ " ) ; flag = false ; } } if ( flag ) { Console . WriteLine ( ) ; Console . Write ( " - 1" ) ; } } public static void Main ( String [ ] args ) { int N = 4 ; int X = 10 ; int T1 = 5 ; int T2 = 6 ; int [ ] a = { 3 , 1 , 4 , 7 } ; findIntegers ( N , a , X , T1 , T2 ) ; N = 4 ; X = 2 ; T1 = 5 ; T2 = 6 ; int [ ] b = { 2 , 18 , 22 , 8 } ; findIntegers ( N , b , X , T1 , T2 ) ; } }


"Find the integral roots of a given Cubic equation | C # program for the above approach ; Function to find the value at x of the given equation ; Find the value equation at x ; Return the value of ans ; Function to find the integral solution of the given equation ; Initialise start and end ; Implement Binary Search ; Find mid ; Find the value of f ( x ) using current mid ; Check if current mid satisfy the equation ; Print mid and return ; Print " NA " if not found any integral solution ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static long check ( int A , int B , int C , int D , long x ) { long ans ; ans = ( A * x * x * x + B * x * x + C * x + D ) ; return ans ; } static void findSolution ( int A , int B , int C , int D , int E ) { long start = 0 , end = 100000 ; long mid , ans ; while ( start <= end ) { mid = start + ( end - start ) / 2 ; ans = check ( A , B , C , D , mid ) ; if ( ans == E ) { Console . WriteLine ( mid ) ; return ; } if ( ans < E ) start = mid + 1 ; else end = mid - 1 ; } Console . Write ( " NA " ) ; } public static void Main ( ) { int A = 1 , B = 0 , C = 0 ; int D = 0 , E = 27 ; findSolution ( A , B , C , D , E ) ; } }


"Find the intersection of two Matrices | C # program to find intersection of two matrices ; Function to print the resultant matrix ; print element value for equal elements else * ; Driver Code"
Solution in C#:

using System ; class GFG { static int N = 4 ; static int M = 4 ; static void printIntersection ( int [ , ] A , int [ , ] B ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( A [ i , j ] == B [ i , j ] ) Console . Write ( A [ i , j ] + " ▁ " ) ; else Console . Write ( " * ▁ " ) ; } Console . WriteLine ( " ▁ " ) ; } } public static void Main ( ) { int [ , ] A = { { 2 , 4 , 6 , 8 } , { 1 , 3 , 5 , 7 } , { 8 , 6 , 4 , 2 } , { 7 , 5 , 3 , 1 } } ; int [ , ] B = { { 2 , 3 , 6 , 8 } , { 1 , 3 , 5 , 2 } , { 8 , 1 , 4 , 2 } , { 3 , 5 , 4 , 1 } } ; printIntersection ( A , B ) ; } }


"Find the k largest numbers after deleting the given elements |  ; Find k maximum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Initializing the largestElement ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else compare it largestElement ; Print top k elements in the heap ; Print top k elements in the heap ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findElementsAfterDel ( int [ ] arr , int m , int [ ] del , int n , int k ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( mp . ContainsKey ( del [ i ] ) ) { mp [ del [ i ] ] ++ ; } else { mp . Add ( del [ i ] , 1 ) ; } } List < int > heap = new List < int > ( ) ; for ( int i = 0 ; i < m ; ++ i ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] -- ; if ( mp [ arr [ i ] ] == 0 ) { mp . Remove ( arr [ i ] ) ; } } else { heap . Add ( arr [ i ] ) ; } } heap . Sort ( ) ; heap . Reverse ( ) ; for ( int i = 0 ; i < k ; ++ i ) { Console . Write ( heap [ i ] + " ▁ " ) ; } } static public void Main ( ) { int [ ] array = { 5 , 12 , 33 , 4 , 56 , 12 , 20 } ; int m = array . Length ; int [ ] del = { 12 , 56 , 5 } ; int n = del . Length ; int k = 3 ; findElementsAfterDel ( array , m , del , n , k ) ; } }


"Find the k smallest numbers after deleting given elements | C # program to find the k maximum number from the array after n deletions ; Find k minimum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else push it in the min heap ; Print top k elements in the min heap ; Pop the top element ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findElementsAfterDel ( int [ ] arr , int m , int [ ] del , int n , int k ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( mp . ContainsKey ( del [ i ] ) ) { mp [ del [ i ] ] ++ ; } else { mp [ del [ i ] ] = 1 ; } } List < int > heap = new List < int > ( ) ; for ( int i = 0 ; i < m ; ++ i ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] -- ; if ( mp [ arr [ i ] ] == 0 ) mp . Remove ( arr [ i ] ) ; } else heap . Add ( arr [ i ] ) ; } heap . Sort ( ) ; for ( int i = 0 ; i < k ; ++ i ) { Console . Write ( heap [ 0 ] + " ▁ " ) ; heap . RemoveAt ( 0 ) ; } } static void Main ( ) { int [ ] array = { 5 , 12 , 33 , 4 , 56 , 12 , 20 } ; int m = array . Length ; int [ ] del = { 12 , 56 , 5 } ; int n = del . Length ; int k = 3 ; findElementsAfterDel ( array , m , del , n , k ) ; } }


"Find the k | C # program find the Kth String in lexicographical order consisting of N - 2 xs and 2 ys ; Function to find the Kth String in lexicographical order which consists of N - 2 xs and 2 ys ; Iterate for all possible positions of eft most Y ; If i is the left most position of Y ; Put Y in their positions ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static void kth_String ( int n , int k ) { for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( k <= ( n - i - 1 ) ) { for ( int j = 0 ; j < n ; j ++ ) { if ( j == i j == n - k ) Console . Write ( ' Y ' ) ; else Console . Write ( ' X ' ) ; } break ; } k -= ( n - i - 1 ) ; } } public static void Main ( String [ ] args ) { int n = 5 , k = 7 ; kth_String ( n , k ) ; } }


"Find the k | C # program to find the K - th smallest factor ; Function to find the k 'th divisor ; initialize vectors v1 and v2 ; store all the divisors in the two vectors accordingly ; reverse the vector v2 to sort it in increasing order ; if k is greater than the size of vectors then no divisor can be possible ; else print the ( k - 1 ) th value of vector ; If K is lying in first vector ; If K is lying in second vector ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findkth ( int n , int k ) { List < int > v1 = new List < int > ( ) ; List < int > v2 = new List < int > ( ) ; for ( int i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v1 . Add ( i ) ; if ( i != Math . Sqrt ( n ) ) v2 . Add ( n / i ) ; } } v2 . Reverse ( ) ; if ( k > ( v1 . Count + v2 . Count ) ) Console . Write ( " Doesn ' t ▁ Exist " ) ; else { if ( k <= v1 . Count ) Console . Write ( v1 [ k - 1 ] ) ; else Console . Write ( v2 [ k - v1 . Count - 1 ] ) ; } } public static void Main ( String [ ] args ) { int n = 15 , k = 2 ; findkth ( n , k ) ; } }


"Find the kth element in the series generated by the given N ranges | C # implementation of the approach ; Function to return the kth element of the required series ; To store the number of integers that lie upto the ith index ; Compute the number of integers ; Stores the index , lying from 1 to n , ; Using binary search , find the index in which the kth element will lie ; Find the position of the kth element in the interval in which it lies ; Driver code"
Solution in C#:

using System ; class GFG { static int getKthElement ( int n , int k , int [ ] L , int [ ] R ) { int l = 1 ; int h = n ; int [ ] total = new int [ n + 1 ] ; total [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { total [ i + 1 ] = total [ i ] + ( R [ i ] - L [ i ] ) + 1 ; } int index = - 1 ; while ( l <= h ) { int m = ( l + h ) / 2 ; if ( total [ m ] > k ) { index = m ; h = m - 1 ; } else if ( total [ m ] < k ) l = m + 1 ; else { index = m ; break ; } } l = L [ index - 1 ] ; h = R [ index - 1 ] ; int x = k - total [ index - 1 ] ; while ( l <= h ) { int m = ( l + h ) / 2 ; if ( ( m - L [ index - 1 ] ) + 1 == x ) { return m ; } else if ( ( m - L [ index - 1 ] ) + 1 > x ) h = m - 1 ; else l = m + 1 ; } return k ; } public static void Main ( ) { int [ ] L = { 1 , 8 , 21 } ; int [ ] R = { 4 , 10 , 23 } ; int n = L . Length ; int k = 6 ; Console . WriteLine ( getKthElement ( n , k , L , R ) ) ; } }


"Find the kth smallest number with sum of digits as m | C # implementation of the approach ; Recursively moving to add all the numbers upto a limit with sum of digits as m ; Max number of digits allowed in a number for this implementation ; Function to return the kth number with sum of digits as m ; The kth smallest number is found ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int N = 2005 ; static HashSet < int > ans = new HashSet < int > ( ) ; static void dfs ( int num , int left , int ct ) { if ( ct >= 15 ) return ; if ( left == 0 ) ans . Add ( num ) ; for ( int i = 0 ; i <= Math . Min ( left , 9 ) ; i ++ ) dfs ( num * 10 + i , left - i , ct + 1 ) ; } static int getKthNum ( int m , int k ) { dfs ( 0 , m , 0 ) ; int ct = 0 ; foreach ( int it in ans ) { ct ++ ; if ( ct == k ) { return it ; } } return - 1 ; } public static void Main ( String [ ] args ) { int m = 5 , k = 3 ; Console . WriteLine ( getKthNum ( m , k ) ) ; } }


"Find the largest Alphabetic character present in the string | C # program to Find the Largest Alphabetic char present in the string of both uppercase and lowercase English characters ; Function to find the Largest Alphabetic char ; Array for keeping track of both uppercase and lowercase english alphabets ; Iterate from right side of array to get the largest index character ; Check for the character if both its uppercase and lowercase exist or not ; Return - 1 if no such character whose uppercase and lowercase present in string str ; Driver code"
Solution in C#:

using System ; class GFG { public static String largestchar ( String str ) { bool [ ] uppercase = new bool [ 26 ] ; bool [ ] lowercase = new bool [ 26 ] ; char [ ] arr = str . ToCharArray ( ) ; foreach ( char c in arr ) { if ( char . IsLower ( c ) ) lowercase = true ; if ( char . IsUpper ( c ) ) uppercase = true ; } for ( int i = 25 ; i >= 0 ; i -- ) { if ( uppercase [ i ] && lowercase [ i ] ) return ( char ) ( i + ' A ' ) + " " ; } return " - 1" ; } public static void Main ( String [ ] args ) { String str = " admeDCAB " ; Console . WriteLine ( largestchar ( str ) ) ; } }


"Find the largest N digit multiple of N | C # program to find largest multiple of N containing N digits ; Function to find the largest N digit multiple of N ; Driver code"
Solution in C#:

using System ; class GFG { static void smallestNumber ( int N ) { Console . Write ( N * Math . Floor ( ( Math . Pow ( 10 , N ) - 1 ) / N ) ) ; } public static void Main ( ) { int N = 2 ; smallestNumber ( N ) ; } }


"Find the largest after deleting the given elements | C # program to find the largest number from the array after n deletions ; Returns maximum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Initializing the largestElement ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else compare it largestElement ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int findlargestAfterDel ( int [ ] arr , int m , int [ ] del , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( mp . ContainsKey ( del [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( del [ i ] , 1 ) ; } } int largestElement = int . MinValue ; for ( int i = 0 ; i < m ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] - 1 ; if ( mp [ arr [ i ] ] == 0 ) mp . Remove ( arr [ i ] ) ; } else largestElement = Math . Max ( largestElement , arr [ i ] ) ; } return largestElement ; } public static void Main ( String [ ] args ) { int [ ] array = { 5 , 12 , 33 , 4 , 56 , 12 , 20 } ; int m = array . Length ; int [ ] del = { 12 , 33 , 56 , 5 } ; int n = del . Length ; Console . WriteLine ( findlargestAfterDel ( array , m , del , n ) ) ; } }


"Find the largest area rectangular sub | C # implementation to find the largest area rectangular sub - matrix whose sum is equal to k ; This function basically finds largest ' k ' sum subarray in arr [ 0. . n - 1 ] . If ' k ' sum doesn 't exist, then it returns false. Else it returns true and sets starting and ending indexes as start and end. ; unordered_map ' um ' implemented as hash table ; traverse the given array ; accumulate sum ; when subarray starts from index '0' update maxLength and start and end points ; make an entry for ' sum ' if it is not present in ' um ' ; check if ' sum - k ' is present in ' um ' or not ; update maxLength and start and end points ; Return true if maximum length is non - zero ; function to find the largest area rectangular sub - matrix whose sum is equal to k ; Variables to store the temporary values ; Variables to store the readonly output ; Set the left column ; Initialize all elements of temp as 0 ; Set the right column for the left column set by outer loop ; Calculate sum between current left and right column for every row ' i ' ; Find largest subarray with ' k ' sum in [ ] temp . The sumEqualToK ( ) function also sets values of ' up ' and ' down ; ' . So if ' sum ' is true then rectangle exists between ( up , left ) and ( down , right ) which are the boundary values . ; Compare no . of elements with previous no . of elements in sub - Matrix . If new sub - matrix has more elements then update maxArea and readonly boundaries like fup , fdown , fleft , fright ; If there is no change in boundaries than check if mat [ 0 , 0 ] equals ' k ' If it is not equal to ' k ' then print that no such k - sum sub - matrix exists ; Print readonly values ; Driver code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static int MAX = 100 ; static int start , end ; static bool sumEqualToK ( int [ ] arr , int n , int k ) { Dictionary < int , int > um = new Dictionary < int , int > ( ) ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum == k ) { maxLen = i + 1 ; start = 0 ; end = i ; } if ( ! um . ContainsKey ( sum ) ) um . Add ( sum , i ) ; if ( um . ContainsKey ( sum - k ) ) { if ( maxLen < ( i - um [ sum - k ] ) ) { maxLen = i - um [ sum - k ] ; start = um [ sum - k ] + 1 ; end = i ; } } } return ( maxLen != 0 ) ; } static void sumZeroMatrix ( int [ , ] mat , int row , int col , int k ) { int [ ] temp = new int [ row ] ; int area ; bool sum = false ; int fup = 0 , fdown = 0 , fleft = 0 , fright = 0 ; int maxArea = int . MinValue ; for ( int left = 0 ; left < col ; left ++ ) { temp = memset ( temp , 0 ) ; for ( int right = left ; right < col ; right ++ ) { for ( int i = 0 ; i < row ; i ++ ) temp [ i ] += mat [ i , right ] ; sum = sumEqualToK ( temp , row , k ) ; area = ( end - start + 1 ) * ( right - left + 1 ) ; if ( sum && maxArea < area ) { fup = start ; fdown = end ; fleft = left ; fright = right ; maxArea = area ; } } } if ( fup == 0 && fdown == 0 && fleft == 0 && fright == 0 && mat [ 0 , 0 ] != k ) { Console . Write ( " No ▁ sub - matrix ▁ with ▁ sum ▁ " + k + " ▁ exists " ) ; return ; } Console . Write ( " ( Top , ▁ Left ) : ▁ " + " ( " + fup + " , ▁ " + fleft + " ) " + "
 " ) ; Console . Write ( " ( Bottom , ▁ Right ) : ▁ " + " ( " + fdown + " , ▁ " + fright + " ) " + "
 " ) ; for ( int j = fup ; j <= fdown ; j ++ ) { for ( int i = fleft ; i <= fright ; i ++ ) Console . Write ( mat [ j , i ] + " ▁ " ) ; Console . WriteLine ( ) ; } } static int [ ] memset ( int [ ] arr , int val ) { for ( int i = 0 ; i < arr . Length ; i ++ ) arr [ i ] = val ; return arr ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 7 , - 6 , 5 } , { - 8 , 6 , 7 , - 2 } , { 10 , - 15 , 3 , 2 } , { - 5 , 2 , 0 , 9 }...