Maximum Sum possible by selecting X elements from a Matrix based on given conditions

Given a matrix G[][] of dimensions N × M, consists of positive integers, the task is to select X elements from the matrix having maximum sum considering the condition that G[i][j] can only be selected from the matrix unless all the elements G[i][k] are selected, where 0 ≤ k < j i.e., the jth element in the current ith row can be selected all its the preceding elements of the current ith row has already been selected.
Examples:

Input: N = 4, M = 4, X = 6, G[][] = {{3, 2, 6, 1}, {1, 9, 2, 4}, {4, 1, 3, 9}, {3, 8, 2, 1}} 
Output: 28 
Explanation: 
Selecting the first element from the 1st row = 3 
Selecting the the first two elements from the 2nd row = 1 + 9 = 10 
Selecting the first element from the 3rd row = 4 
Selecting the first two elements from the 4th row = 3 + 8 = 11 
Hence, the selected elements are {G[0][0], G[1][0], G[1][1], G[2][0], G[3][0], G[3][1]} 
Hence, the sum of the selected elements is = 3 + 10 + 4 + 11 = 28

Input: N = 2, M = 4, X = 4, G[][] = {{10, 10, 100, 30}, {80, 50, 10, 50}} 
Output: 200

Naive Approach: The simplest approach to solve this problem is to calculate the sum for all possible M selections and find the maximum sum among them.

Time Complexity: O(NM)
Auxiliary Space: O(1)



Efficient Approach: The above approach can be optimized using Dynamic Programming. The considerable states are: 
 

  1. Number of rows selected: i.
  2. Number of elements selected: j.

Initialize a matrix dp[][] such that dp[i][j] stores the maximum possible sum that can be obtained by selecting j elements from the first i rows.

The transition for the dp[][] is as follows:

dp[i][j] = maxx=(0, min(j, m))(dp[i – 1][j – x] + prefsum[i][x]) 
where prefsum[i][x] is the sum of the first x elements in the ith row of the matrix.

Below is the implementation of above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
import java.util.*;
import java.io.*;
  
class GFG {
  
    static int n, m, X;
  
    // Function to calculate the maximum
    // possible sum by selecting X elements
    // from the Matrix
    public static int maxSum(int[][] grid)
    {
  
        // Generate prefix sum of the matrix
        int prefsum[][] = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int x = 0; x < m; x++) {
                if (x == 0)
                    prefsum[i][x] = grid[i][x];
                else
                    prefsum[i][x]
                        = prefsum[i][x - 1] + grid[i][x];
            }
        }
  
        int dp[][] = new int[n][X + 1];
  
        // Initialize dp[][]
        for (int dpp[] : dp)
            Arrays.fill(dpp, Integer.MIN_VALUE);
  
        // Maximum possible sum by selecting
        // 0 elements from the first i rows
        for (int i = 0; i < n; i++)
            dp[i][0] = 0;
  
        // If a single row is present
        for (int i = 1; i <= Math.min(m, X); ++i) {
            dp[0][i] = dp[0][i - 1] + grid[0][i - 1];
        }
  
        for (int i = 1; i < n; ++i) {
            for (int j = 1; j <= X; ++j) {
  
                // If elements from the
                // current row is not selected
                dp[i][j] = dp[i - 1][j];
  
                // Iterate over all possible
                // selections from current row
                for (int x = 1; x <= Math.min(j, m);
                     x++) {
                    dp[i][j]
                        = Math.max(dp[i][j],
                                   dp[i - 1][j - x]
                                       + prefsum[i][x - 1]);
                }
            }
        }
  
        // Return maximum possible sum
        return dp[n - 1][X];
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        n = 4;
        m = 4;
        X = 6;
  
        int grid[][] = { { 3, 2, 6, 1 },
                         { 1, 9, 2, 4 },
                         { 4, 1, 3, 9 },
                         { 3, 8, 2, 1 } };
  
        int ans = maxSum(grid);
  
        System.out.println(ans);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
import sys
  
# Function to calculate the maximum
# possible sum by selecting X elements
# from the Matrix
def maxSum(grid):
      
    # Generate prefix sum of the matrix
    prefsum = [[0 for x in range(m)]
                  for y in range(m)]
                    
    for i in range(n):
        for x in range(m):
            if (x == 0):
                prefsum[i][x] = grid[i][x]
            else:
                prefsum[i][x] = (prefsum[i][x - 1] +
                                    grid[i][x])
  
    dp = [[-sys.maxsize - 1 for x in range(X + 1)]
                            for y in range(n)]
  
    # Maximum possible sum by selecting
    # 0 elements from the first i rows
    for i in range(n):
        dp[i][0] = 0
  
    # If a single row is present
    for i in range(1, min(m, X)):
        dp[0][i] = (dp[0][i - 1] + 
                  grid[0][i - 1])
  
    for i in range(1, n):
        for j in range(1, X + 1):
  
            # If elements from the
            # current row is not selected
            dp[i][j] = dp[i - 1][j]
  
            # Iterate over all possible
            # selections from current row
            for x in range(1, min(j, m) + 1):
                    dp[i][j] = max(dp[i][j],
                                   dp[i - 1][j - x] + 
                              prefsum[i][x - 1])
  
    # Return maximum possible sum
    return dp[n - 1][X]
  
# Driver Code
if __name__ == "__main__":
      
    n = 4
    m = 4
    X = 6
  
    grid = [ [ 3, 2, 6, 1 ],
             [ 1, 9, 2, 4 ],
             [ 4, 1, 3, 9 ],
             [ 3, 8, 2, 1 ] ]
    ans = maxSum(grid)
  
    print(ans)
  
# This code is contributed by chitranayal    

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement 
// the above approach 
using System;
  
class GFG{ 
  
static int n, m, X; 
  
// Function to calculate the maximum 
// possible sum by selecting X elements 
// from the Matrix 
public static int maxSum(int[,] grid) 
      
    // Generate prefix sum of the matrix 
    int [,]prefsum = new int[n, m];
      
    for(int i = 0; i < n; i++)
    
        for(int x = 0; x < m; x++) 
        
            if (x == 0) 
                prefsum[i, x] = grid[i, x]; 
            else
                prefsum[i, x] = prefsum[i, x - 1] +
                                   grid[i, x]; 
        
    
  
    int [,]dp = new int[n, X + 1]; 
  
    // Initialize [,]dp 
    for(int i = 1; i < n; i++) 
        for(int j = 1; j <= X; ++j)
            dp[i, j] = int.MinValue;
              
    // Maximum possible sum by selecting 
    // 0 elements from the first i rows 
    for(int i = 0; i < n; i++) 
        dp[i, 0] = 0; 
  
    // If a single row is present 
    for(int i = 1; i <= Math.Min(m, X); ++i)
    
        dp[0, i] = dp[0, i - 1] + grid[0, i - 1]; 
    
  
    for(int i = 1; i < n; ++i)
    
        for(int j = 1; j <= X; ++j)
        
              
            // If elements from the 
            // current row is not selected 
            dp[i, j] = dp[i - 1, j]; 
  
            // Iterate over all possible 
            // selections from current row 
            for(int x = 1; x <= Math.Min(j, m); x++) 
            
                dp[i, j] = Math.Max(dp[i, j], 
                                    dp[i - 1, j - x] + 
                               prefsum[i, x - 1]); 
            
        
    
      
    // Return maximum possible sum 
    return dp[n - 1, X]; 
  
// Driver Code 
public static void Main(String[] args) 
    n = 4; 
    m = 4; 
    X = 6; 
  
    int [,]grid = { { 3, 2, 6, 1 }, 
                    { 1, 9, 2, 4 }, 
                    { 4, 1, 3, 9 }, 
                    { 3, 8, 2, 1 } }; 
  
    int ans = maxSum(grid); 
  
    Console.WriteLine(ans); 
  
// This code is contributed by 29AjayKumar 

chevron_right


Output: 

28

Time Complexity: O(N*M*X)
Auxiliary Space: O(N*M)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : 29AjayKumar, chitranayal