"Find k 'th character of decrypted string | Set 1 | C # program to find K 'th character in decrypted string ; Function to find K 'th  character in Encoded String ; expand string variable is used to store final string after decompressing string str ; String temp = " " ; Current substring int freq = 0 ; Count of current substring ; read characters until you find a number or end of string ; push character in temp ; read number for how many times string temp will be repeated in decompressed string ; generating frequency of temp ; now append string temp into expand equal to its frequency ; this condition is to handle the case when string str is ended with alphabets not with numeric value ; Driver Code"
Solution in C#:

using System ; class GFG { static char encodedChar ( string str , int k ) { String expand = " " ; for ( int i = 0 ; i < str . Length ; ) { while ( i < str . Length && str [ i ] >= ' a ' && str [ i ] <= ' z ' ) { temp += str [ i ] ; i ++ ; } while ( i < str . Length && str [ i ] >= '1' && str [ i ] <= '9' ) { freq = freq * 10 + str [ i ] - '0' ; i ++ ; } for ( int j = 1 ; j <= freq ; j ++ ) expand += temp ; } if ( freq == 0 ) expand += temp ; return expand [ k - 1 ] ; } public static void Main ( ) { string str = " ab4c12ed3" ; int k = 21 ; Console . Write ( encodedChar ( str , k ) ) ; } }


"Find k pairs with smallest sums in two arrays | C # code to print first k pairs with least with least sum from two arrays . ; Function to find k pairs with least sum such that one elemennt of a pair is from arr1 [ ] and other element is from arr2 [ ] ; Stores current index in arr2 [ ] for every element of arr1 [ ] . Initially all values are considered 0. Here current index is the index before which all elements are considered as part of output . ; Initialize current pair sum as infinite ; To pick next pair , traverse for all elements of arr1 [ ] , for every element , find corresponding current element in arr2 [ ] and pick minimum of all formed pairs . ; Check if current element of arr1 [ ] plus element of array2 to be used gives minimum sum ; Update index that gives minimum ; update minimum sum ; Driver code"
Solution in C#:

using System ; class KSmallestPair { static void kSmallestPair ( int [ ] arr1 , int n1 , int [ ] arr2 , int n2 , int k ) { if ( k > n1 * n2 ) { Console . Write ( " k ▁ pairs ▁ don ' t ▁ exist " ) ; return ; } int [ ] index2 = new int [ n1 ] ; while ( k > 0 ) { int min_sum = int . MaxValue ; int min_index = 0 ; for ( int i1 = 0 ; i1 < n1 ; i1 ++ ) { if ( index2 [ i1 ] < n2 && arr1 [ i1 ] + arr2 [ index2 [ i1 ] ] < min_sum ) { min_index = i1 ; min_sum = arr1 [ i1 ] + arr2 [ index2 [ i1 ] ] ; } } Console . Write ( " ( " + arr1 [ min_index ] + " , ▁ " + arr2 [ index2 [ min_index ] ] + " ) ▁ " ) ; index2 [ min_index ] ++ ; k -- ; } } public static void Main ( String [ ] args ) { int [ ] arr1 = { 1 , 3 , 11 } ; int n1 = arr1 . Length ; int [ ] arr2 = { 2 , 4 , 8 } ; int n2 = arr2 . Length ; int k = 4 ; kSmallestPair ( arr1 , n1 , arr2 , n2 , k ) ; } }


"Find k pairs with smallest sums in two arrays | Set 2 | C # program to print the k smallest pairs | Set 2 ; Function to print the K smallest pairs ; if k is greater than total pairs ; _pair _one keeps track of ' first ' in a1 and ' second ' in a2 in _two , _two . first keeps track of element in the a2 [ ] and _two . second in a1 [ ] ; Repeat the above process till all K pairs are printed ; when both the pointers are pointing to the same elements ( point 3 ) ; updates according to step 1 ; see point 2 ; updates opposite to step 1 ; updates according to rule 1 ; see point 2 ; updates opposite to rule 1 ; see point 2 ; else update as necessary ( point 1 ) ; updating according to rule 1 ; see point 2 ; updating according to rule 1 ; see point 2 ; updating according to rule 1 ; see point 2 ; updating according to rule 1 ; see point 2 ; Driver Code"
Solution in C#:

 using System ; class GFG { public class _pair { public int first , second ; } ; static void printKPairs ( int [ ] a1 , int [ ] a2 , int size1 , int size2 , int k ) { if ( k > ( size2 * size1 ) ) { Console . Write ( " k ▁ pairs ▁ don ' t ▁ exist
 " ) ; return ; } _pair _one = new _pair ( ) ; _pair _two = new _pair ( ) ; _one . first = _one . second = _two . first = _two . second = 0 ; int cnt = 0 ; while ( cnt < k ) { if ( _one . first == _two . second && _two . first == _one . second ) { if ( a1 [ _one . first ] < a2 [ _one . second ] ) { Console . Write ( " [ " + a1 [ _one . first ] + " , ▁ " + a2 [ _one . second ] + " ] ▁ " ) ; _one . second = ( _one . second + 1 ) % size2 ; if ( _one . second == 0 ) _one . first = ( _one . first + 1 ) % size1 ; _two . second = ( _two . second + 1 ) % size2 ; if ( _two . second == 0 ) _two . first = ( _two . first + 1 ) % size2 ; } else { Console . Write ( " [ " + a2 [ _one . second ] + " , ▁ " + a1 [ _one . first ] + " ] ▁ " ) ; _one . first = ( _one . first + 1 ) % size1 ; if ( _one . first == 0 ) _one . second = ( _one . second + 1 ) % size2 ; _two . first = ( _two . first + 1 ) % size2 ; if ( _two . first == 0 ) _two . second = ( _two . second + 1 ) % size1 ; } } else if ( a1 [ _one . first ] + a2 [ _one . second ] <= a2 [ _two . first ] + a1 [ _two . second ] ) { if ( a1 [ _one . first ] < a2 [ _one . second ] ) { Console . Write ( " [ " + a1 [ _one . first ] + " , ▁ " + a2 [ _one . second ] + " ] ▁ " ) ; _one . second = ( ( _one . second + 1 ) % size2 ) ; if ( _one . second == 0 ) _one . first = ( _one . first + 1 ) % size1 ; } else { Console . Write ( " [ " + a2 [ _one . second ] + " , ▁ " + a1 [ _one . first ] + " ] ▁ " ) ; _one . first = ( ( _one . first + 1 ) % size1 ) ; if ( _one . first == 0 ) _one . second = ( _one . second + 1 ) % size2 ; } } else if ( a1 [ _one . first ] + a2 [ _one . second ] > a2 [ _two . first ] + a1 [ _two . second ] ) { if ( a2 [ _two . first ] < a1 [ _two . second ] ) { Console . Write ( " [ " + a2 [ _two . first ] + " , ▁ " + a1 [...


"Find k | A simple inorder traversal based C # program to find k - th smallest element in a BST . ; A BST node ; Recursive function to insert an key into BST ; Function to find k 'th largest element in BST. Here count denotes the number of nodes processed so far ; base case ; search in left subtree ; if k 'th smallest is found  in left subtree, return it ; if current element is k 'th smallest, return it ; else search in right subtree ; Function to find k 'th largest element in BST ; Maintain an index to count number of nodes processed so far ; Driver code"
Solution in C#:

using System ; class Node { public int data ; public Node left , right ; public Node ( int x ) { data = x ; left = right = null ; } } class GFG { static int count = 0 ; public static Node insert ( Node root , int x ) { if ( root == null ) return new Node ( x ) ; if ( x < root . data ) root . left = insert ( root . left , x ) ; else if ( x > root . data ) root . right = insert ( root . right , x ) ; return root ; } public static Node kthSmallest ( Node root , int k ) { if ( root == null ) return null ; Node left = kthSmallest ( root . left , k ) ; if ( left != null ) return left ; count ++ ; if ( count == k ) return root ; return kthSmallest ( root . right , k ) ; } public static void printKthSmallest ( Node root , int k ) { count = 0 ; Node res = kthSmallest ( root , k ) ; if ( res == null ) Console . WriteLine ( " There ▁ are ▁ less ▁ " + " than ▁ k ▁ nodes ▁ in ▁ the ▁ BST " ) ; else Console . WriteLine ( " K - th ▁ Smallest " + " ▁ Element ▁ is ▁ " + res . data ) ; } public static void Main ( String [ ] args ) { Node root = null ; int [ ] keys = { 20 , 8 , 22 , 4 , 12 , 10 , 14 } ; foreach ( int x in keys ) root = insert ( root , x ) ; int k = 3 ; printKthSmallest ( root , k ) ; } }


"Find k | A simple inorder traversal based C # program to find k - th smallest element in a BST . ; A BST node ; Recursive function to insert an key into BST ; If a node is inserted in left subtree , then lCount of this node is increased . For simplicity , we are assuming that all keys ( tried to be inserted ) are distinct . ; Function to find k 'th largest element in BST. Here count denotes the number of nodes processed so far ; Base case ; Else search in right subtree ; Driver Code"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right ; public int lCount ; public Node ( int x ) { data = x ; left = right = null ; lCount = 0 ; } } class GFG { public static Node insert ( Node root , int x ) { if ( root == null ) return new Node ( x ) ; if ( x < root . data ) { root . left = insert ( root . left , x ) ; root . lCount ++ ; } else if ( x > root . data ) root . right = insert ( root . right , x ) ; return root ; } public static Node kthSmallest ( Node root , int k ) { if ( root == null ) return null ; int count = root . lCount + 1 ; if ( count == k ) return root ; if ( count > k ) return kthSmallest ( root . left , k ) ; return kthSmallest ( root . right , k - count ) ; } public static void Main ( String [ ] args ) { Node root = null ; int [ ] keys = { 20 , 8 , 22 , 4 , 12 , 10 , 14 } ; foreach ( int x in keys ) root = insert ( root , x ) ; int k = 4 ; Node res = kthSmallest ( root , k ) ; if ( res == null ) Console . WriteLine ( " There ▁ are ▁ less ▁ " + " than ▁ k ▁ nodes ▁ in ▁ the ▁ BST " ) ; else Console . WriteLine ( " K - th ▁ Smallest " + " ▁ Element ▁ is ▁ " + res . data ) ; } }


"Find k | C # implementation to solve k queries for given n ranges ; Structure to store the start and end point ; Comparison function for sorting ; Function to find Kth smallest number in a Vector of merged intervals ; Traverse merged . get ( ) o find Kth smallest element using Linear search . ; To combined both type of ranges , overlapping as well as non - overlapping . ; Sorting intervals according to start time ; Merging all intervals into merged ; To check if starting point of next range is lying between the previous range and ending point of next range is greater than the Ending point of previous range then update ending point of previous range by ending point of next range . ; If starting point of next range is greater than the ending point of previous range then store next range in merged . get ( . ) if ( curr . s > prev . e ) ; Driver code ; Merge all intervals into merged . get ( ) ) ; Processing all queries on merged intervals"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { public class Interval { public int s ; public int e ; public Interval ( int a , int b ) { s = a ; e = b ; } } ; class sortHelper : IComparer { int IComparer . Compare ( object a , object b ) { Interval first = ( Interval ) a ; Interval second = ( Interval ) b ; return first . s - second . s ; } } static int kthSmallestNum ( ArrayList merged , int k ) { int n = merged . Count ; for ( int j = 0 ; j < n ; j ++ ) { if ( k <= Math . Abs ( ( ( Interval ) merged [ j ] ) . e - ( ( Interval ) merged [ j ] ) . s + 1 ) ) return ( ( ( Interval ) merged [ j ] ) . s + k - 1 ) ; k = k - Math . Abs ( ( ( Interval ) merged [ j ] ) . e - ( ( Interval ) merged [ j ] ) . s + 1 ) ; } if ( k != 0 ) return - 1 ; return 0 ; } static ArrayList mergeIntervals ( ArrayList merged , Interval [ ] arr , int n ) { Array . Sort ( arr , new sortHelper ( ) ) ; merged . Add ( ( Interval ) arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { Interval prev = ( Interval ) merged [ merged . Count - 1 ] ; Interval curr = arr [ i ] ; if ( ( curr . s >= prev . s && curr . s <= prev . e ) && ( curr . e > prev . e ) ) { ( ( Interval ) merged [ merged . Count - 1 ] ) . e = ( ( Interval ) curr ) . e ; } else { merged . Add ( curr ) ; } } return merged ; } public static void Main ( string [ ] args ) { Interval [ ] arr = { new Interval ( 2 , 6 ) , new Interval ( 4 , 7 ) } ; int n = arr . Length ; int [ ] query = { 5 , 8 } ; int q = query . Length ; ArrayList merged = new ArrayList ( ) ; merged = mergeIntervals ( merged , arr , n ) ; for ( int i = 0 ; i < q ; i ++ ) Console . WriteLine ( kthSmallestNum ( merged , query [ i ] ) ) ; } }


"Find k | C # implementation to solve k queries for given n ranges ; Structure to store the start and end point ; Function to find Kth smallest number in a Vector of merged intervals ; Traverse merged . get ( ) o find Kth smallest element using Linear search . ; To combined both type of ranges , overlapping as well as non - overlapping . ; Sorting intervals according to start time ; Merging all intervals into merged ; To check if starting point of next range is lying between the previous range and ending point of next range is greater than the Ending point of previous range then update ending point of previous range by ending point of next range . ; If starting point of next range is greater than the ending point of previous range then store next range in merged . get ( . ) if ( curr . s > prev . e ) ; Driver code ; Merge all intervals into merged . get ( ) ) ; Processing all queries on merged intervals"
Solution in C#:


"Find k | C # program to find K - Cores of a graph ; This class represents a undirected graph using adjacency list representation ; No . of vertices ; Pointer to an array containing adjacency lists ; Function to add an edge to graph ; A recursive function to print DFS starting from v . It returns true if degree of v after processing is less than k else false It also updates degree of adjacent if degree of v is less than k . And if degree of a processed adjacent becomes less than k , then it reduces of degree of v also , ; Mark the current node as visited and print it ; Recur for all the vertices adjacent to this vertex ; Degree of v is less than k , then degree of adjacent must be reduced ; If adjacent is not processed , process it ; If degree of adjacent after processing becomes less than k , then reduce degree of v also . ; Return true if degree of v is less than k ; Prints k cores of an undirected graph ; INITIALIZATION Mark all the vertices as not visited and not processed . ; Store degrees of all vertices ; DFS traversal to update degrees of all vertices . ; PRINTING K CORES ; Only considering those vertices which have degree >= K after BFS ; Traverse adjacency list of v and print only those adjacent which have vDegree >= k after BFS . ; Driver Code ; Create a graph given in the above diagram"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { public class Graph { int V ; List < int > [ ] adj ; public Graph ( int V ) { this . V = V ; this . adj = new List < int > [ V ] ; for ( int i = 0 ; i < V ; i ++ ) adj [ i ] = new List < int > ( ) ; } public void addEdge ( int u , int v ) { this . adj [ u ] . Add ( v ) ; this . adj [ v ] . Add ( u ) ; } bool DFSUtil ( int v , bool [ ] visited , int [ ] vDegree , int k ) { visited [ v ] = true ; foreach ( int i in adj [ v ] ) { if ( vDegree [ v ] < k ) vDegree [ i ] -- ; if ( ! visited [ i ] ) { DFSUtil ( i , visited , vDegree , k ) ; } } return ( vDegree [ v ] < k ) ; } public void printKCores ( int k ) { bool [ ] visited = new bool [ V ] ; bool [ ] processed = new bool [ V ] ; int mindeg = int . MaxValue ; int startvertex = 0 ; int [ ] vDegree = new int [ V ] ; for ( int i = 0 ; i < V ; i ++ ) { vDegree [ i ] = adj [ i ] . Count ; if ( vDegree [ i ] < mindeg ) { mindeg = vDegree [ i ] ; startvertex = i ; } } DFSUtil ( startvertex , visited , vDegree , k ) ; for ( int i = 0 ; i < V ; i ++ ) if ( ! visited [ i ] ) DFSUtil ( i , visited , vDegree , k ) ; Console . WriteLine ( " K - Cores ▁ : ▁ " ) ; for ( int v = 0 ; v < V ; v ++ ) { if ( vDegree [ v ] >= k ) { Console . Write ( "
 ▁ " + v ) ; foreach ( int i in adj [ v ] ) if ( vDegree [ i ] >= k ) Console . Write ( " ▁ - > ▁ " + i ) ; } } } } public static void Main ( String [ ] args ) { int k = 3 ; Graph g1 = new Graph ( 9 ) ; g1 . addEdge ( 0 , 1 ) ; g1 . addEdge ( 0 , 2 ) ; g1 . addEdge ( 1 , 2 ) ; g1 . addEdge ( 1 , 5 ) ; g1 . addEdge ( 2 , 3 ) ; g1 . addEdge ( 2 , 4 ) ; g1 . addEdge ( 2 , 5 ) ; g1 . addEdge ( 2 , 6 ) ; g1 . addEdge ( 3 , 4 ) ; g1 . addEdge ( 3 , 6 ) ; g1 . addEdge ( 3 , 7 ) ; g1 . addEdge ( 4 , 6 ) ; g1 . addEdge ( 4 , 7 ) ; g1 . addEdge ( 5 , 6 ) ; g1 . addEdge ( 5 , 8 ) ; g1 . addEdge ( 6 , 7 ) ; g1 . addEdge ( 6 , 8 ) ; g1 . printKCores ( k ) ; Console . WriteLine ( ) ; Graph g2 = new Graph ( 13 ) ; g2 . addEdge ( 0 , 1 ) ; g2 . addEdge ( 0 , 2 ) ; g2 . addEdge ( 0 , 3...


"Find kth smallest number in range [ 1 , n ] when all the odd numbers are deleted | C # implementation of the approach ; Function to return the kth smallest element from the range [ 1 , n ] after removing all the odd elements ; Driver code"
Solution in C#:

using System ; class GFG { static int kthSmallest ( int n , int k ) { return ( 2 * k ) ; } public static void Main ( ) { int n = 8 , k = 4 ; Console . WriteLine ( kthSmallest ( n , k ) ) ; } }


"Find larger of x ^ y and y ^ x | C # program to print greater of x ^ y and y ^ x ; Driver Code"
Solution in C#:

using System ; class GFG { static void printGreater ( int x , int y ) { double X = y * Math . Log ( x ) ; double Y = x * Math . Log ( y ) ; if ( Math . Abs ( X - Y ) < 1e-9 ) { Console . WriteLine ( " Equal " ) ; } else if ( X > Y ) { Console . WriteLine ( x + " ^ " + y ) ; } else { Console . WriteLine ( y + " ^ " + x ) ; } } public static void Main ( ) { int x = 5 , y = 8 ; printGreater ( x , y ) ; } }


"Find largest factor of N such that N / F is less than K | C # program for the above approach ; Function to find the largest factor of N which is less than or equal to K ; Initialise the variable to store the largest factor of N <= K ; Loop to find all factors of N ; Check if j is a factor of N or not ; Check if j <= K If yes , then store the larger value between ans and j in ans ; Check if N / j <= K If yes , then store the larger value between ans and j in ans ; Since max value is always stored in ans , the maximum value divisible by N less than or equal to K will be returned . ; Driver Code ; Given N and K ; Function call"
Solution in C#:

using System ; class GFG { static int solve ( int n , int k ) { int ans = 0 ; for ( int j = 1 ; j * j <= n ; j ++ ) { if ( n % j == 0 ) { if ( j <= k ) { ans = Math . Max ( ans , j ) ; } if ( n / j <= k ) { ans = Math . Max ( ans , n / j ) ; } } } return ans ; } public static void Main ( String [ ] args ) { int N = 8 , K = 7 ; Console . Write ( ( N / solve ( N , K ) ) ) ; } }


"Find largest factor of N such that N / F is less than K | C # program for the above approach ; Function to find the value of X ; Loop to check all the numbers divisible by N that yield minimum N / i value ; Print the value of packages ; Driver code ; Given N and K ; Function call"
Solution in C#:

using System ; class GFG { static void findMaxValue ( int N , int K ) { int packages ; int maxi = 1 ; for ( int i = 1 ; i <= K ; i ++ ) { if ( N % i == 0 ) maxi = Math . Max ( maxi , i ) ; } packages = N / maxi ; Console . WriteLine ( packages ) ; } public static void Main ( String [ ] args ) { int N = 8 , K = 7 ; findMaxValue ( N , K ) ; } }


"Find largest prime factor of a number | C # program to find largest prime factor of number ; function to find largest prime factor ; Initialize the maximum prime factor variable with the lowest one ; Print the number of 2 s that divide n ; equivalent to n /= 2 ; n must be odd at this point ; now we have to iterate only for integers who does not have prime factor 2 and 3 ; This condition is to handle the case when n is a prime number greater than 4 ; Driver code"
Solution in C#:

using System ; class GFG { static long maxPrimeFactors ( long n ) { long maxPrime = - 1 ; while ( n % 2 == 0 ) { maxPrime = 2 ; n >>= 1 ; } while ( n % 3 == 0 ) { maxPrime = 3 ; n = n / 3 ; } for ( int i = 5 ; i <= Math . Sqrt ( n ) ; i += 6 ) { while ( n % i == 0 ) { maxPrime = i ; n = n / i ; } while ( n % ( i + 2 ) == 0 ) { maxPrime = i + 2 ; n = n / ( i + 2 ) ; } } if ( n > 4 ) maxPrime = n ; return maxPrime ; } public static void Main ( ) { long n = 15L ; Console . WriteLine ( maxPrimeFactors ( n ) ) ; n = 25698751364526L ; Console . WriteLine ( maxPrimeFactors ( n ) ) ; } }


"Find largest subtree having identical left and right subtrees | C # program to find the largest subtree having identical left and right subtree ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and null left and right pointers . ; Sets maxSize to size of largest subtree with identical left and right . maxSize is set with size of the maximum sized subtree . It returns size of subtree rooted with current node . This size is used to keep track of maximum size . ; String to store structure of left and right subtrees ; Traverse left subtree and finds its size ; Traverse right subtree and finds its size ; If left and right subtrees are similar update maximum subtree if needed ( Note that left subtree may have a bigger value than right and vice versa ) ; Append left subtree data ; Append current node data ; Append right subtree data ; Function to find the largest subtree having identical left and right subtree ; Driver code ; Let us construct the following Tree 50 / \ 10 60 / \ / \ 5 20 70 70 / \ / \ 65 80 65 80"
Solution in C#:

using System ; class GFG { public class Node { public int data ; public Node left , right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static int maxSize ; static Node maxNode ; public class pair { public int first ; public string second ; public pair ( int a , string b ) { first = a ; second = b ; } } static pair largestSubtreeUtil ( Node root , string str ) { if ( root == null ) return new pair ( 0 , str ) ; string left = " " , right = " " ; pair ls1 = largestSubtreeUtil ( root . left , left ) ; left = ls1 . second ; int ls = ls1 . first ; pair rs1 = largestSubtreeUtil ( root . right , right ) ; right = rs1 . second ; int rs = rs1 . first ; int size = ls + rs + 1 ; if ( left . Equals ( right ) ) { if ( size > maxSize ) { maxSize = size ; maxNode = root ; } } str += " | " + left + " | " ; str += " | " + root . data + " | " ; str += " | " + right + " | " ; return new pair ( size , str ) ; } static int largestSubtree ( Node node ) { maxSize = 0 ; largestSubtreeUtil ( node , " " ) ; return maxSize ; } public static void Main ( string [ ] args ) { Node root = newNode ( 50 ) ; root . left = newNode ( 10 ) ; root . right = newNode ( 60 ) ; root . left . left = newNode ( 5 ) ; root . left . right = newNode ( 20 ) ; root . right . left = newNode ( 70 ) ; root . right . left . left = newNode ( 65 ) ; root . right . left . right = newNode ( 80 ) ; root . right . right = newNode ( 70 ) ; root . right . right . left = newNode ( 65 ) ; root . right . right . right = newNode ( 80 ) ; maxNode = null ; maxSize = largestSubtree ( root ) ; Console . Write ( " Largest ▁ Subtree ▁ is ▁ rooted ▁ at ▁ node ▁ " + maxNode . data + " and size is " } }


"Find largest word in dictionary by deleting some characters of given string | C # program to find largest word in Dictionary by deleting some characters of given String ; Returns true if str1 [ ] is a subsequence of str2 [ ] . m is length of str1 and n is length of str2 ; Traverse str2 and str1 , and compare current character of str2 with first unmatched char of str1 , if matched then move ahead in str1 ; If all characters of str1 were found in str2 ; Returns the longest String in dictionary which is a subsequence of str . ; Traverse through all words of dictionary ; If current word is subsequence of str and is largest such word so far . ; Return longest String ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool isSubSequence ( String str1 , String str2 ) { int m = str1 . Length , n = str2 . Length ; for ( int i = 0 ; i < n && j < m ; i ++ ) { if ( str1 [ j ] == str2 [ i ] ) { j ++ ; } } return ( j == m ) ; } static String findLongestString ( List < String > dict , String str ) { String result = " " ; int length = 0 ; foreach ( String word in dict ) { if ( length < word . Length && isSubSequence ( word , str ) ) { result = word ; length = word . Length ; } } return result ; } public static void Main ( String [ ] args ) { String [ ] arr = { " ale " , " apple " , " monkey " , " plea " } ; List < String > dict = new List < String > ( arr ) ; String str = " abpcplea " ; Console . WriteLine ( findLongestString ( dict , str ) ) ; } }


"Find last 2 survivors in N persons standing in a circle after killing next to immediate neighbour | C # implementation of the approach ; Node for a Linked List ; Function to find the last 2 survivors ; Total is the count of alive people ; Initiating the list of n people ; Total != 2 is terminating condition because at last only two - person will remain alive ; Del represent next person to be deleted or killed ; Last two person to survive ( in any order ) ; Driver code"
Solution in C#:

using System ; class GFG { class Node { public int val ; public Node next ; public Node ( int _val ) { val = _val ; next = null ; } } ; static void getLastTwoPerson ( int n ) { int total = n ; Node head = new Node ( 1 ) ; Node temp = head ; for ( int i = 2 ; i <= n ; i ++ ) { temp . next = new Node ( i ) ; temp = temp . next ; } temp . next = head ; temp = head ; Node del ; while ( total != 2 ) { del = temp . next . next ; temp . next . next = temp . next . next . next ; temp = temp . next ; del = null ; total -= 1 ; } Console . Write ( temp . val + " ▁ " + temp . next . val ) ; } public static void Main ( String [ ] args ) { int n = 2 ; getLastTwoPerson ( n ) ; } }


"Find last digit in factorial | C # program to find last digit in factorial n . ; Explicitly handle all numbers less than or equal to 4 ; For all numbers greater than 4 the last digit is 0 ; Driver code"
Solution in C#:

using System ; class GFG { static int lastDigitFactorial ( int n ) { if ( n == 0 ) return 1 ; else if ( n <= 2 ) return n ; else if ( n == 3 ) return 6 ; else if ( n == 4 ) return 4 ; else return 0 ; } public static void Main ( string [ ] args ) { Console . Write ( lastDigitFactorial ( 6 ) ) ; } }


"Find last five digits of a given five digit number raised to power five | C # program to find last five digits of a five digit number raised to power five ; Function to find the last five digits of a five digit number raised to power five ; Driver code"
Solution in C#:

using System ; class GFG { public static void lastFiveDigits ( int n ) { n = ( n / 10000 ) * 10000 + ( ( n / 100 ) % 10 ) * 1000 + ( n % 10 ) * 100 + ( ( n / 10 ) % 10 ) * 10 + ( n / 1000 ) % 10 ; int ans = 1 ; for ( int i = 0 ; i < 5 ; i ++ ) { ans *= n ; ans %= 100000 ; } Console . WriteLine ( ans ) ; } public static void Main ( string [ ] args ) { int n = 12345 ; lastFiveDigits ( n ) ; } }


"Find last index of a character in a string | C # code to find last index character x in given string . ; Returns last index of x if it is present . Else returns - 1. ; Traverse from right ; Driver code"
Solution in C#:

using System ; class GFG { static int findLastIndex ( string str , char x ) { for ( int i = str . Length - 1 ; i >= 0 ; i -- ) if ( str [ i ] == x ) return i ; return - 1 ; } public static void Main ( ) { string str = " geeksforgeeks " ; char x = ' e ' ; int index = findLastIndex ( str , x ) ; if ( index == - 1 ) Console . WriteLine ( " Character ▁ not ▁ found " ) ; else Console . WriteLine ( " Last ▁ index ▁ is ▁ " + index ) ; } }


"Find last index of a character in a string | C # program to find last index of character x in given string . ; Returns last index of x if it is present Else returns - 1. ; Driver code ; String in which char is to be found ; char whose index is to be found"
Solution in C#:

using System ; class GFG { static int findLastIndex ( string str , char x ) { int index = - 1 ; for ( int i = 0 ; i < str . Length ; i ++ ) if ( str [ i ] == x ) index = i ; return index ; } public static void Main ( ) { string str = " geeksforgeeks " ; char x = ' e ' ; int index = findLastIndex ( str , x ) ; if ( index == - 1 ) Console . WriteLine ( " Character ▁ not ▁ found " ) ; else Console . WriteLine ( " Last ▁ index ▁ is ▁ " + index ) ; } }


"Find last two digits of sum of N factorials | C # program to find the unit place digit of the first N natural numbers factorials ; Function to find the unit ' s ▁ and ▁ ten ' s place digit ; Let us write for cases when N is smaller than or equal to 10. ; We know following ( 1 ! + 2 ! + 3 ! + 4 ! ... + 10 ! ) % 100 = 13 else ( N >= 10 ) ; Driver code"
Solution in C#:

using System ; class GFG { static int get_last_two_digit ( long N ) { if ( N <= 10 ) { long ans = 0 , fac = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { fac = fac * i ; ans += fac ; } return ( int ) ans % 100 ; } return 13 ; } public static void Main ( ) { long N = 1 ; for ( N = 1 ; N <= 10 ; N ++ ) Console . WriteLine ( " For ▁ N ▁ = ▁ " + N + " ▁ : ▁ " + get_last_two_digit ( N ) ) ; } }


"Find least non | C # program to find the least non - overlapping number from a given set intervals ; Function to find the smallest non - overlapping number ; Create a visited array ; Find the first missing value ; Driver code"
Solution in C#:

 using System ; class GFG { static int MAX = ( int ) ( 1e5 + 5 ) ; class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void find_missing ( pair [ ] interval ) { int [ ] vis = new int [ MAX ] ; for ( int i = 0 ; i < interval . Length ; ++ i ) { int start = interval [ i ] . first ; int end = interval [ i ] . second ; vis [ start ] ++ ; vis [ end + 1 ] -- ; } for ( int i = 1 ; i < MAX ; i ++ ) { vis [ i ] += vis [ i - 1 ] ; if ( vis [ i ] == 0 ) { Console . Write ( i + "
 " ) ; return ; } } } public static void Main ( String [ ] args ) { pair [ ] interval = { new pair ( 0 , 14 ) , new pair ( 86 , 108 ) , new pair ( 22 , 30 ) , new pair ( 5 , 17 ) } ; find_missing ( interval ) ; } }


"Find least non | C # program to find the least non - overlapping number from a given set intervals ; Function to find the smallest non - overlapping number ; Sort the intervals based on their starting value ; Check if any missing value exist ; Finally print the missing value ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { class Pair : IComparable < Pair > { public int start , end ; public Pair ( int s , int e ) { start = s ; end = e ; } public int CompareTo ( Pair p ) { return this . start - p . start ; } } static void findMissing ( List < Pair > interval ) { interval . Sort ( ) ; int mx = 0 ; for ( int i = 0 ; i < interval . Count ; ++ i ) { if ( interval [ i ] . start > mx ) { Console . WriteLine ( mx ) ; return ; } else mx = Math . Max ( mx , interval [ i ] . end + 1 ) ; } Console . WriteLine ( mx ) ; } public static void Main ( String [ ] args ) { List < Pair > interval = new List < Pair > ( ) ; interval . Add ( new Pair ( 0 , 14 ) ) ; interval . Add ( new Pair ( 86 , 108 ) ) ; interval . Add ( new Pair ( 22 , 30 ) ) ; interval . Add ( new Pair ( 5 , 17 ) ) ; findMissing ( interval ) ; } }


"Find length of Diagonal of Hexagon | C # Program to find the diagonal of the hexagon ; Function to find the diagonal of the hexagon ; side cannot be negative ; diagonal of the hexagon ; Driver code"
Solution in C#:

using System ; class GFG { static float hexadiagonal ( float a ) { if ( a < 0 ) return - 1 ; return 2 * a ; } public static void Main ( ) { float a = 4 ; Console . WriteLine ( hexadiagonal ( a ) ) ; } }


"Find length of longest subsequence of one string which is substring of another string | Base Case ; if the last char of both Strings are equal ; if the last char of both Strings are not equal ; Driver code ; as minimum length can be 0 only . ; traversing for every length of Y ."
Solution in C#:

using System ; public class GFG { static int maxSubsequenceSubString ( String X , String Y , int n , int m ) { if ( n == 0 m == 0 ) return 0 ; if ( X [ n - 1 ] == Y [ m - 1 ] ) { return 1 + maxSubsequenceSubString ( X , Y , n - 1 , m - 1 ) ; } else { return maxSubsequenceSubString ( X , Y , n - 1 , m ) ; } } public static void Main ( String [ ] args ) { String X = " abcd " ; String Y = " bacdbdcd " ; int n = X . Length , m = Y . Length ; int maximum_length = 0 ; for ( int i = 0 ; i <= m ; i ++ ) { int temp_ans = maxSubsequenceSubString ( X , Y , n , i ) ; if ( temp_ans > maximum_length ) maximum_length = temp_ans ; } Console . Write ( " Length ▁ for ▁ maximum ▁ possible ▁ Subsequence ▁ of ▁ String ▁ X ▁ which ▁ is ▁ SubString ▁ of ▁ Y - > " + maximum_length ) ; } }


"Find length of longest subsequence of one string which is substring of another string | C # program to find maximum length of subsequence of a string X such it is substring in another string Y . ; Return the maximum size of substring of X which is substring in Y . ; Initialize the dp [ ] [ ] to 0. ; Calculating value for each element . ; If alphabet of string X and Y are equal make dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; Else copy the previous value in the row i . e dp [ i - 1 ] [ j - 1 ] ; Finding the maximum length . ; Driver Method"
Solution in C#:

using System ; public class GFG { static int MAX = 1000 ; static int maxSubsequenceSubstring ( string x , string y , int n , int m ) { int [ , ] dp = new int [ MAX , MAX ] ; for ( int i = 0 ; i <= m ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) dp [ i , j ] = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( x [ j - 1 ] == y [ i - 1 ] ) dp [ i , j ] = 1 + dp [ i - 1 , j - 1 ] ; else dp [ i , j ] = dp [ i , j - 1 ] ; } } int ans = 0 ; for ( int i = 1 ; i <= m ; i ++ ) ans = Math . Max ( ans , dp [ i , n ] ) ; return ans ; } public static void Main ( ) { string x = " ABCD " ; string y = " BACDBDCD " ; int n = x . Length , m = y . Length ; Console . WriteLine ( maxSubsequenceSubstring ( x , y , n , m ) ) ; } }


"Find length of longest substring with at most K normal characters | C # implementation to Find length of longest subString with at most K normal characters ; Function to find maximum length of normal subStrings ; keeps count of normal characters ; indexes of subString ; maintain length of longest subString with at most K normal characters ; get position of character ; check if current character is normal ; check if normal characters count exceeds K ; update answer with subString length ; get position of character ; check if character is normal then decrement count ; Driver code ; initialise the String"
Solution in C#:

using System ; public class GFG { static int maxNormalSubString ( char [ ] P , char [ ] Q , int K , int N ) { if ( K == 0 ) return 0 ; int count = 0 ; int left = 0 , right = 0 ; int ans = 0 ; while ( right < N ) { while ( right < N && count <= K ) { int pos = P [ right ] - ' a ' ; if ( Q [ pos ] == '0' ) { if ( count + 1 > K ) break ; else count ++ ; } right ++ ; if ( count <= K ) ans = Math . Max ( ans , right - left ) ; } while ( left < right ) { int pos = P [ left ] - ' a ' ; left ++ ; if ( Q [ pos ] == '0' ) count -- ; if ( count < K ) break ; } } return ans ; } public static void Main ( String [ ] args ) { String P = " giraffe " , Q = "01111001111111111011111111" ; int K = 2 ; int N = P . Length ; Console . Write ( maxNormalSubString ( P . ToCharArray ( ) , Q . ToCharArray ( ) , K , N ) ) ; } }


"Find length of period in decimal value of 1 / n | C # program to find length of period of 1 / n without using map or hash ; Function to find length of period in 1 / n ; Find the ( n + 1 ) th remainder after decimal point in value of 1 / n ; Store ( n + 1 ) th remainder ; Count the number of remainders before next occurrence of ( n + 1 ) ' th ▁ remainder ▁ ' d ' ; Driver code"
Solution in C#:

 using System ; class GFG { static int getPeriod ( int n ) { int rem = 1 ; for ( int i = 1 ; i <= n + 1 ; i ++ ) rem = ( 10 * rem ) % n ; int d = rem ; int count = 0 ; do { rem = ( 10 * rem ) % n ; count ++ ; } while ( rem != d ) ; return count ; } public static void Main ( ) { Console . Write ( getPeriod ( 3 ) + "
 " ) ; Console . Write ( getPeriod ( 7 ) ) ; } }


"Find length of the largest region in Boolean Matrix | C # program to find the length of the largest region in boolean 2D - matrix ; A function to check if a given cell ( row , col ) can be included in DFS ; row number is in range , column number is in range and value is 1 and not yet visited ; A utility function to do DFS for a 2D boolean matrix . It only considers the 8 neighbours as adjacent vertices ; These arrays are used to get row and column numbers of 8 neighbours of a given cell ; Mark this cell as visited ; Recur for all connected neighbours ; increment region length by one ; The main function that returns largest length region of a given boolean 2D matrix ; Make a boolean array to mark visited cells . Initially all cells are unvisited ; Initialize result as 0 and traverse through the all cells of given matrix ; If a cell with value 1 is not ; visited yet , then new region found ; maximum region ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int ROW , COL , count ; static Boolean isSafe ( int [ , ] M , int row , int col , Boolean [ , ] visited ) { return ( ( row >= 0 ) && ( row < ROW ) && ( col >= 0 ) && ( col < COL ) && ( M [ row , col ] == 1 && ! visited [ row , col ] ) ) ; } static void DFS ( int [ , ] M , int row , int col , Boolean [ , ] visited ) { int [ ] rowNbr = { - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 } ; int [ ] colNbr = { - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 } ; visited [ row , col ] = true ; for ( int k = 0 ; k < 8 ; k ++ ) { if ( isSafe ( M , row + rowNbr [ k ] , col + colNbr [ k ] , visited ) ) { count ++ ; DFS ( M , row + rowNbr [ k ] , col + colNbr [ k ] , visited ) ; } } } static int largestRegion ( int [ , ] M ) { Boolean [ , ] visited = new Boolean [ ROW , COL ] ; int result = 0 ; for ( int i = 0 ; i < ROW ; i ++ ) { for ( int j = 0 ; j < COL ; j ++ ) { if ( M [ i , j ] == 1 && ! visited [ i , j ] ) { count = 1 ; DFS ( M , i , j , visited ) ; result = Math . Max ( result , count ) ; } } } return result ; } public static void Main ( String [ ] args ) { int [ , ] M = { { 0 , 0 , 1 , 1 , 0 } , { 1 , 0 , 1 , 1 , 0 } , { 0 , 1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 1 } } ; ROW = 4 ; COL = 5 ; Console . WriteLine ( largestRegion ( M ) ) ; } }


"Find length of the longest consecutive path from a given starting character | C # program to find the longest consecutive path ; tool matrices to recur for adjacent cells . ; dp [ i ] [ j ] Stores length of longest consecutive path starting at arr [ i ] [ j ] . ; check whether mat [ i ] [ j ] is a valid cell or not . ; Check whether current character is adjacent to previous character ( character processed in parent call ) or not . ; i , j are the indices of the current cell and prev is the character processed in the parent call . . also mat [ i ] [ j ] is our current character . ; If this cell is not valid or current character is not adjacent to previous one ( e . g . d is not adjacent to b ) or if this cell is already included in the path than return 0. ; If this subproblem is already solved , return the answer ; Initialize answer ; recur for paths with different adjacent cells and store the length of longest path . ; save the answer and return ; Returns length of the longest path with all characters consecutive to each other . This function first initializes dp array that is used to store results of subproblems , then it calls recursive DFS based function getLenUtil ( ) to find max length path ; check for each possible starting point ; recur for all eight adjacent cells ; Driver code `"
Solution in C#:

using System ; class GFG { static int [ ] x = { 0 , 1 , 1 , - 1 , 1 , 0 , - 1 , - 1 } ; static int [ ] y = { 1 , 0 , 1 , 1 , - 1 , - 1 , 0 , - 1 } ; static int R = 3 ; static int C = 3 ; static int [ , ] dp = new int [ R , C ] ; static bool isvalid ( int i , int j ) { if ( i < 0 j < 0 i >= R j >= C ) return false ; return true ; } static bool isadjacent ( char prev , char curr ) { return ( ( curr - prev ) == 1 ) ; } static int getLenUtil ( char [ , ] mat , int i , int j , char prev ) { if ( ! isvalid ( i , j ) || ! isadjacent ( prev , mat [ i , j ] ) ) return 0 ; if ( dp [ i , j ] != - 1 ) return dp [ i , j ] ; int ans = 0 ; for ( int k = 0 ; k < 8 ; k ++ ) ans = Math . Max ( ans , 1 + getLenUtil ( mat , i + x [ k ] , j + y [ k ] , mat [ i , j ] ) ) ; return dp [ i , j ] = ans ; } static int getLen ( char [ , ] mat , char s ) { for ( int i = 0 ; i < R ; ++ i ) for ( int j = 0 ; j < C ; ++ j ) dp [ i , j ] = - 1 ; int ans = 0 ; for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { if ( mat [ i , j ] == s ) { for ( int k = 0 ; k < 8 ; k ++ ) ans = Math . Max ( ans , 1 + getLenUtil ( mat , i + x [ k ] , j + y [ k ] , s ) ) ; } } } return ans ; } public static void Main ( ) { char [ , ] mat = { { ' a ' , ' c ' , ' d ' } , { ' h ' , ' b ' , ' a ' } , { ' i ' , ' g ' , ' f ' } } ; Console . WriteLine ( getLen ( mat , ' a ' ) ) ; Console . WriteLine ( getLen ( mat , ' e ' ) ) ; Console . WriteLine ( getLen ( mat , ' b ' ) ) ; Console . WriteLine ( getLen ( mat , ' f ' ) ) ; } }


"Find letter 's position in Alphabet using Bit operation | C # implementation of the approach ; Function to calculate the position of characters ; Performing AND operation with number 31 ; Driver code"
Solution in C#:

using System ; class GFG { public static int NUM = 31 ; static void positions ( string str , int n ) { for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( ( str [ i ] & NUM ) + " ▁ " ) ; } } public static void Main ( ) { string str = " Geeks " ; int n = str . Length ; positions ( str , n ) ; } }


"Find lexicographically smallest string in at most one swaps | C # implementation of the above approach ; Function to return the lexicographically smallest String that can be formed by swapping at most one character . The characters might not necessarily be adjacent . ; Store last occurrence of every character ; Set - 1 as default for every character . ; char index to fill in the last occurrence array ; If this is true then this character is being visited for the first time from the last Thus last occurrence of this character is stored in this index ; char to replace ; Find the last occurrence of this character . ; Swap this with the last occurrence ; Driver code"
Solution in C#:

using System ; class GFG { static String findSmallest ( char [ ] s ) { int len = s . Length ; int [ ] loccur = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) loccur [ i ] = - 1 ; for ( int i = len - 1 ; i >= 0 ; -- i ) { int chI = s [ i ] - ' a ' ; if ( loccur [ chI ] == - 1 ) { loccur [ chI ] = i ; } } char [ ] sorted_s = s ; Array . Sort ( sorted_s ) ; for ( int i = 0 ; i < len ; ++ i ) { if ( s [ i ] != sorted_s [ i ] ) { int chI = sorted_s [ i ] - ' a ' ; int last_occ = loccur [ chI ] ; char temp = s [ last_occ ] ; s [ last_occ ] = s [ i ] ; s [ i ] = temp ; break ; } } return String . Join ( " " , s ) ; } public static void Main ( String [ ] args ) { String s = " geeks " ; Console . Write ( findSmallest ( s . ToCharArray ( ) ) ) ; } }


"Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays | C # to find largest bitonic sequence ; Utility Binary search ; Reverse array ; Function to find LIS in reverse form ; Add boundary case , when array n is zero Depend on smart pointers ; It will always point to empty location ; New smallest value ; arr [ i ] wants to extend largest subsequence ; arr [ i ] wants to be a potential candidate of future subsequence It will replace ceil value in tailIndices ; Put LIS into vector ; Function for finding longest bitonic seq ; Find LIS of array 1 in reverse form ; Reverse res to get LIS of first array ; Reverse array2 and find its LIS ; Print result ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < int > res = new List < int > ( ) ; static int GetCeilIndex ( int [ ] arr , int [ ] T , int l , int r , int key ) { while ( r - l > 1 ) { int m = l + ( r - l ) / 2 ; if ( arr [ T [ m ] ] >= key ) r = m ; else l = m ; } return r ; } static int [ ] reverse ( int [ ] a ) { int i , n = a . Length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } static void LIS ( int [ ] arr , int n ) { int [ ] tailIndices = new int [ n ] ; int [ ] prevIndices = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { tailIndices [ i ] = 0 ; prevIndices [ i ] = - 1 ; } int len = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ tailIndices [ 0 ] ] ) tailIndices [ 0 ] = i ; else if ( arr [ i ] > arr [ tailIndices [ len - 1 ] ] ) { prevIndices [ i ] = tailIndices [ len - 1 ] ; tailIndices [ len ++ ] = i ; } else { int pos = GetCeilIndex ( arr , tailIndices , - 1 , len - 1 , arr [ i ] ) ; prevIndices [ i ] = tailIndices [ pos - 1 ] ; tailIndices [ pos ] = i ; } } for ( int i = tailIndices [ len - 1 ] ; i >= 0 ; i = prevIndices [ i ] ) res . Add ( arr [ i ] ) ; } static void longestBitonic ( int [ ] arr1 , int n1 , int [ ] arr2 , int n2 ) { LIS ( arr1 , n1 ) ; res . Reverse ( ) ; arr2 = reverse ( arr2 ) ; LIS ( arr2 , n2 ) ; for ( int i = 0 ; i < res . Count ; i ++ ) Console . Write ( res [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr1 = { 1 , 2 , 4 , 3 , 2 } ; int [ ] arr2 = { 8 , 6 , 4 , 7 , 8 , 9 } ; int n1 = arr1 . Length ; int n2 = arr2 . Length ; longestBitonic ( arr1 , n1 , arr2 , n2 ) ; } }


"Find lost element from a duplicated array | C # program to find missing element from one array such that it has all elements of other array except one . Elements in two arrays can be in any order . ; This function mainly does XOR of all elements of arr1 [ ] and arr2 [ ] ; Do XOR of all element ; Driver Code"
Solution in C#:

using System ; class GFG { static void findMissing ( int [ ] arr1 , int [ ] arr2 , int M , int N ) { if ( M != N - 1 && N != M - 1 ) { Console . WriteLine ( " Invalid ▁ Input " ) ; return ; } int res = 0 ; for ( int i = 0 ; i < M ; i ++ ) res = res ^ arr1 [ i ] ; for ( int i = 0 ; i < N ; i ++ ) res = res ^ arr2 [ i ] ; Console . WriteLine ( " Missing ▁ element ▁ is ▁ " + res ) ; } public static void Main ( ) { int [ ] arr1 = { 4 , 1 , 5 , 9 , 7 } ; int [ ] arr2 = { 7 , 5 , 9 , 4 } ; int M = arr1 . Length ; int N = arr2 . Length ; findMissing ( arr1 , arr2 , M , N ) ; } }


"Find lost element from a duplicated array | C # program to find missing element from same arrays ( except one missing element ) ; Function to find missing element based on binary search approach . arr1 [ ] is of larger size and N is size of it . arr1 [ ] and arr2 [ ] are assumed to be in same order . ; special case , for only element which is missing in second array ; special case , for first element missing ; Initialize current corner points ; loop until lo < hi ; If element at mid indices are equal then go to right subarray ; if lo , hi becomes contiguous , break ; missing element will be at hi index of bigger array ; This function mainly does basic error checking and calls findMissingUtil ; Driver Code"
Solution in C#:

 using System ; class GFG { static int findMissingUtil ( int [ ] arr1 , int [ ] arr2 , int N ) { if ( N == 1 ) return arr1 [ 0 ] ; if ( arr1 [ 0 ] != arr2 [ 0 ] ) return arr1 [ 0 ] ; int lo = 0 , hi = N - 1 ; while ( lo < hi ) { int mid = ( lo + hi ) / 2 ; if ( arr1 [ mid ] == arr2 [ mid ] ) lo = mid ; else hi = mid ; if ( lo == hi - 1 ) break ; } return arr1 [ hi ] ; } static void findMissing ( int [ ] arr1 , int [ ] arr2 , int M , int N ) { if ( N == M - 1 ) Console . WriteLine ( " Missing ▁ Element ▁ is ▁ " + findMissingUtil ( arr1 , arr2 , M ) + "
 " ) ; else if ( M == N - 1 ) Console . WriteLine ( " Missing ▁ Element ▁ is ▁ " + findMissingUtil ( arr2 , arr1 , N ) + "
 " ) ; else Console . WriteLine ( " Invalid ▁ Input " ) ; } public static void Main ( ) { int [ ] arr1 = { 1 , 4 , 5 , 7 , 9 } ; int [ ] arr2 = { 4 , 5 , 7 , 9 } ; int M = arr1 . Length ; int N = arr2 . Length ; findMissing ( arr1 , arr2 , M , N ) ; } }


"Find m | C # program to find m - th summation . ; Function to return mth summation ; base case ; Driver Code"
Solution in C#:

using System ; class GFG { static int SUM ( int n , int m ) { if ( m == 1 ) return ( n * ( n + 1 ) / 2 ) ; int sum = SUM ( n , m - 1 ) ; return ( sum * ( sum + 1 ) / 2 ) ; } public static void Main ( ) { int n = 5 ; int m = 3 ; Console . Write ( " SUM ( " + n + " , ▁ " + m + " ) : ▁ " + SUM ( n , m ) ) ; } }


"Find maximum ( or minimum ) in Binary Tree | C # code to Find maximum ( or minimum ) in Binary Tree ; A binary tree node ; Constructor that allocates a new node with the given data and NULL left and right pointers . ; Returns the max value in a binary tree ; Base case ; Return maximum of 3 values : 1 ) Root 's data 2) Max in Left Subtree 3) Max in right subtree ; Driver code ; Function call"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } public class BinaryTree { public Node root ; public static int findMax ( Node node ) { if ( node == null ) { return int . MinValue ; } int res = node . data ; int lres = findMax ( node . left ) ; int rres = findMax ( node . right ) ; if ( lres > res ) { res = lres ; } if ( rres > res ) { res = rres ; } return res ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 2 ) ; tree . root . left = new Node ( 7 ) ; tree . root . right = new Node ( 5 ) ; tree . root . left . right = new Node ( 6 ) ; tree . root . left . right . left = new Node ( 1 ) ; tree . root . left . right . right = new Node ( 11 ) ; tree . root . right . right = new Node ( 9 ) ; tree . root . right . right . left = new Node ( 4 ) ; Console . WriteLine ( " Maximum ▁ element ▁ is ▁ " + BinaryTree . findMax ( tree . root ) ) ; } }


"Find maximum ( or minimum ) in Binary Tree | Returns the min value in a binary tree"
Solution in C#:

public static int findMin ( Node node ) { if ( node == null ) return int . MaxValue ; int res = node . data ; int lres = findMin ( node . left ) ; int rres = findMin ( node . right ) ; if ( lres < res ) res = lres ; if ( rres < res ) res = rres ; return res ; }


"Find maximum GCD value from root to leaf in a Binary tree | C # program for the above approach ; Initialise to update the maximum gcd value from all the path ; Node structure ; Left & right child of the node ; Initialize constructor ; Function to find gcd of a and b ; Function to find the gcd of a path ; Function to find the maximum value of gcd from root to leaf in a Binary tree ; Check if root is not null ; Find the maximum gcd of path value and store in global maxm variable ; Traverse left of binary tree ; Traverse right of the binary tree ; Driver Code ; Given Tree ; Function call ; Print the maximum AND value"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static int maxm = 0 ; class Node { public int val ; public Node left , right ; public Node ( int x ) { val = x ; left = null ; right = null ; } } ; static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int find_gcd ( List < int > arr ) { if ( arr . Count == 1 ) return arr [ 0 ] ; int g = arr [ 0 ] ; for ( int i = 1 ; i < arr . Count ; i ++ ) { g = gcd ( g , arr [ 1 ] ) ; } return g ; } static void maxm_gcd ( Node root , List < int > ans ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) { ans . Add ( root . val ) ; maxm = Math . Max ( find_gcd ( ans ) , maxm ) ; return ; } ans . Add ( root . val ) ; maxm_gcd ( root . left , ans ) ; maxm_gcd ( root . right , ans ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( 15 ) ; root . left = new Node ( 3 ) ; root . right = new Node ( 7 ) ; root . left . left = new Node ( 15 ) ; root . left . right = new Node ( 1 ) ; root . right . left = new Node ( 31 ) ; root . right . right = new Node ( 9 ) ; maxm_gcd ( root , new List < int > ( ) ) ; Console . Write ( maxm + "
 " ) ; } }


"Find maximum N such that the sum of square of first N natural numbers is not more than X | C # implementation of the approach ; Function to return the sum of the squares of first N natural numbers ; Function to return the maximum N such that the sum of the squares of first N natural numbers is not more than X ; Driver code"
Solution in C#:

using System ; class GFG { static long squareSum ( long N ) { long sum = ( long ) ( N * ( N + 1 ) * ( 2 * N + 1 ) ) / 6 ; return sum ; } static long findMaxN ( long X ) { long low = 1 , high = 100000 ; int N = 0 ; while ( low <= high ) { long mid = ( high + low ) / 2 ; if ( squareSum ( mid ) <= X ) { N = ( int ) mid ; low = mid + 1 ; } else high = mid - 1 ; } return N ; } static public void Main ( ) { long X = 25 ; Console . WriteLine ( findMaxN ( X ) ) ; } }


"Find maximum N such that the sum of square of first N natural numbers is not more than X | C # implementation of the approach ; Function to return the sum of the squares of first N natural numbers ; Function to return the maximum N such that the sum of the squares of first N natural numbers is not more than X ; Iterate through the maxvalue of N ; If the condition fails then return the i - 1 i . e sum of squares till i - 1 ; Driver code"
Solution in C#:

using System ; class GFG { static long squareSum ( long N ) { long sum = ( long ) ( N * ( N + 1 ) * ( 2 * N + 1 ) ) / 6 ; return sum ; } static long findMaxN ( long X ) { long N = ( long ) Math . Sqrt ( X ) ; for ( long i = 1 ; i <= N ; i ++ ) { if ( squareSum ( i ) > X ) return i - 1 ; } return - 1 ; } public static void Main ( string [ ] args ) { long X = 25 ; Console . WriteLine ( findMaxN ( X ) ) ; } }


"Find maximum among x ^ ( y ^ 2 ) or y ^ ( x ^ 2 ) where x and y are given | C # program to find the greater value ; Function to find maximum ; Case 1 ; Case 2 ; Driver Code"
Solution in C#:

using System ; class GFG { static bool findGreater ( int x , int y ) { if ( x > y ) { return false ; } else { return true ; } } public static void Main ( ) { int x = 4 ; int y = 9 ; if ( findGreater ( x , y ) ) Console . WriteLine ( "1" ) ; else Console . WriteLine ( "2" ) ; } }


"Find maximum and minimum distance between magnets | C # program for max and min distance ; Function for finding distance between pivots ; Function for minimum distance ; Function for maximum distance ; Driver Code"
Solution in C#:

using System ; class GFG { static int pivotDis ( int x0 , int y0 , int x1 , int y1 ) { return ( int ) Math . Sqrt ( ( x1 - x0 ) * ( x1 - x0 ) + ( y1 - y0 ) * ( y1 - y0 ) ) ; } static int minDis ( int D , int r1 , int r2 ) { return Math . Max ( ( D - r1 - r2 ) , 0 ) ; } static int maxDis ( int D , int r1 , int r2 ) { return D + r1 + r2 ; } public static void Main ( ) { int x0 = 0 , y0 = 0 , x1 = 8 , y1 = 0 , r1 = 4 , r2 = 5 ; int D = pivotDis ( x0 , y0 , x1 , y1 ) ; Console . WriteLine ( " Distance ▁ while ▁ " + " repulsion ▁ = ▁ " + maxDis ( D , r1 , r2 ) ) ; Console . WriteLine ( " Distance ▁ while ▁ " + " attraction ▁ = ▁ " + minDis ( D , r1 , r2 ) ) ; } }


"Find maximum distance between any city and station | C # program to calculate the maximum distance between any city and its nearest station ; Function to calculate the maximum distance between any city and its nearest station ; Initialize boolean list ; Assign True to cities containing station ; Driver code"
Solution in C#:

using System ; class GFG { static int findMaxDistance ( int numOfCities , int [ ] station , int n ) { bool [ ] hasStation = new bool [ numOfCities + 1 ] ; for ( int city = 0 ; city < n ; city ++ ) { hasStation [ station [ city ] ] = true ; } int dist = 0 ; int maxDist = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { maxDist = Math . Min ( station [ i ] , maxDist ) ; } for ( int city = 0 ; city < numOfCities ; city ++ ) { if ( hasStation [ city ] == true ) { maxDist = Math . Max ( ( dist + 1 ) / 2 , maxDist ) ; dist = 0 ; } else dist += 1 ; } return Math . Max ( maxDist , dist ) ; } public static void Main ( String [ ] args ) { int numOfCities = 6 ; int [ ] station = { 3 , 1 } ; int n = station . Length ; Console . WriteLine ( " Max ▁ Distance : " + findMaxDistance ( numOfCities , station , n ) ) ; } }


"Find maximum height pyramid from the given array of objects | C # program to find maximum height pyramid from the given object width . ; Returns maximum number of pyramidcal levels n boxes of given widths . ; Sort objects in increasing order of widths ; Total width of previous level and total number of objects in previous level ; Number of object in current level . ; Width of current level . ; Picking the object . So increase current width and number of object . ; If current width and number of object are greater than previous . ; Update previous width , number of object on previous level . ; Reset width of current level , number of object on current level . ; Increment number of level . ; Driver Program"
Solution in C#:

using System ; public class GFG { static int maxLevel ( int [ ] boxes , int n ) { Array . Sort ( boxes ) ; int prev_width = boxes [ 0 ] ; int prev_count = 1 ; int curr_count = 0 ; int curr_width = 0 ; for ( int i = 1 ; i < n ; i ++ ) { curr_width += boxes [ i ] ; curr_count += 1 ; if ( curr_width > prev_width && curr_count > prev_count ) { prev_width = curr_width ; prev_count = curr_count ; curr_count = 0 ; curr_width = 0 ; ans ++ ; } } return ans ; } static public void Main ( ) { int [ ] boxes = { 10 , 20 , 30 , 50 , 60 , 70 } ; int n = boxes . Length ; Console . WriteLine ( maxLevel ( boxes , n ) ) ; } }


"Find maximum height to cut all chocolates horizontally such that at least K amount remains | C # program for the above approach ; Function to find the sum of remaining chocolate after making the horizontal cut at height mid ; Stores the sum of chocolates ; Traverse the array arr [ ] ; If the height is at least mid ; Return the possible sum ; Function to find the maximum horizontal cut made to all the chocolates such that the sum of the remaining element is at least K ; Ranges of Binary Search ; Perform the Binary Search ; Find the sum of removed after making cut at height mid ; If the chocolate removed is same as the chocolate needed then return the height ; If the chocolate removed is less than chocolate needed then shift to the left range ; Otherwise , shift to the right range ; Return the possible cut ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int cal ( List < int > arr , int mid ) { int chocolate = 0 ; foreach ( int i in arr ) { if ( i >= mid ) chocolate += i - mid ; } return chocolate ; } static int maximumCut ( List < int > arr , int K ) { int low = 0 ; int high = arr . Max ( ) ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int chocolate = cal ( arr , mid ) ; if ( chocolate == K ) return mid ; else if ( chocolate < K ) high = mid - 1 ; else { low = mid + 1 ; if ( mid > high ) high = mid ; } } return high ; } public static void Main ( ) { int K = 7 ; List < int > arr = new List < int > ( ) { 15 , 20 , 8 , 17 } ; Console . Write ( maximumCut ( arr , K ) ) ; } }


"Find maximum number of edge disjoint paths between two vertices | C # program to find maximum number of edge disjoint paths ; Number of vertices in given graph ; Returns true if there is a path from source ' s ' to sink ' t ' in residual graph . Also fills parent [ ] to store the path ; Create a visited array and mark all vertices as not visited ; Create a queue , enqueue source vertex and mark source vertex as visited ; Standard BFS Loop ; If we reached sink in BFS starting from source , then return true , else false ; Returns tne maximum number of edge - disjoint paths from s to t . This function is copy of goo . gl / wtQ4Ks forFulkerson ( ) discussed at http : ; Create a residual graph and fill the residual graph with given capacities in the original graph as residual capacities in residual graph Residual graph where rGraph [ i , j ] indicates residual capacity of edge from i to j ( if there is an edge . If rGraph [ i , j ] is 0 , then there is not ) ; This array is filled by BFS and to store path ; There is no flow initially ; Augment the flow while there is path from source to sink ; Find minimum residual capacity of the edges along the path filled by BFS . Or we can say find the maximum flow through the path found . ; update residual capacities of the edges and reverse edges along the path ; Add path flow to overall flow ; Return the overall flow ( max_flow is equal to maximum number of edge - disjoint paths ) ; Driver Code ; Let us create a graph shown in the above example"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int V = 8 ; static bool bfs ( int [ , ] rGraph , int s , int t , int [ ] parent ) { bool [ ] visited = new bool [ V ] ; Queue < int > q = new Queue < int > ( ) ; q . Enqueue ( s ) ; visited [ s ] = true ; parent [ s ] = - 1 ; while ( q . Count != 0 ) { int u = q . Peek ( ) ; q . Dequeue ( ) ; for ( int v = 0 ; v < V ; v ++ ) { if ( visited [ v ] == false && rGraph [ u , v ] > 0 ) { q . Enqueue ( v ) ; parent [ v ] = u ; visited [ v ] = true ; } } } return ( visited [ t ] == true ) ; } static int findDisjointPaths ( int [ , ] graph , int s , int t ) { int u , v ; int [ , ] rGraph = new int [ V , V ] ; for ( u = 0 ; u < V ; u ++ ) for ( v = 0 ; v < V ; v ++ ) rGraph [ u , v ] = graph [ u , v ] ; int [ ] parent = new int [ V ] ; int max_flow = 0 ; while ( bfs ( rGraph , s , t , parent ) ) { int path_flow = int . MaxValue ; for ( v = t ; v != s ; v = parent [ v ] ) { u = parent [ v ] ; path_flow = Math . Min ( path_flow , rGraph [ u , v ] ) ; } for ( v = t ; v != s ; v = parent [ v ] ) { u = parent [ v ] ; rGraph [ u , v ] -= path_flow ; rGraph [ v , u ] += path_flow ; } max_flow += path_flow ; } return max_flow ; } public static void Main ( String [ ] args ) { int [ , ] graph = { { 0 , 1 , 1 , 1 , 0 , 0 , 0 , 0 } , { 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 1 , 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 } , { 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 } , { 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 } , { 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } } ; int s = 0 ; int t = 7 ; Console . WriteLine ( " There ▁ can ▁ be ▁ maximum ▁ " + findDisjointPaths ( graph , s , t ) + " ▁ edge - disjoint ▁ paths ▁ from ▁ " + s + " ▁ to ▁ " + t ) ; } }


"Find maximum number that can be formed using digits of a given number | C # program to print the maximum number from the set of digits of a given number ; Function to print the maximum number ; hashed array to store count of digits ; Converting given number to string ; Updating the count array ; result is to store the final number ; Traversing the count array to calculate the maximum number ; return the result ; Driver Code"
Solution in C#:

using System ; class GFG { static int printMaxNum ( int num ) { int [ ] count = new int [ 10 ] ; String str = num . ToString ( ) ; for ( int i = 0 ; i < str . Length ; i ++ ) count [ str [ i ] - '0' ] ++ ; int result = 0 , multiplier = 1 ; for ( int i = 0 ; i <= 9 ; i ++ ) { while ( count [ i ] > 0 ) { result = result + ( i * multiplier ) ; count [ i ] -- ; multiplier = multiplier * 10 ; } } return result ; } public static void Main ( ) { int num = 38293367 ; Console . Write ( printMaxNum ( num ) ) ; } }


"Find maximum of minimum for every window size in a given array | An efficient C # program to find maximum of all minimums of windows of different size ; Used to find previous and next smaller ; Arrays to store previous and next smaller ; Initialize elements of left [ ] and right [ ] ; Fill elements of left [ ] using logic discussed on www . geeksforgeeks . org / next - greater - element / https : ; Empty the stack as stack is going to be used for right [ ] ; Fill elements of right [ ] using same logic ; Create and initialize answer array ; Fill answer array by comparing minimums of all lengths computed using left [ ] and right [ ] ; length of the interval ; arr [ i ] is a possible answer for this length ' len ' interval , check x if arr [ i ] is more than max for ' len ' ; Some entries in ans [ ] may not be filled yet . Fill them by taking values from right side of ans [ ] ; Print the result ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int [ ] arr = new int [ ] { 10 , 20 , 30 , 50 , 10 , 70 , 30 } ; public static void printMaxOfMin ( int n ) { Stack < int > s = new Stack < int > ( ) ; int [ ] left = new int [ n + 1 ] ; int [ ] right = new int [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { left [ i ] = - 1 ; right [ i ] = n ; } for ( int i = 0 ; i < n ; i ++ ) { while ( s . Count > 0 && arr [ s . Peek ( ) ] >= arr [ i ] ) { s . Pop ( ) ; } if ( s . Count > 0 ) { left [ i ] = s . Peek ( ) ; } s . Push ( i ) ; } while ( s . Count > 0 ) { s . Pop ( ) ; } for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( s . Count > 0 && arr [ s . Peek ( ) ] >= arr [ i ] ) { s . Pop ( ) ; } if ( s . Count > 0 ) { right [ i ] = s . Peek ( ) ; } s . Push ( i ) ; } int [ ] ans = new int [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { ans [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { int len = right [ i ] - left [ i ] - 1 ; ans [ len ] = Math . Max ( ans [ len ] , arr [ i ] ) ; } for ( int i = n - 1 ; i >= 1 ; i -- ) { ans [ i ] = Math . Max ( ans [ i ] , ans [ i + 1 ] ) ; } for ( int i = 1 ; i <= n ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } public static void Main ( string [ ] args ) { printMaxOfMin ( arr . Length ) ; } }


"Find maximum of minimum for every window size in a given array | C # program using Naive approach to find maximum of minimum of all windows of different sizes ; Consider all windows of different sizes starting from size 1 ; Initialize max of min for current window size k ; Traverse through all windows of current size k ; Find minimum of current window ; Update maxOfMin if required ; Print max of min for current window size ; Driver Code"
Solution in C#:

using System ; class GFG { static int [ ] arr = { 10 , 20 , 30 , 50 , 10 , 70 , 30 } ; static void printMaxOfMin ( int n ) { for ( int k = 1 ; k <= n ; k ++ ) { int maxOfMin = int . MinValue ; for ( int i = 0 ; i <= n - k ; i ++ ) { int min = arr [ i ] ; for ( int j = 1 ; j < k ; j ++ ) { if ( arr [ i + j ] < min ) min = arr [ i + j ] ; } if ( min > maxOfMin ) maxOfMin = min ; } Console . Write ( maxOfMin + " ▁ " ) ; } } public static void Main ( ) { printMaxOfMin ( arr . Length ) ; } }


"Find maximum operations to reduce N to 1 | C # program to find maximum number moves possible ; To store number of prime factors of each number ; Function to find number of prime factors of each number ; if i is a prime number ; increase value by one from it 's preveious multiple ; make prefix sum this will be helpful for multiple test cases ; Driver Code ; Generate primeFactors array ; required answer"
Solution in C#:

using System ; class GFG { static int N = 1000005 ; static int [ ] primeFactors = new int [ N ] ; static void findPrimeFactors ( ) { for ( int i = 2 ; i < N ; i ++ ) if ( primeFactors [ i ] == 0 ) for ( int j = i ; j < N ; j += i ) primeFactors [ j ] = primeFactors [ j / i ] + 1 ; for ( int i = 1 ; i < N ; i ++ ) primeFactors [ i ] += primeFactors [ i - 1 ] ; } static public void Main ( ) { findPrimeFactors ( ) ; int a = 6 , b = 3 ; Console . WriteLine ( primeFactors [ a ] - primeFactors [ b ] ) ; } }


"Find maximum path sum in a 2D matrix when exactly two left moves are allowed | C # program to find maximum path sum in a 2D matrix when exactly two left moves are allowed ; Function to return the maximum path sum ; Copy last column i . e . starting and ending columns in another array ; Calculate suffix sum in each row ; Select the path we are going to follow ; Driver Code"
Solution in C#:

using System ; class GFG { static int N = 3 ; static int M = 3 ; static int findMaxSum ( int [ , ] arr ) { int sum = 0 ; int [ , ] b = new int [ N , M ] ; for ( int i = 0 ; i < N ; i ++ ) { b [ i , M - 1 ] = arr [ i , M - 1 ] ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = M - 2 ; j >= 0 ; j -- ) { b [ i , j ] = arr [ i , j ] + b [ i , j + 1 ] ; } } for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { sum = Math . Max ( sum , b [ i , j ] + b [ i - 1 , j ] ) ; b [ i , j ] = Math . Max ( b [ i , j ] , b [ i - 1 , j ] + arr [ i , j ] ) ; } } return sum ; } public static void Main ( ) { int [ , ] arr = { { 3 , 7 , 4 } , { 1 , 9 , 6 } , { 1 , 7 , 7 } } ; Console . WriteLine ( findMaxSum ( arr ) ) ; } }


"Find maximum points which can be obtained by deleting elements from array | C # program to find maximum cost after deleting all the elements form the array ; function to return maximum cost obtained ; find maximum element of the array . ; initialize count of all elements to zero . ; calculate frequency of all elements of array . ; stores cost of deleted elements . ; selecting minimum range from L and R . ; finds upto which elements are to be deleted when element num is selected . ; get maximum when selecting element num or not . ; Driver Code ; size of array ; function call to find maximum cost"
Solution in C#:

using System ; class GFG { static int maxCost ( int [ ] a , int n , int l , int r ) { int mx = 0 , k ; for ( int i = 0 ; i < n ; ++ i ) mx = Math . Max ( mx , a [ i ] ) ; int [ ] count = new int [ mx + 1 ] ; for ( int i = 0 ; i < count . Length ; i ++ ) count [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) count [ a [ i ] ] ++ ; int [ ] res = new int [ mx + 1 ] ; res [ 0 ] = 0 ; l = Math . Min ( l , r ) ; for ( int num = 1 ; num <= mx ; num ++ ) { k = Math . Max ( num - l - 1 , 0 ) ; res [ num ] = Math . Max ( res [ num - 1 ] , num * count [ num ] + res [ k ] ) ; } return res [ mx ] ; } public static void Main ( ) { int [ ] a = { 2 , 1 , 2 , 3 , 2 , 2 , 1 } ; int l = 1 , r = 1 ; int n = a . Length ; Console . WriteLine ( maxCost ( a , n , l , r ) ) ; } }


"Find maximum product of digits among numbers less than or equal to N | C # implementation of the approach ; Function that returns the maximum product of digits among numbers less than or equal to N ; Driver code"
Solution in C#:

using System ; class GFG { static int maxProd ( int N ) { if ( N == 0 ) return 1 ; if ( N < 10 ) return N ; return Math . Max ( maxProd ( N / 10 ) * ( N % 10 ) , maxProd ( N / 10 - 1 ) * 9 ) ; } static public void Main ( ) { int N = 390 ; Console . WriteLine ( maxProd ( N ) ) ; } }


"Find maximum subset | C # program for tha above approach ; Function to calculate maximum sum possible by taking at most K elements that is divisibly by D ; Variable to store final answer ; Traverse all subsets ; Update ans if necessary conditions are satisfied ; Driver code ; Input ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maximumSum ( List < int > A , int N , int K , int D ) { int ans = 0 ; for ( int i = 0 ; i < ( 1 << N ) ; i ++ ) { int sum = 0 ; int c = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( ( i >> j & 1 ) != 0 ) { sum += A [ j ] ; c ++ ; } } if ( sum % D == 0 && c <= K ) ans = Math . Max ( ans , sum ) ; } return ans ; } public static void Main ( ) { int N = 5 , K = 3 , D = 7 ; List < int > A = new List < int > ( ) { 1 , 11 , 5 , 5 , 18 } ; Console . Write ( maximumSum ( A , N , K , D ) ) ; } }


"Find maximum sum array of length less than or equal to m | C # program to find maximum sum of array of size less than or equal to m from given n arrays ; N and M to define sizes of arr , dp , current_arr and maxSum ; INF to define min value ; Function to find maximum sum ; dp array of size N x M ; current_arr of size M ; maxsum of size M ; if we have 0 elements from 0 th array ; compute the cumulative sum array ; calculating the maximum contiguous array for every length j , j is from 1 to lengtn of the array ; every state is depending on its previous state ; computation of dp table similar approach as knapsack problem ; now we have done processing with the last array lets find out what is the maximum sum possible ; Driver Code ; first element of each row is the size of that row"
Solution in C#:

 using System ; class GFG { static int N = 105 ; static int M = 1001 ; static int INF = - 1111111111 ; static int maxSum ( int [ ] [ ] arr ) { int [ , ] dp = new int [ N , M ] ; int [ ] current_arr = new int [ M ] ; int [ ] maxsum = new int [ M ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) dp [ i , j ] = - 1 ; } current_arr [ 0 ] = 0 ; dp [ 0 , 0 ] = 0 ; for ( int i = 1 ; i <= 5 ; i ++ ) { int len = arr [ i - 1 ] [ 0 ] ; for ( int j = 1 ; j <= len ; j ++ ) { current_arr [ j ] = arr [ i - 1 ] [ j ] ; current_arr [ j ] += current_arr [ j - 1 ] ; maxsum [ j ] = INF ; } for ( int j = 1 ; j <= len && j <= 6 ; j ++ ) for ( int k = 1 ; k <= len ; k ++ ) if ( j + k - 1 <= len ) maxsum [ j ] = Math . Max ( maxsum [ j ] , current_arr [ j + k - 1 ] - current_arr [ k - 1 ] ) ; for ( int j = 0 ; j <= 6 ; j ++ ) dp [ i , j ] = dp [ i - 1 , j ] ; for ( int j = 1 ; j <= 6 ; j ++ ) for ( int cur = 1 ; cur <= j && cur <= len ; cur ++ ) dp [ i , j ] = Math . Max ( dp [ i , j ] , dp [ i - 1 , j - cur ] + maxsum [ cur ] ) ; } int ans = 0 ; for ( int i = 0 ; i <= 6 ; i ++ ) ans = Math . Max ( ans , dp [ 5 , i ] ) ; return ans ; } static void Main ( ) { int [ ] [ ] arr = new int [ ] [ ] { new int [ ] { 3 , 2 , 3 , 5 } , new int [ ] { 2 , 7 , - 1 } , new int [ ] { 2 , 8 , 10 } , new int [ ] { 4 , 5 , 2 , 6 , 1 } , new int [ ] { 3 , 2 , 3 , - 2 } } ; Console . Write ( " Maximum ▁ sum ▁ can ▁ be ▁ " + " obtained ▁ is ▁ : ▁ " + maxSum ( arr ) + "
 " ) ; } }


"Find maximum sum from top to bottom row with no adjacent diagonal elements | C # implementation to find maximum sum from top to bottom row with no adjacent diagonal elements ; Function to find the maximum path sum from top to bottom row ; Create an auxiliary array of next row with the element and it 's position ; Sort the auxiliary array ; Find maximum from row above to be added to the current element ; Find the maximum element from the next row that can be added to current row element ; Find the maximum sum ; Driver code ; Function to find maximum path"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maxSum ( int [ , ] V , int n , int m ) { int ans = 0 ; for ( int i = n - 2 ; i >= 0 ; -- i ) { List < Tuple < int , int > > aux = new List < Tuple < int , int > > ( ) ; for ( int j = 0 ; j < m ; ++ j ) { aux . Add ( new Tuple < int , int > ( V [ i + 1 , j ] , j ) ) ; } aux . Sort ( ) ; aux . Reverse ( ) ; for ( int j = 0 ; j < m ; ++ j ) { for ( int k = 0 ; k < m ; ++ k ) { if ( aux [ k ] . Item2 - j == 0 || Math . Abs ( aux [ k ] . Item2 - j ) > 1 ) { V [ i , j ] += aux [ k ] . Item1 ; break ; } } } } for ( int i = 0 ; i < m ; ++ i ) { ans = Math . Max ( ans , V [ 0 , i ] ) ; } return ans ; } static void Main ( ) { int [ , ] V = { { 1 , 2 , 3 , 4 } , { 8 , 7 , 6 , 5 } , { 10 , 11 , 12 , 13 } } ; int n = V . GetLength ( 0 ) ; int m = V . GetLength ( 1 ) ; Console . WriteLine ( maxSum ( V , n , m ) ) ; } }


"Find maximum sum of subsequence after flipping signs of at most K elements in given Array | C ++ implementation for the above approach ; Function to calculate the max sum of subsequence ; Variable to store the max sum ; Sort the array ; Iterate over the array ; Flip sign ; Decrement k ; Traverse over the array ; Add only positive elements ; Return the max sum ; Driver Code ; Given array ; Variable to store number of flips are allowed ; Function call to find the maximum sum of subsequence"
Solution in C#:

using System ; class GFG { static int maxSubseq ( int [ ] arr , int N , int K ) { int sum = 0 ; Array . Sort ( arr ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( K == 0 ) break ; if ( arr [ i ] < 0 ) { arr [ i ] = - arr [ i ] ; K -- ; } } for ( int i = 0 ; i < N ; i ++ ) if ( arr [ i ] > 0 ) sum += arr [ i ] ; return sum ; } public static void Main ( string [ ] args ) { int [ ] arr = { 6 , - 10 , - 1 , 0 , - 4 , 2 } ; int K = 2 ; int N = arr . Length ; Console . Write ( maxSubseq ( arr , N , K ) ) ; } }


"Find maximum sum taking every Kth element in the array | C # implementation of the approach ; Function to return the maximum sum for every possible sequence such that a [ i ] + a [ i + k ] + a [ i + 2 k ] + ... + a [ i + qk ] is maximized ; Initialize the maximum with the smallest value ; Find maximum from all sequences ; Sum of the sequence starting from index i ; Update maximum ; Driver code"
Solution in C#:

using System ; class GFG { static int maxSum ( int [ ] arr , int n , int K ) { int maximum = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { int sumk = 0 ; for ( int j = i ; j < n ; j += K ) sumk = sumk + arr [ j ] ; maximum = Math . Max ( maximum , sumk ) ; } return maximum ; } public static void Main ( ) { int [ ] arr = { 3 , 6 , 4 , 7 , 2 } ; int n = arr . Length ; int K = 2 ; Console . WriteLine ( maxSum ( arr , n , K ) ) ; } }


"Find maximum sum taking every Kth element in the array | C # implementation of the approach ; Function to return the maximum sum for every possible sequence such that a [ i ] + a [ i + k ] + a [ i + 2 k ] + ... + a [ i + qk ] is maximized ; Initialize the maximum with the smallest value ; Initialize the sum array with zero ; Iterate from the right ; Update the sum starting at the current element ; Update the maximum so far ; Driver code"
Solution in C#:

using System ; class GFG { static int maxSum ( int [ ] arr , int n , int K ) { int maximum = int . MinValue ; int [ ] sum = new int [ n ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( i + K < n ) sum [ i ] = sum [ i + K ] + arr [ i ] ; else sum [ i ] = arr [ i ] ; maximum = Math . Max ( maximum , sum [ i ] ) ; } return maximum ; } public static void Main ( ) { int [ ] arr = { 3 , 6 , 4 , 7 , 2 } ; int n = arr . Length ; int K = 2 ; Console . Write ( maxSum ( arr , n , K ) ) ; } }


"Find maximum topics to prepare in order to pass the exam | C # implementation of the approach ; Function to return the maximum marks by considering topics which can be completed in the given time duration ; If we are given 0 time then nothing can be done So all values are 0 ; If we are given 0 topics then the time required will be 0 for sure ; Calculating the maximum marks that can be achieved under the given time constraints ; If time taken to read that topic is more than the time left now at position j then do no read that topic ; Two cases arise : 1 ) Considering current topic 2 ) Ignoring current topic We are finding maximum of ( current topic weightage + topics which can be done in leftover time - current topic time ) and ignoring current topic weightage sum ; Moving upwards in table from bottom right to calculate the total time taken to read the topics which can be done in given time and have highest weightage sum ; It means we have not considered reading this topic for max weightage sum ; Adding the topic time ; Evaluating the left over time after considering this current topic ; One topic completed ; It contains the maximum weightage sum formed by considering the topics ; Condition when exam cannot be passed ; Return the marks that can be obtained after passing the exam ; Driver code ; Number of topics , hours left and the passing marks ; n + 1 is taken for simplicity in loops Array will be indexed starting from 1"
Solution in C#:

using System ; class GFG { static int MaximumMarks ( int [ ] marksarr , int [ ] timearr , int h , int n , int p ) { int no_of_topics = n + 1 ; int total_time = h + 1 ; int [ , ] T = new int [ no_of_topics , total_time ] ; int i , j ; for ( i = 0 ; i < no_of_topics ; i ++ ) { T [ i , 0 ] = 0 ; } for ( j = 0 ; j < total_time ; j ++ ) { T [ 0 , j ] = 0 ; } for ( i = 1 ; i < no_of_topics ; i ++ ) { for ( j = 1 ; j < total_time ; j ++ ) { if ( j < timearr [ i ] ) { T [ i , j ] = T [ i - 1 , j ] ; } else { T [ i , j ] = Math . Max ( marksarr [ i ] + T [ i - 1 , j - timearr [ i ] ] , T [ i - 1 , j ] ) ; } } } i = no_of_topics - 1 ; j = total_time - 1 ; int sum = 0 ; while ( i > 0 && j > 0 ) { if ( T [ i , j ] == T [ i - 1 , j ] ) { i -- ; } else { sum += timearr [ i ] ; j -= timearr [ i ] ; i -- ; } } int marks = T [ no_of_topics - 1 , total_time - 1 ] ; if ( marks < p ) return - 1 ; return sum ; } public static void Main ( String [ ] args ) { int n = 4 , h = 10 , p = 10 ; int [ ] marksarr = { 0 , 6 , 4 , 2 , 8 } ; int [ ] timearr = { 0 , 4 , 6 , 2 , 7 } ; Console . WriteLine ( MaximumMarks ( marksarr , timearr , h , n , p ) ) ; } }


"Find maximum unreachable height using two ladders | C # implementation of the approach ; Function to return the maximum height which can 't be reached ; Driver code"
Solution in C#:

using System ; class GFG { static int maxHeight ( int h1 , int h2 ) { return ( ( h1 * h2 ) - h1 - h2 ) ; } public static void Main ( ) { int h1 = 7 , h2 = 5 ; Console . WriteLine ( Math . Max ( 0 , maxHeight ( h1 , h2 ) ) ) ; } }


"Find maximum value of Sum ( i * arr [ i ] ) with only rotations on given array allowed | C # program to find max value of i * arr [ i ] ; Returns max possible value of i * arr [ i ] ; Find array sum and i * arr [ i ] with no rotation Stores sum of arr [ i ] ; Stores sum of i * arr [ i ] ; Initialize result as 0 rotation sum ; Try all rotations one by one and find the maximum rotation sum . ; Return result ; Driver Code"
Solution in C#:

using System ; class Test { static int [ ] arr = new int [ ] { 10 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; static int maxSum ( ) { int arrSum = 0 ; int currVal = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { arrSum = arrSum + arr [ i ] ; currVal = currVal + ( i * arr [ i ] ) ; } int maxVal = currVal ; for ( int j = 1 ; j < arr . Length ; j ++ ) { currVal = currVal + arrSum - arr . Length * arr [ arr . Length - j ] ; if ( currVal > maxVal ) maxVal = currVal ; } return maxVal ; } public static void Main ( ) { Console . WriteLine ( " Max ▁ sum ▁ is ▁ " + maxSum ( ) ) ; } }


"Find maximum value of the last element after reducing the array with given operations | C # implementation of the approach ; Function to return the maximized value ; Overall minimum absolute value of some element from the array ; Add all absolute values ; Count positive and negative elements ; Both positive and negative values are present ; Only positive or negative values are present ; Driver code"
Solution in C#:

using System ; class GFG { static int find_maximum_value ( int [ ] a , int n ) { int sum = 0 ; int minimum = int . MaxValue ; int pos = 0 , neg = 0 ; for ( int i = 0 ; i < n ; i ++ ) { minimum = Math . Min ( minimum , Math . Abs ( a [ i ] ) ) ; sum += Math . Abs ( a [ i ] ) ; if ( a [ i ] >= 0 ) pos += 1 ; else neg += 1 ; } if ( pos > 0 && neg > 0 ) return sum ; return ( sum - 2 * minimum ) ; } static public void Main ( ) { int [ ] a = { 5 , 4 , 6 , 2 } ; int n = a . Length ; Console . WriteLine ( find_maximum_value ( a , n ) ) ; } }


"Find maximum value of x such that n ! % ( k ^ x ) = 0 | C # program to maximize the value of x such that n ! % ( k ^ x ) = 0 ; Function to maximize the value of x such that n ! % ( k ^ x ) = 0 ; Find square root of k and add 1 to it ; Run the loop from 2 to m and k should be greater than 1 ; optimize the value of k ; Minimum store ; Driver Code"
Solution in C#:

using System ; class GfG { public int findX ( int n , int k ) { int r = n , v , u ; int m = ( int ) Math . Sqrt ( k ) + 1 ; for ( int i = 2 ; i <= m && k > 1 ; i ++ ) { if ( i == m ) { i = k ; } for ( u = v = 0 ; k % i == 0 ; v ++ ) { k /= i ; } if ( v > 0 ) { int t = n ; while ( t > 0 ) { t /= i ; u += t ; } r = Math . Min ( r , u / v ) ; } } return r ; } } class geek { public static void Main ( ) { GfG g = new GfG ( ) ; int n = 5 ; int k = 2 ; Console . WriteLine ( g . findX ( n , k ) ) ; } }


"Find maximum volume of a cuboid from the given perimeter and area | C # implementation of the above approach ; function to return maximum volume ; calculate length ; calculate volume ; return result ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static float maxVol ( float P , float A ) { float l = ( float ) ( P - Math . Sqrt ( P * P - 24 * A ) ) / 12 ; float V = ( float ) ( l * ( A / 2.0 - l * ( P / 4.0 - l ) ) ) ; return V ; } public static void Main ( ) { float P = 20 , A = 16 ; Console . WriteLine ( maxVol ( P , A ) ) ; } }


"Find maximum xor of k elements in an array | C # implementation of the approach ; Function to return the maximum xor for a subset of size k from the given array ; Initialize result ; Traverse all subsets of the array ; __builtin_popcount ( ) returns the number of sets bits in an integer ; Initialize current xor as 0 ; If jth bit is set in i then include jth element in the current xor ; Update maximum xor so far ; Utility function ; Driver code"
Solution in C#:

using System ; class GFG { static int Max_Xor ( int [ ] arr , int n , int k ) { int maxXor = int . MinValue ; for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { if ( bitCount ( i ) == k ) { int cur_xor = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) == 0 ) cur_xor = cur_xor ^ arr [ j ] ; } maxXor = Math . Max ( maxXor , cur_xor ) ; } } return maxXor ; } static int bitCount ( long x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 5 , 4 , 1 , 3 , 7 , 6 , 8 } ; int n = arr . Length ; int k = 3 ; Console . WriteLine ( Max_Xor ( arr , n , k ) ) ; } }


"Find middle point segment from given segment lengths | C # implementation of the approach ; Function that returns the segment for the middle point ; the middle point ; stores the segment index ; increment sum by length of the segment ; if the middle is in between two segments ; if sum is greater than middle point ; Driver code"
Solution in C#:

using System ; class GFG { static int findSegment ( int n , int m , int [ ] segment_length ) { double meet_point = ( 1.0 * n ) / 2.0 ; int sum = 0 ; int segment_number = 0 ; for ( int i = 0 ; i < m ; i ++ ) { sum += segment_length [ i ] ; if ( ( double ) sum == meet_point ) { segment_number = - 1 ; break ; } if ( sum > meet_point ) { segment_number = i + 1 ; break ; } } return segment_number ; } public static void Main ( ) { int n = 13 ; int m = 3 ; int [ ] segment_length = new int [ ] { 3 , 2 , 8 } ; int ans = findSegment ( n , m , segment_length ) ; Console . WriteLine ( ans ) ; } }


"Find minimum GCD of all pairs in an array | C # program to find the minimum GCD of any pair in the array ; Function returns the minimum GCD of any pair ; Finding GCD of all the elements in the array . ; Driver code"
Solution in C#:

using System ; class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; else return __gcd ( b , a % b ) ; } static int MinimumGCD ( int [ ] arr , int n ) { int g = 0 ; for ( int i = 0 ; i < n ; i ++ ) { g = __gcd ( g , arr [ i ] ) ; } return g ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 6 , 8 , 3 } ; int N = arr . Length ; Console . WriteLine ( MinimumGCD ( arr , N ) ) ; } }


"Find minimum and maximum elements in singly Circular Linked List | C # program to find minimum and maximum value from singly circular linked list ; structure for a node ; Function to print minimum and maximum nodes of the circular linked list ; check list is empty ; pointer for traversing ; initialize head to current pointer ; initialize max int value to min initialize min int value to max ; While last node is not reached ; If current node data is lesser for min then replace it ; If current node data is greater for max then replace it ; Function to insert a node at the end of a Circular linked list ; Create a new node ; check node is created or not ; insert data into newly created node ; check list is empty if not have any node then make first node it ; if list have already some node ; move firt node to last node ; put first or head node address in new node link ; put new node address into last node link ( next ) ; Function to print the Circular linked list ; if list is empty simply show message ; traverse first to last node ; Driver Code"
Solution in C#:

 using System ; class GFG { public class Node { public int data ; public Node next ; } ; static void printMinMax ( Node head ) { if ( head == null ) { return ; } Node current ; current = head ; int min = int . MaxValue , max = int . MinValue ; while ( current . next != head ) { if ( current . data < min ) { min = current . data ; } if ( current . data > max ) { max = current . data ; } current = current . next ; } Console . WriteLine ( " Minimum = " ▁ + ▁ min ▁ + ▁ " , Maximum = " } static Node ( Node head , int data ) { current = head ; newNode = new Node ( ) ; if ( newNode == null ) { Console . Write ( "
 Memory ▁ Error
 " ) ; return null ; } newNode . data = data ; if ( head == null ) { newNode . next = newNode ; = newNode ; return ; } else { while ( current . next != head ) { current = current . next ; } newNode . next = head ; . next = newNode ; } return ; } static void ( Node head ) { current = head ; if ( head == null ) { Console . Write ( "
 Display ▁ List ▁ is ▁ empty
 " ) ; return ; } else { do { Console . Write ( " { 0 } ▁ " , current . data ) ; = current . next ; } while ( current != head ) ; } } public static void ( ) { Head = null ; Head = insertNode ( Head , 99 ) ; Head = insertNode ( Head , 11 ) ; Head = insertNode ( Head , 22 ) ; Head = insertNode ( Head , 33 ) ; Head = insertNode ( Head , 44 ) ; Head = insertNode ( Head , 55 ) ; Head = insertNode ( Head , 66 ) ; Console . WriteLine ( " Initial ▁ List : ▁ " ) ; displayList ( Head ) ; printMinMax ( Head ) ; } }


"Find minimum difference between any two elements | C # implementation of simple method to find minimum difference between any pair ; Returns minimum difference between any pair ; Initialize difference as infinite ; Find the min diff by comparing difference of all possible pairs in given array ; Return min diff ; Driver method to test the above function"
Solution in C#:

using System ; class GFG { static int findMinDiff ( int [ ] arr , int n ) { int diff = int . MaxValue ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( Math . Abs ( ( arr [ i ] - arr [ j ] ) ) < diff ) diff = Math . Abs ( ( arr [ i ] - arr [ j ] ) ) ; return diff ; } public static void Main ( ) { int [ ] arr = new int [ ] { 1 , 5 , 3 , 19 , 18 , 25 } ; Console . Write ( " Minimum ▁ difference ▁ is ▁ " + findMinDiff ( arr , arr . Length ) ) ; } }


"Find minimum difference between any two elements | C # program to find minimum difference between any pair in an unsorted array ; Returns minimum difference between any pair ; Sort array in non - decreasing order ; Initialize difference as infinite ; Find the min diff by comparing adjacent pairs in sorted array ; Return min diff ; Driver Code"
Solution in C#:

using System ; class GFG { static int findMinDiff ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int diff = int . MaxValue ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i + 1 ] - arr [ i ] < diff ) diff = arr [ i + 1 ] - arr [ i ] ; return diff ; } public static void Main ( ) { int [ ] arr = new int [ ] { 1 , 5 , 3 , 19 , 18 , 25 } ; Console . WriteLine ( " Minimum ▁ difference ▁ is ▁ " + findMinDiff ( arr , arr . Length ) ) ; } }


"Find minimum length sub | C # implementation of the approach ; Function to return the minimum length of a sub - array which contains { 0 , 1 , 2 , 3 , 4 } as a sub - sequence ; To store the indices where 0 , 1 , 2 , 3 and 4 are present ; To store if there exist a valid prefix of sequence in array ; Base Case ; If current element is 0 ; Update the count of 0 s till now ; Push the index of the new 0 ; To check if previous element of the given sequence is found till now ; If it is the end of sequence ; Iterate for other elements of the sequence ; Binary Search to find closest occurrence less than equal to starting point ; Update the starting point ; Driver Code"
Solution in C#:

using System ; class GFG { static int MAX_INT = 1000000 ; static int solve ( int [ ] array , int N ) { int [ , ] pos = new int [ 5 , 10000 ] ; int [ ] pref = new int [ 5 ] ; if ( array [ 0 ] == 0 ) { pref [ 0 ] = 1 ; pos [ 0 , pos . GetLength ( 0 ) - 1 ] = 0 ; } int ans = MAX_INT ; for ( int i = 1 ; i < N ; i ++ ) { if ( array [ i ] == 0 ) { pref [ 0 ] ++ ; pos [ 0 , pos . GetLength ( 0 ) - 1 ] = i ; } else { if ( pref [ array [ i ] - 1 ] > 0 ) { pref [ array [ i ] ] ++ ; pos [ array [ i ] , pos . GetLength ( 1 ) - 1 ] = i ; if ( array [ i ] == 4 ) { int end = i ; int start = i ; for ( int j = 3 ; j >= 0 ; j -- ) { int s = 0 ; int e = pos . GetLength ( 1 ) - 1 ; int temp = - 1 ; while ( s <= e ) { int m = ( s + e ) / 2 ; if ( pos [ j , m ] <= start ) { temp = pos [ j , m ] ; s = m + 1 ; } else e = m - 1 ; } start = temp ; } ans = Math . Min ( ans , end - start + 1 ) ; } } } } return ans ; } public static void Main ( String [ ] args ) { int [ ] array = { 0 , 1 , 2 , 3 , 4 , 2 , 0 , 3 , 4 } ; int N = array . Length ; Console . WriteLine ( solve ( array , N ) ) ; } }


"Find minimum moves to bring all elements in one cell of a matrix | C # implementation to find the minimum number of moves to bring all non - zero element in one cell of the matrix ; Function to find the minimum number of moves to bring all elements in one cell of matrix ; Moves variable to store the sum of number of moves ; Loop to count the number of the moves ; Condition to check that the current cell is a non - zero element ; Driver code ; Coordinates of given cell ; Given matrix ; Function call"
Solution in C#:

using System ; class GFG { static int M = 4 ; static int N = 5 ; public static void no_of_moves ( int [ , ] Matrix , int x , int y ) { int moves = 0 ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( Matrix [ i , j ] != 0 ) { moves += Math . Abs ( x - i ) ; moves += Math . Abs ( y - j ) ; } } } Console . WriteLine ( moves ) ; } public static void Main ( String [ ] args ) { int x = 3 ; int y = 2 ; int [ , ] Matrix = { { 1 , 0 , 1 , 1 , 0 } , { 0 , 1 , 1 , 0 , 1 } , { 0 , 0 , 1 , 1 , 0 } , { 1 , 1 , 1 , 0 , 0 } } ; no_of_moves ( Matrix , x , y ) ; } }


"Find minimum moves to reach target on an infinite line | C # program to find minimum moves to reach target if we can move i steps in i - th move . ; Handling negatives by symmetry ; Keep moving while sum is smaller or difference is odd . ; Driver code"
Solution in C#:

using System ; class GFG { static int reachTarget ( int target ) { target = Math . Abs ( target ) ; int sum = 0 , step = 0 ; while ( sum < target || ( sum - target ) % 2 != 0 ) { step ++ ; sum += step ; } return step ; } public static void Main ( ) { int target = 5 ; Console . WriteLine ( reachTarget ( target ) ) ; } }


"Find minimum number K such that sum of array after multiplication by K exceed S | C # implementation of the approach ; Function to return the minimum value of k that satisfies the given condition ; Store sum of array elements ; Calculate the sum after ; Return minimum possible K ; Driver code"
Solution in C#:

using System ; class GFG { static int findMinimumK ( int [ ] a , int n , int S ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } return ( int ) Math . Ceiling ( ( ( S + 1 ) * 1.0 ) / ( sum * 1.0 ) ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 10 , 7 , 8 , 10 , 12 , 19 } ; int n = a . Length ; int S = 200 ; Console . Write ( findMinimumK ( a , n , S ) ) ; } }


"Find minimum number of Log value needed to calculate Log upto N | C # program to find number of log values needed to calculate all the log values from 1 to N ; In this vector prime [ i ] will store true if prime [ i ] is prime , else store false ; Using sieve of Eratosthenes to find all prime upto N ; Function to find number of log values needed to calculate all the log values from 1 to N ; calculate primes upto N ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int MAX = 1000005 ; static List < Boolean > prime = new List < Boolean > ( MAX ) ; static void vecIni ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { prime . Add ( true ) ; } } static void sieve ( int N ) { prime . Insert ( 0 , false ) ; prime . Insert ( 1 , false ) ; for ( int i = 2 ; i <= N ; i ++ ) { if ( prime [ i ] ) { for ( int j = 2 ; i * j <= N ; j ++ ) { prime . Insert ( i * j , false ) ; } } } } static int countLogNeeded ( int N ) { int count = 0 ; sieve ( N ) ; for ( int i = 1 ; i <= N ; i ++ ) { if ( prime [ i ] ) { count ++ ; } } return count ; } public static void Main ( ) { vecIni ( ) ; int N = 6 ; Console . Write ( countLogNeeded ( N ) ) ; } }


"Find minimum number of coins that make a given value | A Dynamic Programming based Java program to find minimum of coins to make a given change V ; m is size of coins array ( number of different coins ) ; table [ i ] will be storing the minimum number of coins required for i value . So table [ V ] will have result ; Base case ( If given value V is 0 ) ; Initialize all table values as Infinite ; Compute minimum coins required for all values from 1 to V ; Go through all coins smaller than i ; Driver Code"
Solution in C#:

using System ; class GFG { static int minCoins ( int [ ] coins , int m , int V ) { int [ ] table = new int [ V + 1 ] ; table [ 0 ] = 0 ; for ( int i = 1 ; i <= V ; i ++ ) table [ i ] = int . MaxValue ; for ( int i = 1 ; i <= V ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) if ( coins [ j ] <= i ) { int sub_res = table [ i - coins [ j ] ] ; if ( sub_res != int . MaxValue && sub_res + 1 < table [ i ] ) table [ i ] = sub_res + 1 ; } } return table [ V ] ; } static public void Main ( ) { int [ ] coins = { 9 , 6 , 5 , 1 } ; int m = coins . Length ; int V = 11 ; Console . WriteLine ( " Minimum ▁ coins ▁ required ▁ is ▁ " + minCoins ( coins , m , V ) ) ; } }


"Find minimum number of coins that make a given value | A Naive recursive C # program to find minimum of coins to make a given change V ; m is size of coins array ( number of different coins ) ; base case ; Initialize result ; Try every coin that has smaller value than V ; Check for INT_MAX to avoid overflow and see if result can minimized ; Driver Code"
Solution in C#:

using System ; class coin { static int minCoins ( int [ ] coins , int m , int V ) { if ( V == 0 ) return 0 ; int res = int . MaxValue ; for ( int i = 0 ; i < m ; i ++ ) { if ( coins [ i ] <= V ) { int sub_res = minCoins ( coins , m , V - coins [ i ] ) ; if ( sub_res != int . MaxValue && sub_res + 1 < res ) res = sub_res + 1 ; } } return res ; } public static void Main ( ) { int [ ] coins = { 9 , 6 , 5 , 1 } ; int m = coins . Length ; int V = 11 ; Console . Write ( " Minimum ▁ coins ▁ required ▁ is ▁ " + minCoins ( coins , m , V ) ) ; } }