Maximize sum by selecting X different | Java program for the above approach ; Function to find maximum sum of at most N with different index array elements such that at most X are from A [ ] , Y are from B [ ] and Z are from C [ ] ; Base Cases ; Selecting i - th element from A [ ] ; Selecting i - th element from B [ ] ; Selecting i - th element from C [ ] ; i - th elements not selected from any of the arrays ; Select the maximum sum from all the possible calls ; Driver Code ; Given X , Y and Z ; Given A [ ] ; Given B [ ] ; Given C [ ] ; Given Size ; Function Call
class GFG { static int FindMaxS ( int X , int Y , int Z , int n , int A [ ] , int B [ ] , int C [ ] ) { if ( X < 0 Y < 0 Z < 0 ) return Integer . MIN_VALUE ; if ( n < 0 ) return 0 ; int ch = A [ n ] + FindMaxS ( X - 1 , Y , Z , n - 1 , A , B , C ) ; int ca = B [ n ] + FindMaxS ( X , Y - 1 , Z , n - 1 , A , B , C ) ; int co = C [ n ] + FindMaxS ( X , Y , Z - 1 , n - 1 , A , B , C ) ; int no = FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ; int maximum = Math . max ( ch , Math . max ( ca , Math . max ( co , no ) ) ) ; return maximum ; } public static void main ( String [ ] args ) { int X = 1 ; int Y = 1 ; int Z = 1 ; int A [ ] = { 10 , 0 , 5 } ; int B [ ] = { 5 , 10 , 0 } ; int C [ ] = { 0 , 5 , 10 } ; int n = B . length ; System . out . println ( FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ) ; } }