"Maximum sum of elements from each row in the matrix | C # Program to find row - wise maximum element sum considering elements in increasing order . ; Function to perform given task ; Getting the maximum element from last row ; Comparing it with the elements of above rows ; Maximum of current row . ; If we could not an element smaller than prev_max . ; Driver Program"
Solution in C#:

using System ; class GFG { static int N = 3 ; static int getGreatestSum ( int [ , ] a ) { int prev_max = 0 ; for ( int j = 0 ; j < N ; j ++ ) if ( prev_max < a [ N - 1 , j ] ) prev_max = a [ N - 1 , j ] ; int sum = prev_max ; for ( int i = N - 2 ; i >= 0 ; i -- ) { int curr_max = - 2147483648 ; for ( int j = 0 ; j < N ; j ++ ) if ( prev_max > a [ i , j ] && a [ i , j ] > curr_max ) curr_max = a [ i , j ] ; if ( curr_max == - 2147483648 ) return - 1 ; prev_max = curr_max ; sum += prev_max ; } return sum ; } public static void Main ( ) { int [ , ] a = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; Console . WriteLine ( getGreatestSum ( a ) ) ; int [ , ] b = { { 4 , 5 , 6 } , { 4 , 5 , 6 } , { 4 , 5 , 6 } } ; Console . WriteLine ( getGreatestSum ( b ) ) ; } }


"Maximum sum of i * arr [ i ] among all rotations of a given array | A Naive C # program to find maximum sum rotation ; Returns maximum value of i * arr [ i ] ; Initialize result ; Consider rotation beginning with i for all possible values of i . ; Initialize sum of current rotation ; Compute sum of all values . We don 't  acutally rotation the array, but compute  sum by finding ndexes when arr[i] is  first element ; Update result if required ; Driver code"
Solution in C#:

using System ; class GFG { static int maxSum ( int [ ] arr , int n ) { int res = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { int curr_sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) { int index = ( i + j ) % n ; curr_sum += j * arr [ index ] ; } res = Math . Max ( res , curr_sum ) ; } return res ; } public static void Main ( ) { int [ ] arr = { 8 , 3 , 1 , 2 } ; int n = arr . Length ; Console . WriteLine ( maxSum ( arr , n ) ) ; } }


"Maximum sum of i * arr [ i ] among all rotations of a given array | An efficient C # program to compute maximum sum of i * arr [ i ] ; Compute sum of all array elements ; Compute sum of i * arr [ i ] for initial configuration . ; Initialize result ; Compute values for other iterations ; Compute next value using previous value in O ( 1 ) time ; Update current value ; Update result if required ; Driver code"
Solution in C#:

using System ; class GFG { static int maxSum ( int [ ] arr , int n ) { int cum_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) cum_sum += arr [ i ] ; int curr_val = 0 ; for ( int i = 0 ; i < n ; i ++ ) curr_val += i * arr [ i ] ; int res = curr_val ; for ( int i = 1 ; i < n ; i ++ ) { int next_val = curr_val - ( cum_sum - arr [ i - 1 ] ) + arr [ i - 1 ] * ( n - 1 ) ; curr_val = next_val ; res = Math . Max ( res , next_val ) ; } return res ; } public static void Main ( ) { int [ ] arr = { 8 , 3 , 1 , 2 } ; int n = arr . Length ; Console . Write ( maxSum ( arr , n ) ) ; } }


"Maximum sum of increasing order elements from n arrays | C # program to find maximum sum by selecting a element from n arrays ; To calculate maximum sum by selecting element from each array ; Sort each array ; Store maximum element of last array ; Selecting maximum element from previoulsy selected element ; j = - 1 means no element is found in a [ i ] so return 0 ; Driver Code"
Solution in C#:

using System ; class GFG { static int M = 4 ; static void sort ( ref int [ , ] a , int row , int n ) { for ( int i = 0 ; i < M - 1 ; i ++ ) { if ( a [ row , i ] > a [ row , i + 1 ] ) { int temp = a [ row , i ] ; a [ row , i ] = a [ row , i + 1 ] ; a [ row , i + 1 ] = temp ; } } } static int maximumSum ( int [ , ] a , int n ) { int i = 0 , j = 0 ; for ( i = 0 ; i < n ; i ++ ) sort ( ref a , i , n ) ; int sum = a [ n - 1 , M - 1 ] ; int prev = a [ n - 1 , M - 1 ] ; for ( i = n - 2 ; i >= 0 ; i -- ) { for ( j = M - 1 ; j >= 0 ; j -- ) { if ( a [ i , j ] < prev ) { prev = a [ i , j ] ; sum += prev ; break ; } } if ( j == - 1 ) return 0 ; } return sum ; } static void Main ( ) { int [ , ] arr = new int [ , ] { { 1 , 7 , 3 , 4 } , { 4 , 2 , 5 , 1 } , { 9 , 5 , 1 , 8 } } ; int n = arr . GetLength ( 0 ) ; Console . Write ( maximumSum ( arr , n ) ) ; } }


"Maximum sum of increasing order elements from n arrays | C # program to find maximum sum by selecting a element from n arrays ; To calculate maximum sum by selecting element from each array ; Store maximum element of last array ; Selecting maximum element from previoulsy selected element ; max_smaller equals to INT_MIN means no element is found in a [ i ] so return 0 ; Driver code"
Solution in C#:

using System ; class GFG { static int M = 4 ; static int maximumSum ( int [ , ] a , int n ) { int prev = Math . Max ( a [ n - 1 , 0 ] , a [ n - 1 , M - 1 ] + 1 ) ; int sum = prev ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int max_smaller = Int32 . MinValue ; for ( int j = M - 1 ; j >= 0 ; j -- ) { if ( a [ i , j ] < prev && a [ i , j ] > max_smaller ) { max_smaller = a [ i , j ] ; } } if ( max_smaller == Int32 . MinValue ) { return 0 ; } prev = max_smaller ; sum += max_smaller ; } return sum ; } static public void Main ( ) { int [ , ] arr = { { 1 , 7 , 3 , 4 } , { 4 , 2 , 5 , 1 } , { 9 , 5 , 1 , 8 } } ; int n = arr . GetLength ( 0 ) ; Console . Write ( maximumSum ( arr , n ) ) ; } }


"Maximum sum of minimums of pairs in an array | C # implementation of the approach ; Function to return the maximum required sum of the pairs ; Sort the array ; To store the sum ; Start making pairs of every two consecutive elements as n is even ; Minimum element of the current pair ; Return the maximum possible sum ; Driver code"
Solution in C#:

using System ; class GFG { static int maxSum ( int [ ] a , int n ) { Array . Sort ( a ) ; int sum = 0 ; for ( int i = 0 ; i < n - 1 ; i += 2 ) { sum += a [ i ] ; } return sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 2 , 1 , 4 , 5 } ; int n = arr . Length ; Console . WriteLine ( maxSum ( arr , n ) ) ; } }


"Maximum sum of nodes in Binary tree such that no two are adjacent | C # program to find maximum sum from a subset of nodes of binary tree ; A binary tree node structure ; method returns maximum sum possible from subtrees rooted at grandChildrens of node ' node ' ; call for children of left child only if it is not NULL ; call for children of right child only if it is not NULL ; Utility method to return maximum sum rooted at node ' node ' ; If node is already processed then return calculated value from map ; take current node value and call for all grand children ; don 't take current node value and  call for all children ; choose maximum from both above calls and store that in map ; Returns maximum sum from subset of nodes of binary tree under given constraints ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; public class FindSumOfNotAdjacentNodes { public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } ; public static int sumOfGrandChildren ( Node node , Dictionary < Node , int > mp ) { int sum = 0 ; if ( node . left != null ) sum += getMaxSumUtil ( node . left . left , mp ) + getMaxSumUtil ( node . left . right , mp ) ; if ( node . right != null ) sum += getMaxSumUtil ( node . right . left , mp ) + getMaxSumUtil ( node . right . right , mp ) ; return sum ; } public static int getMaxSumUtil ( Node node , Dictionary < Node , int > mp ) { if ( node == null ) return 0 ; if ( mp . ContainsKey ( node ) ) return mp [ node ] ; int incl = node . data + sumOfGrandChildren ( node , mp ) ; int excl = getMaxSumUtil ( node . left , mp ) + getMaxSumUtil ( node . right , mp ) ; mp . Add ( node , Math . Max ( incl , excl ) ) ; return mp [ node ] ; } public static int getMaxSum ( Node node ) { if ( node == null ) return 0 ; Dictionary < Node , int > mp = new Dictionary < Node , int > ( ) ; return getMaxSumUtil ( node , mp ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . right . left = new Node ( 4 ) ; root . right . right = new Node ( 5 ) ; root . left . left = new Node ( 1 ) ; Console . Write ( getMaxSum ( root ) ) ; } }


"Maximum sum of nodes in Binary tree such that no two are adjacent | C # program to find maximum sum in Binary Tree such that no two nodes are adjacent . ; A binary tree node structure ; maxSumHelper function ; This node is included ( Left and right children are not included ) ; This node is excluded ( Either left or right child is included ) ; Returns maximum sum from subset of nodes of binary tree under given constraints ; Driver code ; Pair class"
Solution in C#:

using System ; public class FindSumOfNotAdjacentNodes { public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } ; public static Pair maxSumHelper ( Node root ) { Pair sum ; if ( root == null ) { sum = new Pair ( 0 , 0 ) ; return sum ; } Pair sum1 = maxSumHelper ( root . left ) ; Pair sum2 = maxSumHelper ( root . right ) ; Pair sum3 = new Pair ( 0 , 0 ) ; sum3 . first = sum1 . second + sum2 . second + root . data ; sum3 . second = Math . Max ( sum1 . first , sum1 . second ) + Math . Max ( sum2 . first , sum2 . second ) ; return sum3 ; } public static int maxSum ( Node root ) { Pair res = maxSumHelper ( root ) ; return Math . Max ( res . first , res . second ) ; } public static void Main ( ) { Node root = new Node ( 10 ) ; root . left = new Node ( 1 ) ; root . left . left = new Node ( 2 ) ; root . left . left . left = new Node ( 1 ) ; root . left . right = new Node ( 3 ) ; root . left . right . left = new Node ( 4 ) ; root . left . right . right = new Node ( 5 ) ; Console . Write ( maxSum ( root ) ) ; } } public class Pair { public int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } }


"Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming | C # program to find maximum sum of a subset of nodes that are not adjacent ; Function to find the diameter of the tree using Dynamic Programming ; Traverse for all children of node ; Call DFS function again ; Include the current node then donot include the children ; Donot include current node , then include children or not include them ; Recurrence value ; Driver program to test above functions ; Constructed tree is 1 / \ 2 3 / \ 4 5 ; create undirected edges ; Numbers to node ; Find maximum sum by calling function"
Solution in C#:

using System ; using System . Collections ; class GFG { public static void dfs ( int node , int parent , int [ ] dp1 , int [ ] dp2 , ArrayList [ ] adj , int [ ] tree ) { int sum1 = 0 , sum2 = 0 ; foreach ( int i in adj [ node ] ) { if ( i == parent ) continue ; dfs ( i , node , dp1 , dp2 , adj , tree ) ; sum1 += dp2 [ i ] ; sum2 += Math . Max ( dp1 [ i ] , dp2 [ i ] ) ; } dp1 [ node ] = tree [ node ] + sum1 ; dp2 [ node ] = sum2 ; } public static void Main ( string [ ] arg ) { int n = 5 ; ArrayList [ ] adj = new ArrayList [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { adj [ i ] = new ArrayList ( ) ; } adj [ 1 ] . Add ( 2 ) ; adj [ 2 ] . Add ( 1 ) ; adj [ 1 ] . Add ( 3 ) ; adj [ 3 ] . Add ( 1 ) ; adj [ 2 ] . Add ( 4 ) ; adj [ 4 ] . Add ( 2 ) ; adj [ 2 ] . Add ( 5 ) ; adj [ 5 ] . Add ( 2 ) ; int [ ] tree = new int [ n + 1 ] ; tree [ 1 ] = 10 ; tree [ 2 ] = 5 ; tree [ 3 ] = 11 ; tree [ 4 ] = 6 ; tree [ 5 ] = 8 ; int [ ] dp1 = new int [ n + 1 ] ; int [ ] dp2 = new int [ n + 1 ] ; Array . Fill ( dp1 , 0 ) ; Array . Fill ( dp2 , 0 ) ; dfs ( 1 , 1 , dp1 , dp2 , adj , tree ) ; Console . Write ( " Maximum ▁ sum : ▁ " + Math . Max ( dp1 [ 1 ] , dp2 [ 1 ] ) ) ; } }


"Maximum sum of non | C # program to implement above approach ; Variable to store states of dp ; Variable to check if a given state has been solved ; Function to find the maximum sum subsequence such that no two elements are adjacent ; Base case ; To check if a state has been solved ; Variable to store prefix sum for sub - array { i , j } ; Required recurrence relation ; Returning the value ; Driver code ; Input array"
Solution in C#:

using System ; class GFG { static int maxLen = 10 ; static int [ ] dp = new int [ maxLen ] ; static bool [ ] visit = new bool [ maxLen ] ; static int maxSum ( int [ ] arr , int i , int n , int k ) { if ( i >= n ) return 0 ; if ( visit [ i ] ) return dp [ i ] ; visit [ i ] = true ; int tot = 0 ; dp [ i ] = maxSum ( arr , i + 1 , n , k ) ; for ( int j = i ; j < ( i + k ) && ( j < n ) ; j ++ ) { tot += arr [ j ] ; dp [ i ] = Math . Max ( dp [ i ] , tot + maxSum ( arr , j + 2 , n , k ) ) ; } return dp [ i ] ; } static public void Main ( ) { int [ ] arr = { - 1 , 2 , - 3 , 4 , 5 } ; int k = 2 ; int n = arr . Length ; Console . WriteLine ( maxSum ( arr , 0 , n , k ) ) ; } }


"Maximum sum of pairs that are at least K distance apart in an array | C # program for the above approach ; Function to find the largest sum pair that are K distant apart ; Stores the prefix maximum array ; Base Case ; Traverse the array and update the maximum value upto index i ; Stores the maximum sum of pairs ; Iterate over the range [ K , N ] ; Find the maximum value of the sum of valid pairs ; Return the resultant sum ; Driver Code"
Solution in C#:

using System ; class GFG { static int getMaxPairSum ( int [ ] arr , int N , int K ) { int [ ] preMax = new int [ N ] ; preMax [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { preMax [ i ] = Math . Max ( preMax [ i - 1 ] , arr [ i ] ) ; } int res = Int32 . MinValue ; for ( int i = K ; i < N ; i ++ ) { res = Math . Max ( res , arr [ i ] + preMax [ i - K ] ) ; } return res ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 4 , 8 , 6 , 3 } ; int K = 3 ; int N = arr . Length ; Console . Write ( getMaxPairSum ( arr , N , K ) ) ; } }


"Maximum sum of pairs with specific difference | C # program to find maximum pair sum whose difference is less than K ; Method to return maximum sum we can get by finding less than K difference pairs ; Sort elements to ensure every i and i - 1 is closest possible pair ; To get maximum possible sum , iterate from largest to smallest , giving larger numbers priority over smaller numbers . ; Case I : Diff of arr [ i ] and arr [ i - 1 ] is less then K , add to maxSum Case II : Diff between arr [ i ] and arr [ i - 1 ] is not less then K , move to next i since with sorting we know , arr [ i ] - arr [ i - 1 ] < arr [ i ] - arr [ i - 2 ] and so on . ; Assuming only positive numbers . ; When a match is found skip this pair ; Driver Code"
Solution in C#:

using System ; class GFG { static int maxSumPairWithDifferenceLessThanK ( int [ ] arr , int N , int k ) { int maxSum = 0 ; Array . Sort ( arr ) ; for ( int i = N - 1 ; i > 0 ; -- i ) { if ( arr [ i ] - arr [ i - 1 ] < k ) { maxSum += arr [ i ] ; maxSum += arr [ i - 1 ] ; -- i ; } } return maxSum ; } public static void Main ( ) { int [ ] arr = { 3 , 5 , 10 , 15 , 17 , 12 , 9 } ; int N = arr . Length ; int K = 4 ; Console . Write ( maxSumPairWithDifferenceLessThanK ( arr , N , K ) ) ; } }


"Maximum sum of pairs with specific difference | C # program to find maximum pair sum whose difference is less than K ; method to return maximum sum we can get by finding less than K difference pair ; Sort input array in ascending order . ; dp [ i ] denotes the maximum disjoint pair sum we can achieve using first i elements ; if no element then dp value will be 0 ; first give previous value to dp [ i ] i . e . no pairing with ( i - 1 ) th element ; if current and previous element can form a pair ; update dp [ i ] by choosing maximum between pairing and not pairing ; last index will have the result ; Driver code to test above methods"
Solution in C#:

using System ; class GFG { static int maxSumPairWithDifferenceLessThanK ( int [ ] arr , int N , int K ) { Array . Sort ( arr ) ; int [ ] dp = new int [ N ] ; dp [ 0 ] = 0 ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] = dp [ i - 1 ] ; if ( arr [ i ] - arr [ i - 1 ] < K ) { if ( i >= 2 ) dp [ i ] = Math . Max ( dp [ i ] , dp [ i - 2 ] + arr [ i ] + arr [ i - 1 ] ) ; else dp [ i ] = Math . Max ( dp [ i ] , arr [ i ] + arr [ i - 1 ] ) ; } } return dp [ N - 1 ] ; } public static void Main ( ) { int [ ] arr = { 3 , 5 , 10 , 15 , 17 , 12 , 9 } ; int N = arr . Length ; int K = 4 ; Console . WriteLine ( maxSumPairWithDifferenceLessThanK ( arr , N , K ) ) ; } }


"Maximum sum of pairwise product in an array with negative allowed | C # program for above implementation ; Function to find the maximum sum ; Sort the array first ; First multiply negative numbers pairwise and sum up from starting as to get maximum sum . ; Second multiply positive numbers pairwise and summed up from the last as to get maximum sum . ; To handle case if positive and negative numbers both are odd in counts . ; If one of them occurs odd times ; Drivers code"
Solution in C#:

using System ; class GFG { static int Mod = 1000000007 ; static long findSum ( int [ ] arr , int n ) { long sum = 0 ; Array . Sort ( arr ) ; int i = 0 ; while ( i < n && arr [ i ] < 0 ) { if ( i != n - 1 && arr [ i + 1 ] <= 0 ) { sum = ( sum + ( arr [ i ] * arr [ i + 1 ] ) % Mod ) % Mod ; i += 2 ; } else break ; } int j = n - 1 ; while ( j >= 0 && arr [ j ] > 0 ) { if ( j != 0 && arr [ j - 1 ] > 0 ) { sum = ( sum + ( arr [ j ] * arr [ j - 1 ] ) % Mod ) % Mod ; j -= 2 ; } else break ; } if ( j > i ) sum = ( sum + ( arr [ i ] * arr [ j ] ) % Mod ) % Mod ; else if ( i == j ) sum = ( sum + arr [ i ] ) % Mod ; return sum ; } public static void Main ( ) { int [ ] arr = { - 1 , 9 , 4 , 5 , - 4 , 7 } ; int n = arr . Length ; Console . WriteLine ( findSum ( arr , n ) ) ; } }


"Maximum sum of segments among all segments formed in array after Q queries | C # program for the above approach ; Stores the maximum integer of the sets for each query ; Function to perform the find operation of disjoint set union ; Function to perform the Union operation of disjoint set union ; Find the parent of a and b ; Update the parent ; Update the sum of set a ; Function to find the maximum element from the sets after each operation ; Stores the parent elements of the sets ; Stores the rank of the sets ; Stores the sum of the sets ; Stores the maximum element for each query ; Initially set is empty ; Update the sum as the current element ; After the last query set will be empty and sum will be 0 ; Check if the current element is not in any set then make parent as current element of the queries ; Check left side of the queries [ i ] is not added in any set ; Add the queries [ i ] and the queries [ i ] - 1 in one set ; Check right side of the queries [ i ] is not added in any set ; Add queries [ i ] and the queries [ i ] + 1 in one set ; Update the maxAns ; Push maxAns to the currMax ; Print currMax values in the reverse order ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static int maxAns = int . MinValue ; static int Find ( int [ ] parent , int a ) { return parent [ a ] = ( parent [ a ] == a ) ? a : ( Find ( parent , parent [ a ] ) ) ; } static void Union ( int [ ] parent , int [ ] rank , int [ ] setSum , int a , int b ) { a = Find ( parent , a ) ; b = Find ( parent , b ) ; if ( a == b ) return ; if ( rank [ a ] > rank [ b ] ) rank [ a ] ++ ; if ( rank [ b ] > rank [ a ] ) { int x = a ; a = b ; b = x ; } parent [ b ] = a ; setSum [ a ] += setSum [ b ] ; } static void maxValues ( int [ ] arr , int [ ] queries , int N ) { int [ ] parent = new int [ N + 1 ] ; int [ ] rank = new int [ N + 1 ] ; int [ ] setSum = new int [ N + 1 ] ; List < int > currMax = new List < int > ( ) ; for ( int i = 1 ; i < N + 1 ; i ++ ) { parent [ i ] = - 1 ; setSum [ i ] = arr [ i - 1 ] ; } currMax . Add ( 0 ) ; for ( int i = N - 1 ; i > 0 ; i -- ) { if ( parent [ queries [ i ] ] == - 1 ) { parent [ queries [ i ] ] = queries [ i ] ; } if ( queries [ i ] - 1 >= 0 && parent [ queries [ i ] - 1 ] != - 1 ) { Union ( parent , rank , setSum , queries [ i ] , queries [ i ] - 1 ) ; } if ( queries [ i ] + 1 <= N && parent [ queries [ i ] + 1 ] != - 1 ) { Union ( parent , rank , setSum , queries [ i ] , queries [ i ] + 1 ) ; } maxAns = Math . Max ( setSum [ queries [ i ] ] , maxAns ) ; currMax . Add ( maxAns ) ; } for ( int i = currMax . Count - 1 ; i >= 0 ; i -- ) { Console . Write ( currMax [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 2 , 5 } ; int [ ] queries = { 3 , 4 , 1 , 2 } ; int N = arr . Length ; maxValues ( arr , queries , N ) ; } }


"Maximum sum of values in a given range of an Array for Q queries when shuffling is allowed | C # implementation to find the maximum sum of K subarrays when shuffling is allowed ; Function to find the maximum sum of all subarrays ; Initialize maxsum and prefixArray ; Find the frequency using prefix Array ; Perform prefix sum ; Sort both arrays to get a greedy result ; Finally multiply largest frequency with largest array element . ; Return the answer ; Driver Code ; Initial Array ; Subarrays ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maximumSubarraySum ( int [ ] a , int n , List < List < int > > subarrays ) { int i , maxsum = 0 ; int [ ] prefixArray = new int [ n ] ; for ( i = 0 ; i < subarrays . Count ; ++ i ) { prefixArray [ subarrays [ i ] [ 0 ] - 1 ] ++ ; prefixArray [ subarrays [ i ] [ 1 ] ] -- ; } for ( i = 1 ; i < n ; i ++ ) { prefixArray [ i ] += prefixArray [ i - 1 ] ; } Array . Sort ( prefixArray ) ; Array . Sort ( a ) ; for ( i = 0 ; i < n ; i ++ ) maxsum += a [ i ] * prefixArray [ i ] ; return maxsum ; } static void Main ( ) { int n = 6 ; int [ ] a = { 4 , 1 , 2 , 1 , 9 , 2 } ; List < List < int > > subarrays = new List < List < int > > ( ) ; subarrays . Add ( new List < int > { 1 , 2 } ) ; subarrays . Add ( new List < int > { 1 , 3 } ) ; subarrays . Add ( new List < int > { 1 , 4 } ) ; subarrays . Add ( new List < int > { 3 , 4 } ) ; Console . WriteLine ( maximumSubarraySum ( a , n , subarrays ) ) ; } }


"Maximum sum path in a matrix from top to bottom and back | C # implementation of the approach ; Input matrix ; DP matrix ; Function to return the sum of the cells arr [ i1 ] [ j1 ] and arr [ i2 ] [ j2 ] ; Recursive function to return the required maximum cost path ; Column number of second path ; Base Case ; If already calculated , return from DP matrix ; Recurring for neighbouring cells of both paths together ; Saving result to the DP matrix for current state ; Driver code ; set initial value"
Solution in C#:

using System ; class GFG { static int n = 4 , m = 4 ; static int [ , ] arr = { { 1 , 0 , 3 , - 1 } , { 3 , 5 , 1 , - 2 } , { - 2 , 0 , 1 , 1 } , { 2 , 1 , - 1 , 1 } } ; static int [ , , ] cache = new int [ 5 , 5 , 5 ] ; static int sum ( int i1 , int j1 , int i2 , int j2 ) { if ( i1 == i2 && j1 == j2 ) { return arr [ i1 , j1 ] ; } return arr [ i1 , j1 ] + arr [ i2 , j2 ] ; } static int maxSumPath ( int i1 , int j1 , int i2 ) { int j2 = i1 + j1 - i2 ; if ( i1 >= n i2 >= n j1 >= m j2 >= m ) { return 0 ; } if ( cache [ i1 , j1 , i2 ] != - 1 ) { return cache [ i1 , j1 , i2 ] ; } int ans = int . MinValue ; ans = Math . Max ( ans , maxSumPath ( i1 + 1 , j1 , i2 + 1 ) + sum ( i1 , j1 , i2 , j2 ) ) ; ans = Math . Max ( ans , maxSumPath ( i1 , j1 + 1 , i2 ) + sum ( i1 , j1 , i2 , j2 ) ) ; ans = Math . Max ( ans , maxSumPath ( i1 , j1 + 1 , i2 + 1 ) + sum ( i1 , j1 , i2 , j2 ) ) ; ans = Math . Max ( ans , maxSumPath ( i1 + 1 , j1 , i2 ) + sum ( i1 , j1 , i2 , j2 ) ) ; cache [ i1 , j1 , i2 ] = ans ; return ans ; } public static void Main ( String [ ] args ) { for ( int i = 0 ; i < 5 ; i ++ ) for ( int i1 = 0 ; i1 < 5 ; i1 ++ ) for ( int i2 = 0 ; i2 < 5 ; i2 ++ ) cache [ i , i1 , i2 ] = - 1 ; Console . WriteLine ( maxSumPath ( 0 , 0 , 0 ) ) ; } }


"Maximum sum path in a matrix from top to bottom | C # implementation to find the maximum sum path in a matrix ; function to find the maximum sum path in a matrix ; if there is a single element only ; dp [ ] [ ] matrix to store the results of each iteration ; base case , copying elements of last row ; building up the dp [ ] [ ] matrix from bottom to the top row ; finding the maximum diagonal element in the ( i + 1 ) th row if that cell exists ; adding that ' max ' element to the mat [ i ] [ j ] element ; finding the maximum value from the first row of dp [ ] [ ] ; required maximum sum ; Driver code"
Solution in C#:

using System ; class MaxSumPath { static int maxSum ( int [ , ] mat , int n ) { if ( n == 1 ) return mat [ 0 , 0 ] ; int [ , ] dp = new int [ n , n ] ; int maxSum = int . MinValue , max ; for ( int j = 0 ; j < n ; j ++ ) dp [ n - 1 , j ] = mat [ n - 1 , j ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < n ; j ++ ) { max = int . MinValue ; if ( ( ( j - 1 ) >= 0 ) && ( max < dp [ i + 1 , j - 1 ] ) ) max = dp [ i + 1 , j - 1 ] ; if ( ( ( j + 1 ) < n ) && ( max < dp [ i + 1 , j + 1 ] ) ) max = dp [ i + 1 , j + 1 ] ; dp [ i , j ] = mat [ i , j ] + max ; } } for ( int j = 0 ; j < n ; j ++ ) if ( maxSum < dp [ 0 , j ] ) maxSum = dp [ 0 , j ] ; return maxSum ; } public static void Main ( ) { int [ , ] mat = { { 5 , 6 , 1 , 7 } , { - 2 , 10 , 8 , - 1 } , { 3 , - 7 , - 9 , 11 } , { 12 , - 4 , 2 , 6 } } ; int n = 4 ; Console . WriteLine ( " Maximum ▁ Sum ▁ = ▁ " + maxSum ( mat , n ) ) ; } }


"Maximum sum path in a matrix from top | C # program for the above approach ; Function to find the maximum sum path in the grid ; Dimensions of grid [ , ] ; Stores maximum sum at each cell sum [ i , j ] from cell sum [ 0 , 0 ] ; Iterate to compute the maximum sum path in the grid ; Update the maximum path sum ; Return the maximum sum ; Driver Code"
Solution in C#:

using System ; class GFG { static int MaximumPath ( int [ , ] grid ) { int N = grid . GetLength ( 0 ) ; int M = grid . GetLength ( 1 ) ; int [ , ] sum = new int [ N + 1 , M + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= M ; j ++ ) { sum [ i , j ] = Math . Max ( sum [ i - 1 , j ] , sum [ i , j - 1 ] ) + grid [ i - 1 , j - 1 ] ; } } return sum [ N , M ] ; } public static void Main ( String [ ] args ) { int [ , ] grid = { { 1 , 2 } , { 3 , 5 } } ; Console . Write ( MaximumPath ( grid ) ) ; } }


"Maximum sum possible by assigning alternate positive and negative sign to elements in a subsequence | C # program for the above approach ; Function to find the maximum sum subsequence ; Base Case ; If current state is already calculated then use it ; If current element is positive ; Update ans and recursively call with update value of flag ; Else current element is negative ; Update ans and recursively call with update value of flag ; Return maximum sum subsequence ; Function that finds the maximum sum of element of the subsequence with alternate + ve and - ve signs ; Create auxiliary array dp [ ] [ ] ; Initialize dp [ ] [ ] ; Function Call ; Driver Code ; Given array arr [ ] ; Function call"
Solution in C#:

using System ; class GFG { static int findMax ( int [ ] a , int [ , ] dp , int i , int flag ) { if ( i == ( int ) a . Length ) { return 0 ; } if ( dp [ i , flag ] != - 1 ) { return dp [ i , flag ] ; } int ans ; if ( flag == 0 ) { ans = Math . Max ( findMax ( a , dp , i + 1 , 0 ) , a [ i ] + findMax ( a , dp , i + 1 , 1 ) ) ; } else { ans = Math . Max ( findMax ( a , dp , i + 1 , 1 ) , - 1 * a [ i ] + findMax ( a , dp , i + 1 , 0 ) ) ; } return dp [ i , flag ] = ans ; } static void findMaxSumUtil ( int [ ] arr , int N ) { int [ , ] dp = new int [ N , 2 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { dp [ i , j ] = - 1 ; } } Console . WriteLine ( findMax ( arr , dp , 0 , 0 ) ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 1 , 2 } ; int N = arr . Length ; findMaxSumUtil ( arr , N ) ; } }


"Maximum sum possible for a sub | C # implementation of the approach ; Function to return the maximum sum possible ; dp [ i ] represent the maximum sum so far after reaching current position i ; Initialize dp [ 0 ] ; Initialize the dp values till k since any two elements included in the sub - sequence must be atleast k indices apart , and thus first element and second element will be k indices apart ; Fill remaining positions ; Return the maximum sum ; Driver code"
Solution in C#:

using System ; using System . Linq ; class GFG { static int maxSum ( int [ ] arr , int k , int n ) { if ( n == 0 ) return 0 ; if ( n == 1 ) return arr [ 0 ] ; if ( n == 2 ) return Math . Max ( arr [ 0 ] , arr [ 1 ] ) ; int [ ] dp = new int [ n ] ; dp [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i <= k ; i ++ ) dp [ i ] = Math . Max ( arr [ i ] , dp [ i - 1 ] ) ; for ( int i = k + 1 ; i < n ; i ++ ) dp [ i ] = Math . Max ( arr [ i ] , dp [ i - ( k + 1 ) ] + arr [ i ] ) ; int max = dp . Max ( ) ; return max ; } static void Main ( ) { int [ ] arr = { 6 , 7 , 1 , 3 , 8 , 2 , 4 } ; int n = arr . Length ; int k = 2 ; Console . WriteLine ( maxSum ( arr , k , n ) ) ; } }


"Maximum sum possible for every node by including it in a segment of N | C # program to calculate the maximum sum possible for every node by including it in a segment of the N - Ary Tree ; Stores the maximum sum possible for every node by including them in a segment with their successors ; Stores the maximum sum possible for every node by including them in a segment with their ancestors ; Store the maximum sum for every node by including it in a segment with its successors ; Update the maximum sums for each node by including them in a sequence with their ancestors ; Condition to check , if current node is not root ; Add edges ; Function to find the maximum answer for each node ; Compute the maximum sums with successors ; Store the computed maximums ; Update the maximum sums by including their ancestors ; Print the desired result ; Number of nodes ; Graph ; Add edges ; Weight of each node ; Compute the max sum of segments for each node ; Print the answer for every node"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int [ ] dp1 = new int [ 100005 ] ; static int [ ] dp2 = new int [ 100005 ] ; static void dfs1 ( int u , int par , List < List < int > > g , int [ ] weight ) { dp1 [ u ] = weight [ u ] ; for ( int c = 0 ; c < g [ u ] . Count ; c ++ ) { if ( g [ u ] != par ) { dfs1 ( g [ u ] , u , g , weight ) ; dp1 [ u ] += Math . Max ( 0 , dp1 [ g [ u ] ] ) ; } } } static void dfs2 ( int u , int par , List < List < int > > g , int [ ] weight ) { if ( par != 0 ) { int maxSumAncestors = dp2 [ par ] - Math . Max ( 0 , dp1 [ u ] ) ; dp2 [ u ] = dp1 [ u ] + Math . Max ( 0 , maxSumAncestors ) ; } for ( int c = 0 ; c < g [ u ] . Count ; c ++ ) { if ( g [ u ] != par ) { dfs2 ( g [ u ] , u , g , weight ) ; } } } static void addEdge ( int u , int v , List < List < int > > g ) { g [ u ] . Add ( v ) ; g [ v ] . Add ( u ) ; } static void maxSumSegments ( List < List < int > > g , int [ ] weight , int n ) { dfs1 ( 1 , 0 , g , weight ) ; for ( int i = 1 ; i < n + 1 ; i ++ ) dp2 [ i ] = dp1 [ i ] ; dfs2 ( 1 , 0 , g , weight ) ; } static void printAns ( int n ) { for ( int i = 1 ; i < n ; i ++ ) Console . Write ( dp2 [ i ] + " ▁ " ) ; } static void Main ( ) { int n = 7 ; List < List < int > > g = new List < List < int > > ( ) ; for ( int i = 0 ; i < 100005 ; i ++ ) { g . Add ( new List < int > ( ) ) ; } addEdge ( 1 , 2 , g ) ; addEdge ( 1 , 3 , g ) ; addEdge ( 2 , 4 , g ) ; addEdge ( 2 , 5 , g ) ; addEdge ( 3 , 6 , g ) ; addEdge ( 4 , 7 , g ) ; int [ ] weight = new int [ n + 1 ] ; weight [ 1 ] = - 8 ; weight [ 2 ] = 9 ; weight [ 3 ] = 7 ; weight [ 4 ] = - 4 ; weight [ 5 ] = 5 ; weight [ 6 ] = - 10 ; weight [ 7 ] = - 6 ; maxSumSegments ( g , weight , n ) ; printAns ( n ) ; } }


"Maximum sum subarray after altering the array | C # implementation of the approach ; Function that returns true if all the array element are <= 0 ; If any element is non - negative ; Function to return the vector representing the right to left Kadane array as described in the approach ; Function to return the prefix_sum vector ; Function to return the maximum sum subarray ; Function to get the maximum sum subarray in the modified array ; kadane_r_to_l [ i ] will store the maximum subarray sum for thre subarray arr [ i ... N - 1 ] ; Get the prefix sum array ; To get max_prefix_sum_at_any_index ; Summation of both gives the maximum subarray sum after applying the operation ; Function to return the maximum subarray sum after performing the given operation at most once ; If all element are negative then return the maximum element ; Maximum subarray sum without performing any operation ; Maximum subarray sum after performing the operations of first type ; Reversing the array to use the same existing function for operations of the second type ; Driver code"
Solution in C#:

using System . Collections . Generic ; using System ; class GFG { static bool areAllNegative ( int [ ] arr ) { int n = arr . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) return false ; } return true ; } static int [ ] getRightToLeftKadane ( int [ ] arr ) { int max_so_far = 0 , max_ending_here = 0 ; int size = arr . Length ; int [ ] new_arr = new int [ size ] ; for ( int i = 0 ; i < size ; i ++ ) new_arr [ i ] = arr [ i ] ; for ( int i = size - 1 ; i >= 0 ; i -- ) { max_ending_here = max_ending_here + new_arr [ i ] ; if ( max_ending_here < 0 ) max_ending_here = 0 ; else if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; new_arr [ i ] = max_so_far ; } return new_arr ; } static int [ ] getPrefixSum ( int [ ] arr ) { int n = arr . Length ; int [ ] new_arr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) new_arr [ i ] = arr [ i ] ; for ( int i = 1 ; i < n ; i ++ ) new_arr [ i ] = new_arr [ i - 1 ] + new_arr [ i ] ; return new_arr ; } static int maxSumSubArr ( int [ ] a ) { int max_so_far = 0 , max_ending_here = 0 ; int n = a . Length ; for ( int i = 0 ; i < n ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_ending_here < 0 ) max_ending_here = 0 ; else if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; } return max_so_far ; } static int maxSumSubWithOp ( int [ ] arr ) { int [ ] kadane_r_to_l = getRightToLeftKadane ( arr ) ; int size = arr . Length ; int [ ] prefixSum = getPrefixSum ( arr ) ; for ( int i = 1 ; i < size ; i ++ ) { prefixSum [ i ] = Math . Max ( prefixSum [ i - 1 ] , prefixSum [ i ] ) ; } int max_subarray_sum = 0 ; for ( int i = 0 ; i < size - 1 ; i ++ ) { max_subarray_sum = Math . Max ( max_subarray_sum , prefixSum [ i ] + kadane_r_to_l [ i + 1 ] ) ; } return max_subarray_sum ; } static int maxSum ( int [ ] arr , int size ) { if ( areAllNegative ( arr ) ) { int mx = - 1000000000 ; for ( int i = 0 ; i < size ; i ++ ) { if ( arr [ i ] > mx ) mx = arr [ i ] ; } return mx ; } int resSum = maxSumSubArr ( arr ) ; resSum = Math . Max (...


"Maximum sum subarray having sum less than or equal to given sum using Set | Java program to find maximum sum subarray less than K ; Function to maximum required sum < K ; Hash to lookup for value ( cum_sum - K ) ; Getting cummulative sum from [ 0 to i ] ; Lookup for upperbound of ( cSum - K ) in hash ; Check if upper_bound of ( cSum - K ) exists then update max sum ; Insert cummulative value in hash ; Return maximum sum lesser than K ; x is the target value or key ; Driver code ; Initialise the array ; Initialise the value of K ; Size of array"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maxSubarraySum ( int [ ] arr , int N , int K ) { HashSet < int > cum_set = new HashSet < int > ( ) ; cum_set . Add ( 0 ) ; int max_sum = Int32 . MinValue , cSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cSum += arr [ i ] ; List < int > al = new List < int > ( ) ; int end = 0 ; foreach ( int it in cum_set ) { end = it ; al . Add ( it ) ; } al . Sort ( ) ; int sit = lower_bound ( al , cSum - K ) ; if ( sit != end ) max_sum = Math . Max ( max_sum , cSum - sit ) ; cum_set . Add ( cSum ) ; } return max_sum ; } static int lower_bound ( List < int > al , int x ) { int l = - 1 , r = al . Count ; while ( l + 1 < r ) { int m = ( l + r ) >> 1 ; if ( al [ m ] >= x ) r = m ; else l = m ; } return r ; } public static void Main ( string [ ] args ) { int [ ] arr = { 5 , - 2 , 6 , 3 , - 5 } ; int K = 15 ; int N = arr . Length ; Console . Write ( maxSubarraySum ( arr , N , K ) ) ; } }


"Maximum sum subarray of size K with sum less than X | C # program for the above approach ; Function to calculate maximum sum among all subarrays of size K with the sum less than X ; Initialize sum_K to 0 ; Calculate sum of first K elements ; If sum_K is less than X ; Initialize MaxSum with sum_K ; Iterate over the array from ( K + 1 ) - th index ; Subtract the first element from the previous K elements and add the next element ; If sum_K is less than X ; Update the Max_Sum ; Driver Code ; Size of Array ; Function Call"
Solution in C#:

using System ; class GFG { private static void maxSumSubarr ( int [ ] A , int N , int K , int X ) { int sum_K = 0 ; for ( int i = 0 ; i < K ; i ++ ) { sum_K += A [ i ] ; } int Max_Sum = 0 ; if ( sum_K < X ) { Max_Sum = sum_K ; } for ( int i = K ; i < N ; i ++ ) { sum_K -= ( A [ i - K ] - A [ i ] ) ; if ( sum_K < X ) { Max_Sum = Math . Max ( Max_Sum , sum_K ) ; } } Console . WriteLine ( Max_Sum ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { - 5 , 8 , 7 , 2 , 10 , 1 , 20 , - 4 , 6 , 9 } ; int K = 5 ; int X = 30 ; int N = arr . Length ; maxSumSubarr ( arr , N , K , X ) ; } }


"Maximum sum subarray of size range [ L , R ] | C # program to find Maximum sum subarray of size between L and R . ; function to find Maximum sum subarray of size between L and R ; calculating prefix sum ; maintain 0 for initial values of i upto R Once i = R , then we need to erase that 0 from our multiset as our first index of subarray cannot be 0 anymore . ; we maintain flag to counter if that initial 0 was erased from set or not . ; erase 0 from multiset once i = b ; insert pre [ i - L ] ; find minimum value in multiset . ; erase pre [ i - R ] ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void max_sum_subarray ( List < int > arr , int L , int R ) { int n = arr . Count ; int [ ] pre = new int [ n ] ; pre [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { pre [ i ] = pre [ i - 1 ] + arr [ i ] ; } List < int > s1 = new List < int > ( ) ; s1 . Add ( 0 ) ; int ans = Int32 . MinValue ; ans = Math . Max ( ans , pre [ L - 1 ] ) ; int flag = 0 ; for ( int i = L ; i < n ; i ++ ) { if ( i - R >= 0 ) { if ( flag == 0 ) { int it = s1 . IndexOf ( 0 ) ; s1 . RemoveAt ( it ) ; flag = 1 ; } } if ( i - L >= 0 ) s1 . Add ( pre [ i - L ] ) ; ans = Math . Max ( ans , pre [ i ] - s1 [ 0 ] ) ; if ( i - R >= 0 ) { int it = s1 . IndexOf ( pre [ i - R ] ) ; s1 . RemoveAt ( it ) ; } } Console . WriteLine ( ans ) ; } static void Main ( ) { int L , R ; L = 1 ; R = 3 ; List < int > arr = new List < int > ( ) { 1 , 2 , 2 , 1 } ; max_sum_subarray ( arr , L , R ) ; } }


"Maximum sum subarray removing at most one element | C # program to get maximum sum subarray removing at - most one element ; Method returns maximum sum of all subarray where removing one element is also allowed ; Maximum sum subarrays in forward and backward directions ; Initialize current max and max so far . ; calculating maximum sum subarrays in forward direction ; storing current maximum till ith , in forward array ; calculating maximum sum subarrays in backward direction ; storing current maximum from ith , in backward array ; Initializing final ans by max_so_far so that , case when no element is removed to get max sum subarray is also handled ; choosing maximum ignoring ith element ; Driver code"
Solution in C#:

using System ; class GFG { static int maxSumSubarrayRemovingOneEle ( int [ ] arr , int n ) { int [ ] fw = new int [ n ] ; int [ ] bw = new int [ n ] ; int cur_max = arr [ 0 ] , max_so_far = arr [ 0 ] ; fw [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { cur_max = Math . Max ( arr [ i ] , cur_max + arr [ i ] ) ; max_so_far = Math . Max ( max_so_far , cur_max ) ; fw [ i ] = cur_max ; } cur_max = max_so_far = bw [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { cur_max = Math . Max ( arr [ i ] , cur_max + arr [ i ] ) ; max_so_far = Math . Max ( max_so_far , cur_max ) ; bw [ i ] = cur_max ; } int fans = max_so_far ; for ( int i = 1 ; i < n - 1 ; i ++ ) fans = Math . Max ( fans , fw [ i - 1 ] + bw [ i + 1 ] ) ; return fans ; } public static void Main ( ) { int [ ] arr = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; int n = arr . Length ; Console . WriteLine ( maxSumSubarrayRemovingOneEle ( arr , n ) ) ; } }


"Maximum sum submatrix | C # program for the above approach ; Function to find maximum continuous maximum sum in the array ; Stores current and maximum sum ; Traverse the array v ; Add the value of the current element ; Update the maximum sum ; Return the maximum sum ; Function to find the maximum submatrix sum ; Store the rows and columns of the matrix ; Create an auxiliary matrix ; Traverse the matrix , prefix and initialize it will all 0 s ; Calculate prefix sum of all rows of matrix [ , ] A and store in matrix prefix [ ] ; Update the prefix [ , ] ; Store the maximum submatrix sum ; Iterate for starting column ; Iterate for last column ; To store current array elements ; Traverse every row ; Store the sum of the kth row ; Update the prefix sum ; Push it in a vector ; Update the maximum overall sum ; Print the answer ; Driver Code ; Function Call"
Solution in C#:

 using System ; using System . Collections . Generic ; public class GFG { static int kadane ( List < int > v ) { int currSum = 0 ; int maxSum = int . MinValue ; for ( int i = 0 ; i < ( int ) v . Count ; i ++ ) { currSum += v [ i ] ; if ( currSum > maxSum ) { maxSum = currSum ; } if ( currSum < 0 ) { currSum = 0 ; } } return maxSum ; } static void maxSubmatrixSum ( int [ , ] A ) { int r = A . GetLength ( 0 ) ; int c = A . GetLength ( 1 ) ; int [ , ] prefix = new int [ r , c ] ; for ( int i = 0 ; i < r ; i ++ ) { for ( int j = 0 ; j < c ; j ++ ) { prefix [ i , j ] = 0 ; } } for ( int i = 0 ; i < r ; i ++ ) { for ( int j = 0 ; j < c ; j ++ ) { if ( j == 0 ) prefix [ i , j ] = A [ i , j ] ; else prefix [ i , j ] = A [ i , j ] + prefix [ i , j - 1 ] ; } } int maxSum = int . MinValue ; for ( int i = 0 ; i < c ; i ++ ) { for ( int j = i ; j < c ; j ++ ) { List < int > v = new List < int > ( ) ; for ( int k = 0 ; k < r ; k ++ ) { int el = 0 ; if ( i == 0 ) el = prefix [ k , j ] ; else el = prefix [ k , j ] - prefix [ k , i - 1 ] ; v . Add ( el ) ; } maxSum = Math . Max ( maxSum , kadane ( v ) ) ; } } Console . Write ( maxSum + "
 " ) ; } public static void Main ( String [ ] args ) { int [ , ] matrix = { { 0 , - 2 , - 7 , 0 } , { 9 , 2 , - 6 , 2 } , { - 4 , 1 , - 4 , 1 } , { - 1 , 8 , 0 , - 2 } } ; maxSubmatrixSum ( matrix ) ; } }


"Maximum sum submatrix | C # program to implement the above approach ; Function to find maximum sum submatrix ; Stores the number of rows and columns in the matrix ; Stores maximum submatrix sum ; Take each row as starting row ; Take each column as the starting column ; Take each row as the ending row ; Take each column as the ending column ; Stores the sum of submatrix having topleft index ( i , j ) and bottom right index ( k , l ) ; Iterate the submatrix row - wise and calculate its sum ; Update the maximum sum ; Print the answer ; Driver Code"
Solution in C#:

using System ; public class GFG { static void maxSubmatrixSum ( int [ , ] matrix ) { int r = matrix . GetLength ( 0 ) ; int c = matrix . GetLength ( 1 ) ; int maxSubmatrix = 0 ; for ( int i = 0 ; i < r ; i ++ ) { for ( int j = 0 ; j < c ; j ++ ) { for ( int k = i ; k < r ; k ++ ) { for ( int l = j ; l < c ; l ++ ) { int sumSubmatrix = 0 ; for ( int m = i ; m <= k ; m ++ ) { for ( int n = j ; n <= l ; n ++ ) { sumSubmatrix += matrix [ m , n ] ; } } maxSubmatrix = Math . Max ( maxSubmatrix , sumSubmatrix ) ; } } } } Console . WriteLine ( maxSubmatrix ) ; } public static void Main ( String [ ] args ) { int [ , ] matrix = { { 0 , - 2 , - 7 , 0 } , { 9 , 2 , - 6 , 2 } , { - 4 , 1 , - 4 , 1 } , { - 1 , 8 , 0 , - 2 } } ; maxSubmatrixSum ( matrix ) ; } }


"Maximum sum subsequence with at | C # program to find maximum sum subsequence such that elements are at least k distance away . ; MS [ i ] is going to store maximum sum subsequence in subarray from arr [ i ] to arr [ n - 1 ] ; We fill MS from right to left . ; Driver code"
Solution in C#:

using System ; class GFG { static int maxSum ( int [ ] arr , int N , int k ) { int [ ] MS = new int [ N ] ; MS [ N - 1 ] = arr [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( i + k + 1 >= N ) MS [ i ] = Math . Max ( arr [ i ] , MS [ i + 1 ] ) ; else MS [ i ] = Math . Max ( arr [ i ] + MS [ i + k + 1 ] , MS [ i + 1 ] ) ; } return MS [ 0 ] ; } public static void Main ( ) { int N = 10 , k = 2 ; int [ ] arr = { 50 , 70 , 40 , 50 , 90 , 70 , 60 , 40 , 70 , 50 } ; Console . WriteLine ( maxSum ( arr , N , k ) ) ; } }


"Maximum sum subsequence with values differing by at least 2 | C # program to find maximum sum subsequence with values differing by at least 2 ; Function to find maximum sum subsequence such that two adjacent values elements are not selected ; map to store the frequency of array elements ; Make a dp array to store answer upto i th value ; Base cases ; Iterate for all possible values of arr [ i ] ; Return the last value ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int get_max_sum ( int [ ] arr , int n ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . ContainsKey ( arr [ i ] ) ) { int x = freq [ arr [ i ] ] ; freq [ arr [ i ] ] = x + 1 ; } else freq . Add ( arr [ i ] , 1 ) ; } int [ ] dp = new int [ 100001 ] ; for ( int i = 0 ; i < 100001 ; i ++ ) dp [ i ] = 0 ; dp [ 0 ] = 0 ; if ( freq . ContainsKey ( 0 ) ) dp [ 1 ] = freq [ 0 ] ; else dp [ 1 ] = 0 ; for ( int i = 2 ; i <= 100000 ; i ++ ) { int temp = ( freq . ContainsKey ( i ) ) ? freq [ i ] : 0 ; dp [ i ] = Math . Max ( dp [ i - 1 ] , dp [ i - 2 ] + i * temp ) ; } return dp [ 100000 ] ; } public static void Main ( String [ ] args ) { int N = 3 ; int [ ] arr = { 2 , 2 , 3 } ; Console . WriteLine ( get_max_sum ( arr , N ) ) ; } }


"Maximum sum subset having equal number of positive and negative elements | C # implementation to find the maximum sum subset having equal number of positive and negative elements in the subset ; Function to find maximum sum subset with equal number of positive and negative elements ; Loop to store the positive and negative elements in two different array ; Sort both the array ; Pointers starting from the highest elements ; Find pairs having sum greater than zero ; Driver code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static int findMaxSum ( int [ ] arr , int n ) { List < int > a = new List < int > ( ) ; List < int > b = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) { a . Add ( arr [ i ] ) ; } else if ( arr [ i ] < 0 ) { b . Add ( arr [ i ] ) ; } } a . Sort ( ) ; b . Sort ( ) ; int p = a . Count - 1 ; int q = b . Count - 1 ; int s = 0 ; while ( p >= 0 && q >= 0 ) { if ( a [ p ] + b [ q ] > 0 ) { s = s + a [ p ] + b [ q ] ; } else { break ; } p = p - 1 ; q = q - 1 ; } return s ; } public static void Main ( String [ ] args ) { int [ ] arr1 = { 1 , - 2 , 3 , 4 , - 5 , 8 } ; int n1 = arr1 . Length ; Console . Write ( findMaxSum ( arr1 , n1 ) + "
 " ) ; } }


"Maximum sum such that exactly half of the elements are selected and no two adjacent | C # program to find maximum sum possible such that exactly Math . Floor ( N / 2 ) elements are selected and no two selected elements are adjacent to each other ; Function return the maximum sum possible under given condition ; Base case ; When i is odd ; When i is even ; Maximum of if we pick last element or not ; Driver code"
Solution in C#:

using System ; class GFG { static int MaximumSum ( int [ ] a , int n ) { int [ , ] dp = new int [ n + 1 , 2 ] ; dp [ 2 , 1 ] = a [ 1 ] ; dp [ 2 , 0 ] = a [ 0 ] ; for ( int i = 3 ; i < n + 1 ; i ++ ) { if ( i % 2 == 1 ) { int temp = Math . Max ( ( Math . Max ( dp [ i - 3 , 1 ] , dp [ i - 3 , 0 ] ) ) , Math . Max ( dp [ i - 2 , 1 ] , dp [ i - 2 , 0 ] ) ) ; dp [ i , 1 ] = a [ i - 1 ] + temp ; dp [ i , 0 ] = Math . Max ( ( Math . Max ( a [ i - 2 ] + dp [ i - 2 , 0 ] , a [ i - 2 ] + dp [ i - 3 , 1 ] ) ) , Math . Max ( a [ i - 2 ] + dp [ i - 3 , 0 ] , a [ i - 3 ] + dp [ i - 3 , 0 ] ) ) ; } else { dp [ i , 1 ] = a [ i - 1 ] + ( Math . Max ( ( Math . Max ( dp [ i - 2 , 1 ] , dp [ i - 2 , 0 ] ) ) , dp [ i - 1 , 0 ] ) ) ; dp [ i , 0 ] = a [ i - 2 ] + dp [ i - 2 , 0 ] ; } } return Math . Max ( dp [ n , 1 ] , dp [ n , 0 ] ) ; } static int max ( int [ ] arr ) { return 1 ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = A . Length ; Console . Write ( MaximumSum ( A , N ) ) ; } }


"Maximum sum such that exactly half of the elements are selected and no two adjacent | C # program to find maximum sum possible such that exactly Math . floor ( N / 2 ) elements are selected and no two selected elements are adjacent to each other ; Function return the maximum sum possible under given condition ; Intitialising the dp table ; Base case ; Condition to select the current element ; Condition to not select the current element if possible ; Driver code"
Solution in C#:

using System ; class GFG { static int MaximumSum ( int [ ] a , int n ) { int [ , ] dp = new int [ n + 1 , n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < n + 1 ; j ++ ) dp [ i , j ] = Int32 . MinValue ; } for ( int i = 0 ; i < n + 1 ; i ++ ) dp [ i , 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { int val = Int32 . MinValue ; if ( ( i - 2 >= 0 && dp [ i - 2 , j - 1 ] != Int32 . MinValue ) i - 2 < 0 ) { val = a [ i - 1 ] + ( i - 2 >= 0 ? dp [ i - 2 , j - 1 ] : 0 ) ; } if ( i - 1 >= j ) { val = Math . Max ( val , dp [ i - 1 , j ] ) ; } dp [ i , j ] = val ; } } return dp [ n , n / 2 ] ; } public static void Main ( ) { int [ ] A = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = A . Length ; Console . Write ( MaximumSum ( A , N ) ) ; } }


"Maximum sum such that no two elements are adjacent | Set 2 | C # program to implement above approach ; variable to store states of dp ; variable to check if a given state has been solved ; Function to find the maximum sum subsequence such that no two elements are adjacent ; Base case ; To check if a state has been solved ; Required recurrence relation ; Returning the value ; Driver code"
Solution in C#:

using System ; class GFG { static int maxLen = 10 ; static int [ ] dp = new int [ maxLen ] ; static bool [ ] v = new bool [ maxLen ] ; static int maxSum ( int [ ] arr , int i , int n ) { if ( i >= n ) return 0 ; if ( v [ i ] ) return dp [ i ] ; v [ i ] = true ; dp [ i ] = Math . Max ( maxSum ( arr , i + 1 , n ) , arr [ i ] + maxSum ( arr , i + 2 , n ) ) ; return dp [ i ] ; } public static void Main ( ) { int [ ] arr = { 12 , 9 , 7 , 33 } ; int n = arr . Length ; Console . Write ( maxSum ( arr , 0 , n ) ) ; } }


"Maximum time in HH : MM : SS format that can be represented by given six digits | C # Program of the above approach ; Function to return the maximum possible time in 24 - Hours format that can be represented by array elements ; Stores the frequency of the array elements ; Maximum possible time ; Iterate to minimum possible time ; Conditions to reduce the the time iteratively ; If all required digits are present in the Map ; Retrieve Original Count ; If seconds is reduced to 0 ; If minutes is reduced to 0 ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static String largestTimeFromDigits ( int [ ] A ) { Dictionary < int , int > mp1 = new Dictionary < int , int > ( ) ; Dictionary < int , int > mp2 = new Dictionary < int , int > ( ) ; foreach ( int x in A ) { if ( mp1 . ContainsKey ( x ) ) mp1 [ x ] = mp1 [ x ] + 1 ; else mp1 . Add ( x , 1 ) ; } mp2 = new Dictionary < int , int > ( mp1 ) ; int hr = 23 , m = 59 , s = 59 ; while ( hr >= 0 ) { int h0 = hr / 10 , h1 = hr % 10 ; int m0 = m / 10 , m1 = m % 10 ; int s0 = s / 10 , s1 = s % 10 ; int p = 0 ; int [ ] arr = { h0 , h1 , m0 , m1 , s0 , s1 } ; foreach ( int it in arr ) { if ( mp1 . ContainsKey ( it ) && mp1 [ it ] > 0 ) { mp1 [ it ] = mp1 [ it ] - 1 ; } else { p = 1 ; } } if ( p == 0 ) { String st = " " ; st = String . Join ( " " , h0 ) + String . Join ( " " , h1 ) ; st += ' : ' + String . Join ( " " , m0 ) + String . Join ( " " , m1 ) ; st += ' : ' + String . Join ( " " , s0 ) + String . Join ( " " , s1 ) ; return st ; } mp1 = new Dictionary < int , int > ( mp2 ) ; if ( s == 0 ) { s = 59 ; m -- ; } else if ( m < 0 ) { m = 59 ; hr -- ; } if ( s > 0 ) { s -- ; } } return " - 1" ; } public static void Main ( String [ ] args ) { int [ ] v = { 0 , 2 , 1 , 9 , 3 , 2 } ; Console . Write ( largestTimeFromDigits ( v ) ) ; } }


"Maximum times X and Y can be reduced to near 0 using numbers A or B | C # program to implement the above approach ; Helper function to check if we can perform Mid number of moves ; Remove atleast Mid * B from both X and Y ; If any value is negative return false . ; Calculate remaining values ; If val >= Mid then it is possible to perform this much moves ; Else return false ; Initialize a variable to store the answer ; Fix the left and right range ; Binary Search over the answer ; Check for the middle value as the answer ; It is possible to perform this much moves ; Maximise the answer ; Return answer ; Driver Code ; Generalise that A >= B"
Solution in C#:

using System ; class GFG { static int MAXN = 10000000 ; static Boolean can ( int Mid , int X , int Y , int A , int B ) { int p1 = X - Mid * B ; int p2 = Y - Mid * B ; if ( p1 < 0 p2 < 0 ) { return false ; } int k = A - B ; if ( k == 0 ) { return true ; } int val = p1 / k + p2 / k ; if ( val >= Mid ) { return true ; } return false ; } static int maxPossibleMoves ( int X , int Y , int A , int B ) { int ans = 0 ; int L = 1 , R = MAXN ; while ( L <= R ) { int Mid = ( L + R ) / 2 ; if ( can ( Mid , X , Y , A , B ) ) { L = Mid + 1 ; ans = Math . Max ( ans , Mid ) ; } else { R = Mid - 1 ; } } return ans ; } public static void Main ( String [ ] args ) { int X = 10 , Y = 12 , A = 2 , B = 5 ; if ( A < B ) { int temp = A ; A = B ; B = temp ; } Console . Write ( maxPossibleMoves ( X , Y , A , B ) ) ; } }


"Maximum trace possible for any sub | C # program for the above approach ; Function to return the maximum trace possible for a sub - matrix of the given matrix ; Calculate the trace for each of the sub - matrix with top left corner at cell ( r , s ) ; Update the maximum trace ; Return the maximum trace ; Driver code"
Solution in C#:

using System ; class GFG { static int N = 3 ; static int MaxTraceSub ( int [ ] [ ] mat ) { int max_trace = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { int r = i , s = j , trace = 0 ; while ( r < N && s < N ) { trace += mat [ r ] [ s ] ; r ++ ; s ++ ; max_trace = Math . Max ( trace , max_trace ) ; } } } return max_trace ; } public static void Main ( ) { int [ ] [ ] mat = new int [ ] [ ] { new int [ ] { 10 , 2 , 5 } , new int [ ] { 6 , 10 , 4 } , new int [ ] { 2 , 7 , - 10 } } ; Console . WriteLine ( MaxTraceSub ( mat ) ) ; } }


"Maximum value at each level in an N | C # program for the above approach ; Function to find the maximum value at each level of N - ary tree ; Stores the adjacency list ; Create the adjacency list ; Perform level order traversal of nodes at each level ; Push the root node ; Iterate until queue is empty ; Get the size of queue ; Iterate for all the nodes in the queue currently ; Dequeue an node from queue ; Enqueue the children of dequeued node ; Print the result ; Driver Code ; Number of nodes ; Edges of the N - ary tree ; Given cost ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void maxAtLevel ( int N , int M , int [ ] Value , int [ , ] Edges ) { List < int > [ ] adj = new List < int > [ N ] ; for ( int i = 0 ; i < adj . Length ; i ++ ) adj [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < M ; i ++ ) { int u = Edges [ i , 0 ] ; int v = Edges [ i , 1 ] ; adj [ u ] . Add ( v ) ; } Queue < int > q = new Queue < int > ( ) ; q . Enqueue ( 0 ) ; while ( q . Count != 0 ) { int count = q . Count ; int maxVal = 0 ; while ( count -- > 0 ) { int temp = q . Peek ( ) ; q . Dequeue ( ) ; maxVal = Math . Max ( maxVal , Value [ temp ] ) ; for ( int i = 0 ; i < adj [ temp ] . Count ; i ++ ) { q . Enqueue ( adj [ temp ] [ i ] ) ; } } Console . Write ( maxVal + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int N = 10 ; int [ , ] Edges = { { 0 , 1 } , { 0 , 2 } , { 0 , 3 } , { 1 , 4 } , { 1 , 5 } , { 3 , 6 } , { 6 , 7 } , { 6 , 8 } , { 6 , 9 } } ; int [ ] Value = { 1 , 2 , - 1 , 3 , 4 , 5 , 8 , 6 , 12 , 7 } ; maxAtLevel ( N , N - 1 , Value , Edges ) ; } }


"Maximum value in an array after m range increment operations | C # implementation of simple approach to find maximum value after m range increments . ; Function to find the maximum element after m operations ; Start performing m operations ; Store lower and upper index i . e . range ; Add ' k [ i ] ' value at this operation to whole range ; Find maximum value after all operations and return ; Driver Code ; Number of values ; Value of k to be added at each operation"
Solution in C#:

using System ; public class GFG { static int findMax ( int n , int [ ] a , int [ ] b , int [ ] k , int m ) { int [ ] arr = new int [ n ] ; for ( int i = 0 ; i < m ; i ++ ) { int lowerbound = a [ i ] ; int upperbound = b [ i ] ; for ( int j = lowerbound ; j <= upperbound ; j ++ ) arr [ j ] += k [ i ] ; } int res = Int32 . MinValue ; for ( int i = 0 ; i < n ; i ++ ) res = Math . Max ( res , arr [ i ] ) ; return res ; } static public void Main ( ) { int n = 5 ; int [ ] a = { 0 , 1 , 2 } ; int [ ] b = { 1 , 4 , 3 } ; int [ ] k = { 100 , 100 , 100 } ; int m = a . Length ; Console . WriteLine ( " Maximum ▁ value ▁ after ▁ ' m ' ▁ " + " operations ▁ is ▁ " + findMax ( n , a , b , k , m ) ) ; } }


"Maximum value obtained by performing given operations in an Array | C # implementation of the above approach ; A function to calculate the maximum value ; basecases ; Loop to iterate and add the max value in the dp array ; Driver Code"
Solution in C#:


"Maximum value of ( arr [ i ] * arr [ j ] ) + ( arr [ j ] | C # program for the above approach ; Function to find the value of the expression a * b + ( b - a ) ; Function to find the maximum value of the expression a * b + ( b - a ) possible for any pair ( a , b ) ; Sort the vector in ascending order ; Stores the maximum value ; Update ans by choosing the pair of the minimum and 2 nd minimum ; Update ans by choosing the pair of maximum and 2 nd maximum ; Return the value of ans ; Driver Code ; Given inputs"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int calc ( int a , int b ) { return a * b + ( b - a ) ; } static int findMaximum ( List < int > arr , int N ) { arr . Sort ( ) ; int ans = - 1000000000 ; ans = Math . Max ( ans , calc ( arr [ 0 ] , arr [ 1 ] ) ) ; ans = Math . Max ( ans , calc ( arr [ N - 2 ] , arr [ N - 1 ] ) ) ; return ans ; } public static void Main ( ) { List < int > arr = new List < int > { 0 , - 47 , 12 } ; int N = ( int ) arr . Count ; Console . Write ( findMaximum ( arr , N ) ) ; } }


"Maximum value of B less than A such that A ^ B = A + B | Function to find the maximum value of B such that A ^ B = A + B ; Binary Representation of A ; Loop to find the negation of the integer A ; output ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void maxValue ( int a ) { String c = Convert . ToString ( a , 2 ) ; String b = " " ; for ( int i = 0 ; i < c . Length ; i ++ ) { if ( ( c [ i ] - '0' ) == 1 ) b += '0' ; else b += '1' ; } Console . Write ( Convert . ToInt32 ( b , 2 ) ) ; } public static void Main ( String [ ] args ) { int a = 4 ; maxValue ( a ) ; } }


"Maximum value of X such that difference between any array element and X does not exceed K | C # program for the above approach ; Function to find maximum value of X such that | A [ i ] - X | a K ; Stores the smallest array element ; Store the possible value of X ; Traverse the array A [ ] ; If required criteria is not satisfied ; Update ans ; Print the result ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void maximumNumber ( int [ ] arr , int N , int K ) { int mn = 100000000 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < mn ) mn = arr [ i ] ; } int ans = mn + K ; for ( int i = 0 ; i < N ; i ++ ) { if ( Math . Abs ( arr [ i ] - ans ) > K ) { ans = - 1 ; break ; } } Console . Write ( ans ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 5 } ; int K = 2 ; int N = arr . Length ; maximumNumber ( arr , N , K ) ; } }


"Maximum value of an integer for which factorial can be calculated on a machine | C # program to find maximum value of an integer for which factorial can be calculated on your system ; when fact crosses its size , it gives negative value ; Driver Code"
Solution in C#:

using System ; class GFG { public static int findMaxValue ( ) { int res = 2 ; long fact = 2 ; while ( true ) { if ( fact < 0 ) break ; res ++ ; fact = fact * res ; } return res - 1 ; } public static void Main ( ) { Console . Write ( " Maximum ▁ value ▁ of " + " ▁ integer ▁ " + findMaxValue ( ) ) ; } }


"Maximum value of arr [ i ] + arr [ j ] + i â €“ j for any pair of an array | C # program for the above approach ; Function to find the maximum value of arr [ i ] + arr [ j ] + i - j over all pairs ; Stores the maximum value of ( arr [ i ] + i ) ; Stores the required result ; Traverse the array arr [ ] ; Calculate for current pair and update maximum value ; Update maxValue if ( arr [ i ] + I ) is greater than maxValue ; Print the result ; Driver code"
Solution in C#:

using System ; class GFG { static void maximumValue ( int [ ] arr , int n ) { int maxvalue = arr [ 0 ] ; int result = 0 ; for ( int i = 1 ; i < n ; i ++ ) { result = Math . Max ( result , maxvalue + arr [ i ] - i ) ; maxvalue = Math . Max ( maxvalue , arr [ i ] + i ) ; } Console . Write ( result ) ; } static void Main ( ) { int [ ] arr = { 1 , 9 , 3 , 6 , 5 } ; int N = arr . Length ; maximumValue ( arr , N ) ; } }


"Maximum value of arr [ i ] + arr [ j ] + i â €“ j for any pair of an array | C # program for the above approach ; Function to find the maximum value of arr [ i ] + arr [ j ] + i - j over all pairs ; Stores the required result ; Traverse over all the pairs ( i , j ) ; Calculate the value of the expression and update the overall maximum value ; Print the result ; Driver code"
Solution in C#:

using System ; class GFG { static void maximumValue ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { ans = Math . Max ( ans , arr [ i ] + arr [ j ] + i - j ) ; } } Console . Write ( ans ) ; } static void Main ( ) { int [ ] arr = { 1 , 9 , 3 , 6 , 5 } ; int N = arr . Length ; maximumValue ( arr , N ) ; } }


"Maximum value of division of two numbers in an Array | C # program to maximum value of division of two numbers in an array ; Function to maximum value of division of two numbers in an array ; Traverse through the array ; Return the required answer ; Driver code"
Solution in C#:

using System ; class GFG { static int Division ( int [ ] a , int n ) { int maxi = int . MinValue , mini = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { maxi = Math . Max ( a [ i ] , maxi ) ; mini = Math . Min ( a [ i ] , mini ) ; } return maxi / mini ; } public static void Main ( String [ ] args ) { int [ ] a = { 3 , 7 , 9 , 3 , 11 } ; int n = a . Length ; Console . WriteLine ( Division ( a , n ) ) ; } }


"Maximum value of expression ( arr [ i ] + arr [ j ] * arr [ k ] ) formed from a valid Triplet | C # program for the above approach ; Function that generate all valid triplets and calculate the value of the valid triplets ; Generate all triplets ; Check whether the triplet is valid or not ; Update the value ; Print the maximum value ; Driver Code ; Given array [ ] arr ; Function Call"
Solution in C#:

using System ; class GFG { static void max_valid_triplet ( int [ ] A , int n ) { int ans = - 1 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { for ( int j = i + 1 ; j < n - 1 ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { if ( A [ i ] < A [ j ] && A [ j ] < A [ k ] ) { int value = A [ i ] + A [ j ] * A [ k ] ; if ( value > ans ) { ans = value ; } } } } } Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { int [ ] arr = new int [ ] { 7 , 9 , 3 , 8 , 11 , 10 } ; int n = arr . Length ; max_valid_triplet ( arr , n ) ; } }


"Maximum value of | arr [ 0 ] | C # implementation of the approach ; Function to return the maximum required value ; Driver code"
Solution in C#:

using System ; class GFG { static int maxValue ( int n ) { if ( n == 1 ) return 0 ; return ( ( n * n / 2 ) - 1 ) ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( maxValue ( n ) ) ; } }


"Maximum value of | arr [ i ] | C # program to find maximum value of | arr [ i ] - arr [ j ] | + | i - j | ; Return maximum value of | arr [ i ] - arr [ j ] | + | i - j | ; Iterating two for loop , one for i and another for j . ; Evaluating | arr [ i ] - arr [ j ] | + | i - j | and compare with previous maximum . ; Driver code"
Solution in C#:

using System ; class GFG { static int findValue ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) ans = Math . Max ( ans , Math . Abs ( arr [ i ] - arr [ j ] ) + Math . Abs ( i - j ) ) ; return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 1 } ; int n = arr . Length ; Console . Write ( findValue ( arr , n ) ) ; } }


"Maximum value of | arr [ i ] | Efficient Java program to find maximum value of | arr [ i ] - arr [ j ] | + | i - j | ; Return maximum | arr [ i ] - arr [ j ] | + | i - j | ; Calculating first_array and second_array ; Finding maximum and minimum value in first_array ; Storing the difference between maximum and minimum value in first_array ; Finding maximum and minimum value in second_array ; Storing the difference between maximum and minimum value in second_array ; Driver Code"
Solution in C#:

using System ; class GFG { static int findValue ( int [ ] arr , int n ) { int [ ] a = new int [ n ] ; int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = ( arr [ i ] + i ) ; b [ i ] = ( arr [ i ] - i ) ; } int x = a [ 0 ] , y = a [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > x ) x = a [ i ] ; if ( a [ i ] < y ) y = a [ i ] ; } int ans1 = ( x - y ) ; x = b [ 0 ] ; y = b [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( b [ i ] > x ) x = b [ i ] ; if ( b [ i ] < y ) y = b [ i ] ; } int ans2 = ( x - y ) ; return Math . Max ( ans1 , ans2 ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 1 } ; int n = arr . Length ; Console . WriteLine ( findValue ( arr , n ) ) ; } }


"Maximum value possible by rotating digits of a given number | C # program for the above approach ; Function to find the maximum value possible by rotations of digits of N ; Store the required result ; Store the number of digits ; Iterate over the range [ 1 , len - 1 ] ; Store the unit 's digit ; Store the remaining number ; Find the next rotation ; If the current rotation is greater than the overall answer , then update answer ; Print the result ; Driver Code"
Solution in C#:

using System ; class GFG { static void findLargestRotation ( int num ) { int ans = num ; double lg = ( double ) ( Math . Log10 ( num ) + 1 ) ; int len = ( int ) ( Math . Floor ( lg ) ) ; int x = ( int ) Math . Pow ( 10 , len - 1 ) ; for ( int i = 1 ; i < len ; i ++ ) { int lastDigit = num % 10 ; num = num / 10 ; num += ( lastDigit * x ) ; if ( num > ans ) { ans = num ; } } Console . Write ( ans ) ; } public static void Main ( string [ ] args ) { int N = 657 ; findLargestRotation ( N ) ; } }


"Maximum value with the choice of either dividing or considering as it is | C # program for maximize result when we have choice to divide or consider as it is . ; function for calculating max possible result ; Compute remaining values in bottom up manner . ; Driver code"
Solution in C#:

using System ; class GFG { static int maxDP ( int n ) { int [ ] res = new int [ n + 1 ] ; res [ 0 ] = 0 ; res [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res [ i ] = Math . Max ( i , ( res [ i / 2 ] + res [ i / 3 ] + res [ i / 4 ] + res [ i / 5 ] ) ) ; return res [ n ] ; } public static void Main ( ) { int n = 60 ; Console . WriteLine ( " MaxSum ▁ = ▁ " + maxDP ( n ) ) ; } }


"Maximum water that can be stored between two buildings | C # implementation of the above approach ; Return the maximum water that can be stored ; Check all possible pairs of buildings ; Maximum so far ; Driver code"
Solution in C#:

using System ; class GFG { static int maxWater ( int [ ] height , int n ) { int max = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int current = ( Math . Min ( height [ i ] , height [ j ] ) * ( j - i - 1 ) ) ; max = Math . Max ( max , current ) ; } } return max ; } static public void Main ( ) { int [ ] height = { 2 , 1 , 3 , 4 , 6 , 5 } ; int n = height . Length ; Console . WriteLine ( maxWater ( height , n ) ) ; } }


"Maximum water that can be stored between two buildings | C # implementation of the approach ; Return the maximum water that can be stored ; To store the maximum water so far ; Both the pointers are pointing at the first and the last buildings respectively ; While the water can be stored between the currently chosen buildings ; Update maximum water so far and increment i ; Update maximum water so far and decrement j ; Any of the pointers can be updated ( or both ) ; Driver code"
Solution in C#:

using System ; class GFG { static int maxWater ( int [ ] height , int n ) { int max = 0 ; int i = 0 , j = n - 1 ; while ( i < j ) { if ( height [ i ] < height [ j ] ) { max = Math . Max ( max , ( j - i - 1 ) * height [ i ] ) ; i ++ ; } else if ( height [ j ] < height [ i ] ) { max = Math . Max ( max , ( j - i - 1 ) * height [ j ] ) ; j -- ; } else { max = Math . Max ( max , ( j - i - 1 ) * height [ i ] ) ; i ++ ; j -- ; } } return max ; } static public void Main ( ) { int [ ] height = { 2 , 1 , 3 , 4 , 6 , 5 } ; int n = height . Length ; Console . Write ( maxWater ( height , n ) ) ; } }


"Maximum weight path ending at any element of last row in a matrix | C # Code for Maximum weight path ending at any element of last row in a matrix ; Function which return the maximum weight path sum ; create 2D matrix to store the sum of the path ; Initialize first column of total weight array ( dp [ i to N ] [ 0 ] ) ; Calculate rest path sum of weight matrix ; find the max weight path sum to reach the last row ; return maximum weight path sum ; Driver program to test above function"
Solution in C#:

using System ; class GFG { public static int maxCost ( int [ , ] mat , int N ) { int [ , ] dp = new int [ N , N ] ; dp [ 0 , 0 ] = mat [ 0 , 0 ] ; for ( int i = 1 ; i < N ; i ++ ) dp [ i , 0 ] = mat [ i , 0 ] + dp [ i - 1 , 0 ] ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 1 ; j < i + 1 && j < N ; j ++ ) dp [ i , j ] = mat [ i , j ] + Math . Max ( dp [ i - 1 , j - 1 ] , dp [ i - 1 , j ] ) ; int result = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( result < dp [ N - 1 , i ] ) result = dp [ N - 1 , i ] ; return result ; } public static void Main ( ) { int [ , ] mat = { { 4 , 1 , 5 , 6 , 1 } , { 2 , 9 , 2 , 11 , 10 } , { 15 , 1 , 3 , 15 , 2 } , { 16 , 92 , 41 , 4 , 3 } , { 8 , 142 , 6 , 4 , 8 } } ; int N = 5 ; Console . Write ( " Maximum ▁ Path ▁ Sum ▁ : ▁ " + maxCost ( mat , N ) ) ; } }


"Maximum weight transformation of a given string | C # program to find maximum weight transformation of a given string ; Returns weight of the maximum weight transformation ; Base case ; If this subproblem is already solved ; Don 't make pair, so  weight gained is 1 ; If we can make pair ; If elements are dissimilar , weight gained is 4 ; if elements are similar so for making a pair we toggle any of them . Since toggle cost is 1 so overall weight gain becomes 3 ; save and return maximum of above cases ; Initializes lookup table and calls getMaxRec ( ) ; Create and initialize lookup table ; Call recursive function ; Driver Code"
Solution in C#:

using System ; class GFG { static int getMaxRec ( string str , int i , int n , int [ ] lookup ) { if ( i >= n ) return 0 ; if ( lookup [ i ] != - 1 ) return lookup [ i ] ; int ans = 1 + getMaxRec ( str , i + 1 , n , lookup ) ; if ( i + 1 < n ) { if ( str [ i ] != str [ i + 1 ] ) ans = Math . Max ( 4 + getMaxRec ( str , i + 2 , n , lookup ) , ans ) ; else ans = Math . Max ( 3 + getMaxRec ( str , i + 2 , n , lookup ) , ans ) ; } return lookup [ i ] = ans ; } static int getMaxWeight ( string str ) { int n = str . Length ; int [ ] lookup = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) lookup [ i ] = - 1 ; return getMaxRec ( str , 0 , str . Length , lookup ) ; } public static void Main ( ) { string str = " AAAAABB " ; Console . Write ( " Maximum ▁ weight ▁ of ▁ a " + " ▁ transformation ▁ of ▁ " + str + " ▁ is ▁ " + getMaxWeight ( str ) ) ; } }


"Maximum weighted edge in path between two nodes in an N | C # implementation to find the maximum weighted edge in the simple path between two nodes in N - ary Tree ; Depths of Nodes ; Parent at every 2 ^ i level ; Maximum node at every 2 ^ i level ; Graph that stores destinations and its weight ; Function to traverse the nodes using the Depth - First Search Traversal ; Condition to check if its equal to its parent then skip ; DFS Recursive Call ; Function to find the ansector ; Loop to set every 2 ^ i distance ; Loop to calculate for each node in the N - ary tree ; Storing maximum edge ; Swaping if node a is at more depth than node b because we will always take at more depth ; Difference between the depth of the two given nodes ; Changing Node B to its parent at 2 ^ i distance ; Subtracting distance by 2 ^ i ; Take both a , b to its lca and find maximum ; Loop to find the maximum 2 ^ ith parent the is different for both a and b ; Updating ans ; Changing value to its parent ; Function to compute the Least common Ansector ; Driver Code ; Undirected tree ; Computing LCA"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int N = 100005 ; static int [ ] level = new int [ N ] ; static int LG = 20 ; static int [ , ] dp = new int [ LG , N ] ; static int [ , ] mx = new int [ LG , N ] ; static List < List < Tuple < int , int > > > v = new List < List < Tuple < int , int > > > ( ) ; static int n = 0 ; static void dfs_lca ( int a , int par , int lev ) { dp [ 0 , a ] = par ; level [ a ] = lev ; for ( int i = 0 ; i < v [ a ] . Count ; i ++ ) { if ( v [ a ] [ i ] . Item1 == par ) continue ; mx [ 0 , v [ a ] [ i ] . Item1 ] = v [ a ] [ i ] . Item2 ; dfs_lca ( v [ a ] [ i ] . Item1 , a , lev + 1 ) ; } } static void find_ancestor ( ) { for ( int i = 1 ; i < 16 ; i ++ ) { for ( int j = 1 ; j < n + 1 ; j ++ ) { dp [ i , j ] = dp [ i - 1 , dp [ i - 1 , j ] ] ; mx [ i , j ] = Math . Max ( mx [ i - 1 , j ] , mx [ i - 1 , dp [ i - 1 , j ] ] ) ; } } } static int getMax ( int a , int b ) { if ( level [ b ] < level [ a ] ) { int temp = a ; a = b ; b = temp ; } int ans = 0 ; int diff = level [ b ] - level [ a ] ; while ( diff > 0 ) { int log = ( int ) ( Math . Log ( diff ) / Math . Log ( 2 ) ) ; ans = Math . Max ( ans , mx [ log , b ] ) ; b = dp [ log , b ] ; diff -= ( 1 << log ) ; } while ( a != b ) { int i = ( int ) ( Math . Log ( level [ a ] ) / Math . Log ( 2 ) ) ; while ( i > 0 && dp [ i , a ] == dp [ i , b ] ) { i -= 1 ; } ans = Math . Max ( ans , mx [ i , a ] ) ; ans = Math . Max ( ans , mx [ i , b ] ) ; a = dp [ i , a ] ; b = dp [ i , b ] ; } return ans ; } static void compute_lca ( ) { dfs_lca ( 1 , 0 , 0 ) ; find_ancestor ( ) ; } static void Main ( ) { for ( int i = 0 ; i < LG ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { dp [ i , j ] = 0 ; mx [ i , j ] = 0 ; } } for ( int i = 0 ; i < N ; i ++ ) { v . Add ( new List < Tuple < int , int > > ( ) ) ; } v [ 1 ] . Add ( new Tuple < int , int > ( 2 , 2 ) ) ; v [ 2 ] . Add ( new Tuple < int , int > ( 1 , 2 ) ) ; v [ 1 ] . Add ( new Tuple < int , int > ( 3 , 5 ) ) ; v [ 3 ] . Add ( new Tuple < int , int > ( 1 , 5 ) ) ; v [ 3...


"Maximum width of a binary tree | C # program to calculate maximum width of a binary tree using queue ; A binary tree node has data , pointer to left child and a pointer to right child ; Function to find the maximum width of the tree using level order traversal ; Base case ; Initialize result ; Do Level order traversal keeping track of number of nodes at every level ; Get the size of queue when the level order traversal for one level finishes ; Update the maximum node count value ; Iterate for all the nodes in the queue currently ; Dequeue an node from queue ; Enqueue left and right children of dequeued node ; Driver code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; public class maxwidthusingqueue { public class node { public int data ; public node left , right ; public node ( int data ) { this . data = data ; } } static int maxwidth ( node root ) { if ( root == null ) return 0 ; int maxwidth = 0 ; Queue < node > q = new Queue < node > ( ) ; q . Enqueue ( root ) ; while ( q . Count != 0 ) { int count = q . Count ; maxwidth = Math . Max ( maxwidth , count ) ; while ( count -- > 0 ) { node temp = q . Dequeue ( ) ; if ( temp . left != null ) { q . Enqueue ( temp . left ) ; } if ( temp . right != null ) { q . Enqueue ( temp . right ) ; } } } return maxwidth ; } public static void Main ( String [ ] args ) { node root = new node ( 1 ) ; root . left = new node ( 2 ) ; root . right = new node ( 3 ) ; root . left . left = new node ( 4 ) ; root . left . right = new node ( 5 ) ; root . right . right = new node ( 8 ) ; root . right . right . left = new node ( 6 ) ; root . right . right . right = new node ( 7 ) ; Console . WriteLine ( " Maximum ▁ width ▁ = ▁ " + maxwidth ( root ) ) ; } }


"Maximum width of a binary tree | C # program to calculate width of binary tree ; A binary tree node has data , pointer to left child and a pointer to right child ; Function to get the maximum width of a binary tree ; Create an array that will store count of nodes at each level ; Fill the count array using preorder traversal ; Return the maximum value from count array ; A function that fills count array with count of nodes at every level of given binary tree ; Compute the " height " of a tree -- the number of nodes along the longest path from the root node down to the farthest leaf node . ; compute the height of each subtree ; use the larger one ; Return the maximum value from count array ; Driver program to test above functions ; Constructed bunary tree is : 1 / \ 2 3 / \ \ 4 5 8 / \ 6 7"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { Node root ; int getMaxWidth ( Node node ) { int width ; int h = height ( node ) ; int [ ] count = new int [ 10 ] ; int level = 0 ; getMaxWidthRecur ( node , count , level ) ; return getMax ( count , h ) ; } void getMaxWidthRecur ( Node node , int [ ] count , int level ) { if ( node != null ) { count [ level ] ++ ; getMaxWidthRecur ( node . left , count , level + 1 ) ; getMaxWidthRecur ( node . right , count , level + 1 ) ; } } int height ( Node node ) { if ( node == null ) return 0 ; else { int lHeight = height ( node . left ) ; int rHeight = height ( node . right ) ; return ( lHeight > rHeight ) ? ( lHeight + 1 ) : ( rHeight + 1 ) ; } } int getMax ( int [ ] arr , int n ) { int max = arr [ 0 ] ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } return max ; } public static void Main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . right = new Node ( 8 ) ; tree . root . right . right . left = new Node ( 6 ) ; tree . root . right . right . right = new Node ( 7 ) ; Console . WriteLine ( " Maximum ▁ width ▁ is ▁ " + tree . getMaxWidth ( tree . root ) ) ; } }


"Maximum width of a binary tree | C # program to calculate width of binary tree ; A binary tree node has data , pointer to left child and a pointer to right child ; Function to get the maximum width of a binary tree ; Get width of each level and compare the width with maximum width so far ; Get width of a given level ; Compute the " height " of a tree -- the number of nodes along the longest path from the root node down to the farthest leaf node . ; compute the height of each subtree ; use the larger one ; Driver code ; Constructed binary tree is : 1 / \ 2 3 / \ \ 4 5 8 / \ 6 7 ; Function call"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { public Node root ; public virtual int getMaxWidth ( Node node ) { int maxWidth = 0 ; int width ; int h = height ( node ) ; int i ; for ( i = 1 ; i <= h ; i ++ ) { width = getWidth ( node , i ) ; if ( width > maxWidth ) { maxWidth = width ; } } return maxWidth ; } public virtual int getWidth ( Node node , int level ) { if ( node == null ) { return 0 ; } if ( level == 1 ) { return 1 ; } else if ( level > 1 ) { return getWidth ( node . left , level - 1 ) + getWidth ( node . right , level - 1 ) ; } return 0 ; } public virtual int height ( Node node ) { if ( node == null ) { return 0 ; } else { int lHeight = height ( node . left ) ; int rHeight = height ( node . right ) ; return ( lHeight > rHeight ) ? ( lHeight + 1 ) : ( rHeight + 1 ) ; } } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . right = new Node ( 8 ) ; tree . root . right . right . left = new Node ( 6 ) ; tree . root . right . right . right = new Node ( 7 ) ; Console . WriteLine ( " Maximum ▁ width ▁ is ▁ " + tree . getMaxWidth ( tree . root ) ) ; } }


"Maze With N doors and 1 Key | C # implementation of the approach ; Recursive function to check whether there is a path from the top left cell to the bottom right cell of the maze ; Check whether the current cell is within the maze ; If key is required to move further ; If the key hasn 't been used before ; If current cell is the destination ; Either go down or right ; If current cell is the destination ; Either go down or right ; Driver code ; We are making these { { '0' , '0' , '1' } , { '1' , '0' , '1' } , { '1' , '1' , '0' } } ; ; If there is a path from the cell ( 0 , 0 )"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool findPath ( List < List < int > > maze , int xpos , int ypos , bool key ) { if ( xpos < 0 xpos >= maze . Count ypos < 0 ypos >= maze . Count ) return false ; if ( maze [ xpos ] [ ypos ] == '1' ) { if ( key == true ) if ( xpos == maze . Count - 1 && ypos == maze . Count - 1 ) return true ; return findPath ( maze , xpos + 1 , ypos , false ) || findPath ( maze , xpos , ypos + 1 , false ) ; } if ( xpos == maze . Count - 1 && ypos == maze . Count - 1 ) return true ; return findPath ( maze , xpos + 1 , ypos , key ) || findPath ( maze , xpos , ypos + 1 , key ) ; } static bool mazeProb ( List < List < int > > maze , int xpos , int ypos ) { bool key = true ; if ( findPath ( maze , xpos , ypos , key ) ) return true ; return false ; } public static void Main ( String [ ] args ) { int size = 3 ; List < List < int > > maze = new List < List < int > > ( size ) ; for ( int i = 0 ; i < size ; i ++ ) { maze . Add ( new List < int > ( ) ) ; } maze [ 0 ] . Add ( 0 ) ; maze [ 0 ] . Add ( 0 ) ; maze [ 0 ] . Add ( 1 ) ; maze [ 1 ] . Add ( 1 ) ; maze [ 1 ] . Add ( 0 ) ; maze [ 1 ] . Add ( 1 ) ; maze [ 2 ] . Add ( 1 ) ; maze [ 2 ] . Add ( 1 ) ; maze [ 2 ] . Add ( 0 ) ; if ( mazeProb ( maze , 0 , 0 ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Mean of distinct odd fibonacci nodes in a Linked List | C # program to implement the above approach ; Structure of a singly Linked List ; Stores data value of a Node ; Stores pointer to next Node ; Function to insert a node at the beginning of the singly Linked List ; Create a new Node ; Insert the data into the Node ; Insert pointer to the next Node ; Update head_ref ; Function to find the largest element from the linked list ; Stores the largest element in the linked list ; Iterate over the linked list ; If max is less than head . data ; Update max ; Update head ; Function to store all Fibonacci numbers up to the largest element of the list ; Store all Fibonacci numbers up to Max ; Stores first element of Fibonacci number ; Stores second element of Fibonacci number ; Insert prev into hashmap ; Insert curr into hashmap ; Insert all elements of Fibonacci numbers up to Max ; Stores current fibonacci number ; Insert temp into hashmap ; Update prev ; Update curr ; Function to find the mean of odd Fibonacci nodes ; Stores the largest element in the linked list ; Stores all fibonacci numbers up to Max ; Stores current node of linked list ; Stores count of odd Fibonacci nodes ; Stores sum of all odd fibonacci nodes ; Traverse the linked list ; if the data value of current node is an odd number ; if data value of the node is present in hashmap ; Update cnt ; Update sum ; Remove current fibonacci number from hashmap so that duplicate elements can 't be  counted ; Update curr ; Return the required mean ; Driver Code ; Stores head node of the linked list ; Insert all data values in the linked list"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node next ; } ; static Node head ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static int largestElement ( Node head_ref ) { int Max = int . MinValue ; Node head = head_ref ; while ( head != null ) { if ( Max < head . data ) { Max = head . data ; } head = head . next ; } return Max ; } static HashSet < int > createDictionary ( int Max ) { HashSet < int > hashmap = new HashSet < int > ( ) ; int prev = 0 ; int curr = 1 ; hashmap . Add ( prev ) ; hashmap . Add ( curr ) ; while ( curr <= Max ) { int temp = curr + prev ; hashmap . Add ( temp ) ; prev = curr ; curr = temp ; } return hashmap ; } static double meanofnodes ( ) { int Max = largestElement ( head ) ; HashSet < int > hashmap = createDictionary ( Max ) ; Node curr = head ; int cnt = 0 ; double sum = 0.0 ; while ( curr != null ) { if ( ( curr . data ) % 2 == 1 ) { if ( hashmap . Contains ( curr . data ) ) { cnt ++ ; sum += curr . data ; hashmap . Remove ( curr . data ) ; } } curr = curr . next ; } return ( sum / cnt ) ; } public static void Main ( String [ ] args ) { head = null ; head = push ( head , 5 ) ; head = push ( head , 21 ) ; head = push ( head , 8 ) ; head = push ( head , 12 ) ; head = push ( head , 3 ) ; head = push ( head , 13 ) ; head = push ( head , 144 ) ; head = push ( head , 6 ) ; Console . Write ( meanofnodes ( ) ) ; } }


"Mean of fourth powers of first N natural numbers | C # program for the above approach ; Function to find the average of the fourth power of first N natural numbers ; Store the resultant average calculated using formula ; Return the average ; Driver Code"
Solution in C#:

using System ; class GFG { static double findAverage ( int N ) { double avg = ( ( 6 * N * N * N * N ) + ( 15 * N * N * N ) + ( 10 * N * N ) - 1 ) / 30.0 ; return avg ; } public static void Main ( ) { int N = 3 ; Console . WriteLine ( findAverage ( N ) ) ; } }


"Mean of fourth powers of first N natural numbers | C # program for the above approach ; Function to find the average of the fourth power of first N natural numbers ; Stores the sum of the fourth powers of first N natural numbers ; Calculate the sum of fourth power ; Return the average ; Driver Code"
Solution in C#:

using System ; class GFG { static double findAverage ( int N ) { double S = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { S += i * i * i * i ; } return S / N ; } public static void Main ( ) { int N = 3 ; Console . WriteLine ( findAverage ( N ) ) ; } }


"Mean of given array after removal of K percent of smallest and largest array elements | C # program for the above approach ; Function to calculate the mean of a given array after removal of Kth percent of smallest and largest array elements ; Sort the array ; Find the K - th percent of the array size ; Traverse the array ; Skip the first K - th percent & last K - th percent array elements ; Mean of the rest of elements ; Print mean upto 5 decimal places ; Driver Code"
Solution in C#:

using System ; class GFG { static void meanOfRemainingElements ( int [ ] arr , int N , int K ) { Array . Sort ( arr ) ; int kthPercent = ( N * K ) / 100 ; float sum = 0f ; for ( int i = 0 ; i < N ; i ++ ) if ( i >= kthPercent && i < ( N - kthPercent ) ) sum += arr [ i ] ; float mean = ( sum / ( N - 2 * kthPercent ) ) ; Console . WriteLine ( Math . Round ( mean , 5 ) ) ; } public static void Main ( ) { int [ ] arr = { 6 , 2 , 7 , 5 , 1 , 2 , 0 , 3 , 10 , 2 , 5 , 0 , 5 , 5 , 0 , 8 , 7 , 6 , 8 , 0 } ; int arr_size = arr . Length ; int K = 5 ; meanOfRemainingElements ( arr , arr_size , K ) ; } }


"Mean of minimum of all possible K | C # code for the above approach ; Function to find the value of nCr ; Base Case ; Find nCr recursively ; Function to find the expected minimum values of all the subsets of size K ; Find the factorials that will be used later ; Find the factorials ; Total number of subsets ; Stores the sum of minimum over all possible subsets ; Iterate over all possible minimum ; Find the mean over all subsets ; Driver Code"
Solution in C#:

using System ; public class GFG { static int nCr ( int n , int r , int [ ] f ) { if ( n < r ) { return 0 ; } return f [ n ] / ( f [ r ] * f [ n - r ] ) ; } static int findMean ( int N , int X ) { int [ ] f = new int [ N + 1 ] ; f [ 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { f [ i ] = f [ i - 1 ] * i ; } int total = nCr ( N , X , f ) ; int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { count += nCr ( N - i , X - 1 , f ) * i ; } double E_X = ( double ) ( count ) / ( double ) ( total ) ; Console . Write ( " { 0 : F9 } " , E_X ) ; return 0 ; } static publ int N = 3 , X = 2 ; findMean ( N , X ) ; } } void Main ( ) {


"Mean of range in array | C # program to find floor value of mean in range l to r ; To calculate prefixSum of array ; Calculate prefix sum of array ; To return floor of mean in range l to r ; Sum of elements in range l to r is prefixSum [ r ] - prefixSum [ l - 1 ] Number of elements in range l to r is r - l + 1 ; Driver program to test above functions"
Solution in C#:

using System ; public class GFG { public static readonly int MAX = 1000005 ; static int [ ] prefixSum = new int [ MAX ] ; static void calculatePrefixSum ( int [ ] arr , int n ) { prefixSum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] ; } static int findMean ( int l , int r ) { if ( l == 0 ) return ( int ) Math . Floor ( ( double ) ( prefixSum [ r ] / ( r + 1 ) ) ) ; return ( int ) Math . Floor ( ( double ) ( prefixSum [ r ] - prefixSum [ l - 1 ] ) / ( r - l + 1 ) ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; calculatePrefixSum ( arr , n ) ; Console . WriteLine ( findMean ( 1 , 2 ) ) ; Console . WriteLine ( findMean ( 1 , 3 ) ) ; Console . WriteLine ( findMean ( 1 , 4 ) ) ; } }


"Mean of range in array | C # program to find floor value of mean in range l to r ; To find mean of range in l to r ; Both sum and count are initialize to 0 ; To calculate sum and number of elements in range l to r ; Calculate floor value of mean ; Returns mean of array in range l to r ; Driver program to test findMean ( )"
Solution in C#:

using System ; public class GFG { static int findMean ( int [ ] arr , int l , int r ) { int sum = 0 , count = 0 ; for ( int i = l ; i <= r ; i ++ ) { sum += arr [ i ] ; count ++ ; } int mean = ( int ) Math . Floor ( ( double ) sum / count ) ; return mean ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; Console . WriteLine ( findMean ( arr , 0 , 2 ) ) ; Console . WriteLine ( findMean ( arr , 1 , 3 ) ) ; Console . WriteLine ( findMean ( arr , 0 , 4 ) ) ; } }


"Mean | C # program to find mean ; Function for calculating mean ; Driver Code"
Solution in C#:

 using System ; class GFG { public static double findMean ( int [ ] a , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] ; return ( double ) sum / ( double ) n ; } public static void Main ( ) { int [ ] a = { 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 } ; int n = a . Length ; Console . Write ( " Mean ▁ = ▁ " + findMean ( a , n ) + "
 " ) ; } }


"Measure one litre using two vessels and infinite water supply | Sample run of the Algo for V1 with capacity 3 and V2 with capacity 7 1. Fill V1 : V1 = 3 , V2 = 0 2. Transfer from V1 to V2 , and fill V1 : V1 = 3 , V2 = 3 2. Transfer from V1 to V2 , and fill V1 : V1 = 3 , V2 = 6 3. Transfer from V1 to V2 , and empty V2 : V1 = 2 , V2 = 0 4. Transfer from V1 to V2 , and fill V1 : V1 = 3 , V2 = 2 5. Transfer from V1 to V2 , and fill V1 : V1 = 3 , V2 = 5 6. Transfer from V1 to V2 , and empty V2 : V1 = 1 , V2 = 0 7. Stop as V1 now contains 1 litre . Note that V2 was made empty in steps 3 and 6 because it became full ; A utility function to get GCD of two numbers ; Class to represent a Vessel ; A vessel has capacity , and current amount of water in it ; Constructor : initializes capacity as given , and current as 0 ; The main function to fill one litre in this vessel . Capacity of V2 must be greater than this vessel and two capacities must be coprime ; solution exists iff a and b are co - prime ; fill A ( smaller vessel ) ; Transfer water from V1 to V2 and reduce current of V1 by the amount equal to transferred water ; Finally , there will be 1 litre in vessel 1 ; Fills vessel with given amount and returns the amount of water transferred to it . If the vessel becomes full , then the vessel is made empty ; If the vessel can accommodate the given amount ; If the vessel cannot accommodate the given amount , then store the amount of water transferred ; Since the vessel becomes full , make the vessel empty so that it can be filled again ; Driver program to test above function ; a must be smaller than b ; Create two vessels of capacities a and b ; Get 1 litre in first vessel"
Solution in C#:

 using System ; class GFG { static int gcd ( int a , int b ) { return b > 0 ? gcd ( b , a % b ) : a ; } class Vessel { int capacity , current ; public Vessel ( int capacity ) { this . capacity = capacity ; current = 0 ; } public void makeOneLitre ( Vessel V2 ) { if ( gcd ( capacity , V2 . capacity ) != 1 ) return ; while ( current != 1 ) { if ( current == 0 ) current = capacity ; Console . Write ( " Vessel ▁ 1 : ▁ " + current + " ▁ Vessel ▁ 2 : ▁ " + V2 . current + "
 " ) ; current = current - V2 . transfer ( current ) ; } Console . Write ( " Vessel ▁ 1 : ▁ " + current + " ▁ Vessel ▁ 2 : ▁ " + V2 . current + "
 " ) ; } int transfer ( int amount ) { if ( current + amount < capacity ) { current += amount ; return amount ; } int transferred = capacity - current ; current = 0 ; return transferred ; } } public static void Main ( String [ ] args ) { int a = 3 , b = 7 ; Vessel V1 = new Vessel ( a ) ; Vessel V2 = new Vessel ( b ) ; V1 . makeOneLitre ( V2 ) ; } }


"Median after K additional integers | C # program to find median of an array when we are allowed to add additional K integers to it . ; Find median of array after adding k elements ; sorting the array in increasing order . ; printing the median of array . Since n + K is always odd and K < n , so median of array always lies in the range of n . ; Driver code"
Solution in C#:

using System ; class GFG { static void printMedian ( int [ ] arr , int n , int K ) { Array . Sort ( arr ) ; Console . Write ( arr [ ( n + K ) / 2 ] ) ; } public static void Main ( ) { int [ ] arr = { 5 , 3 , 2 , 8 } ; int k = 3 ; int n = arr . Length ; printMedian ( arr , n , k ) ; } }


"Median and Mode using Counting Sort | C # Program for Mode and Median using Counting Sort technique ; function that sort input array a [ ] and calculate mode and median using counting sort . ; The output array b [ ] will have sorted array ; variable to store max of input array which will to have size of count array ; auxiliary ( count ) array to store count . Initialize count array as 0. Size of count array will be equal to ( max + 1 ) . ; Store count of each element of input array ; mode is the index with maximum count ; Update count [ ] array with sum ; Sorted output array b [ ] to calculate median ; Median according to odd and even array size respectively . ; Output the result ; Driver Code"
Solution in C#:

using System ; using System . Linq ; class GFG { static void printModeMedian ( int [ ] a , int n ) { int [ ] b = new int [ n ] ; int max = a . Max ( ) ; int t = max + 1 ; int [ ] count = new int [ t ] ; for ( int i = 0 ; i < t ; i ++ ) { count [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { count [ a [ i ] ] ++ ; } int mode = 0 ; int k = count [ 0 ] ; for ( int i = 1 ; i < t ; i ++ ) { if ( count [ i ] > k ) { k = count [ i ] ; mode = i ; } } for ( int i = 1 ; i < t ; i ++ ) { count [ i ] = count [ i ] + count [ i - 1 ] ; } for ( int i = 0 ; i < n ; i ++ ) { b [ count [ a [ i ] ] - 1 ] = a [ i ] ; count [ a [ i ] ] -- ; } float median ; if ( n % 2 != 0 ) { median = b [ n / 2 ] ; } else { median = ( float ) ( ( b [ ( n - 1 ) / 2 ] + b [ ( n / 2 ) ] ) / 2.0 ) ; } Console . WriteLine ( " median ▁ = ▁ " + median ) ; Console . WriteLine ( " mode ▁ = ▁ " + mode ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 4 , 1 , 2 , 7 , 1 , 2 , 5 , 3 , 6 } ; int n = a . Length ; printModeMedian ( a , n ) ; } }