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
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 ) ) ; } }