Maximize sum by choosing elements from different section of a matrix
Last Updated :
14 Sep, 2022
Given a matrix of N rows and M columns. It is given that M is a multiple of 3. The columns are divided into 3 sections, the first section is from 0 to m/3-1, the second section is from m/3 to 2m/3-1 and the third section is from 2m/3 to m. The task is to choose a single element from each row and, in adjacent rows, we cannot select from the same section. We have to maximize the sum of the elements chosen.
Examples:
Input: mat[][] = {
{1, 3, 5, 2, 4, 6},
{6, 4, 5, 1, 3, 2},
{1, 3, 5, 2, 4, 6},
{6, 4, 5, 1, 3, 2},
{6, 4, 5, 1, 3, 2},
{1, 3, 5, 2, 4, 6}}
Output: 35
Input: mat[][] = {
{1, 2, 3},
{3, 2, 1},
{5, 4, 2}
Output: 10
Approach: The problem can be solved using dynamic programming solution by storing the subproblems and reusing them. Create a dp[][] array where dp[i][0] represents the sum of elements of rows from 0 to i taking elements from section 1. Similarly, for dp[i][1] and dp[i][2]. So, print the max(dp[n – 1][0], dp[n – 1][1], dp[n – 1][2].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int n = 6, m = 6;
void maxSum( long arr[n][m])
{
long dp[n + 1][3] = { 0 };
for ( int i = 0; i < n; i++) {
long m1 = 0, m2 = 0, m3 = 0;
for ( int j = 0; j < m; j++) {
if ((j / (m / 3)) == 0) {
m1 = max(m1, arr[i][j]);
}
else if ((j / (m / 3)) == 1) {
m2 = max(m2, arr[i][j]);
}
else if ((j / (m / 3)) == 2) {
m3 = max(m3, arr[i][j]);
}
}
dp[i + 1][0] = max(dp[i][1], dp[i][2]) + m1;
dp[i + 1][1] = max(dp[i][0], dp[i][2]) + m2;
dp[i + 1][2] = max(dp[i][1], dp[i][0]) + m3;
}
cout << max(max(dp[n][0], dp[n][1]), dp[n][2]) << '\n' ;
}
int main()
{
long arr[n][m] = { { 1, 3, 5, 2, 4, 6 },
{ 6, 4, 5, 1, 3, 2 },
{ 1, 3, 5, 2, 4, 6 },
{ 6, 4, 5, 1, 3, 2 },
{ 6, 4, 5, 1, 3, 2 },
{ 1, 3, 5, 2, 4, 6 } };
maxSum(arr);
return 0;
}
|
Java
class GFG
{
static int n = 6 , m = 6 ;
static void maxSum( long arr[][])
{
long [][]dp= new long [n + 1 ][ 3 ];
for ( int i = 0 ; i < n; i++)
{
long m1 = 0 , m2 = 0 , m3 = 0 ;
for ( int j = 0 ; j < m; j++)
{
if ((j / (m / 3 )) == 0 )
{
m1 = Math.max(m1, arr[i][j]);
}
else if ((j / (m / 3 )) == 1 )
{
m2 = Math.max(m2, arr[i][j]);
}
else if ((j / (m / 3 )) == 2 )
{
m3 = Math.max(m3, arr[i][j]);
}
}
dp[i + 1 ][ 0 ] = Math.max(dp[i][ 1 ], dp[i][ 2 ]) + m1;
dp[i + 1 ][ 1 ] = Math.max(dp[i][ 0 ], dp[i][ 2 ]) + m2;
dp[i + 1 ][ 2 ] = Math.max(dp[i][ 1 ], dp[i][ 0 ]) + m3;
}
System.out.print(Math.max(Math.max(dp[n][ 0 ], dp[n][ 1 ]), dp[n][ 2 ]) + "\n" );
}
public static void main(String[] args)
{
long arr[][] = { { 1 , 3 , 5 , 2 , 4 , 6 },
{ 6 , 4 , 5 , 1 , 3 , 2 },
{ 1 , 3 , 5 , 2 , 4 , 6 },
{ 6 , 4 , 5 , 1 , 3 , 2 },
{ 6 , 4 , 5 , 1 , 3 , 2 },
{ 1 , 3 , 5 , 2 , 4 , 6 } };
maxSum(arr);
}
}
|
Python3
import numpy as np
n = 6 ; m = 6 ;
def maxSum(arr) :
dp = np.zeros((n + 1 , 3 ));
for i in range (n) :
m1 = 0 ; m2 = 0 ; m3 = 0 ;
for j in range (m) :
if ((j / / (m / / 3 )) = = 0 ) :
m1 = max (m1, arr[i][j]);
elif ((j / / (m / / 3 )) = = 1 ) :
m2 = max (m2, arr[i][j]);
elif ((j / / (m / / 3 )) = = 2 ) :
m3 = max (m3, arr[i][j]);
dp[i + 1 ][ 0 ] = max (dp[i][ 1 ], dp[i][ 2 ]) + m1;
dp[i + 1 ][ 1 ] = max (dp[i][ 0 ], dp[i][ 2 ]) + m2;
dp[i + 1 ][ 2 ] = max (dp[i][ 1 ], dp[i][ 0 ]) + m3;
print ( max ( max (dp[n][ 0 ], dp[n][ 1 ]), dp[n][ 2 ]));
if __name__ = = "__main__" :
arr = [[ 1 , 3 , 5 , 2 , 4 , 6 ],
[ 6 , 4 , 5 , 1 , 3 , 2 ],
[ 1 , 3 , 5 , 2 , 4 , 6 ],
[ 6 , 4 , 5 , 1 , 3 , 2 ],
[ 6 , 4 , 5 , 1 , 3 , 2 ],
[ 1 , 3 , 5 , 2 , 4 , 6 ]];
maxSum(arr);
|
C#
using System;
class GFG
{
static int n = 6, m = 6;
static void maxSum( long [,]arr)
{
long [,]dp = new long [n + 1, 3];
for ( int i = 0; i < n; i++)
{
long m1 = 0, m2 = 0, m3 = 0;
for ( int j = 0; j < m; j++)
{
if ((j / (m / 3)) == 0)
{
m1 = Math.Max(m1, arr[i, j]);
}
else if ((j / (m / 3)) == 1)
{
m2 = Math.Max(m2, arr[i, j]);
}
else if ((j / (m / 3)) == 2)
{
m3 = Math.Max(m3, arr[i, j]);
}
}
dp[i + 1, 0] = Math.Max(dp[i, 1], dp[i, 2]) + m1;
dp[i + 1, 1] = Math.Max(dp[i, 0], dp[i, 2]) + m2;
dp[i + 1, 2] = Math.Max(dp[i, 1], dp[i, 0]) + m3;
}
Console.Write(Math.Max(Math.Max(dp[n, 0],
dp[n, 1]),
dp[n, 2]) + "\n" );
}
public static void Main(String[] args)
{
long [,]arr = { { 1, 3, 5, 2, 4, 6 },
{ 6, 4, 5, 1, 3, 2 },
{ 1, 3, 5, 2, 4, 6 },
{ 6, 4, 5, 1, 3, 2 },
{ 6, 4, 5, 1, 3, 2 },
{ 1, 3, 5, 2, 4, 6 } };
maxSum(arr);
}
}
|
Javascript
<script>
const n = 6, m = 6;
function maxSum(arr)
{
const dp = new Array(n+1).fill(0).map(() => new Array(3).fill(0));
for ( var i = 0; i < n; i++) {
var m1 = 0, m2 = 0, m3 = 0;
for ( var j = 0; j < m; j++) {
if (parseInt(j / (m / 3)) == 0) {
m1 = Math.max(m1, arr[i][j]);
}
else if (parseInt(j / (m / 3)) == 1) {
m2 = Math.max(m2, arr[i][j]);
}
else if (parseInt(j / (m / 3)) == 2) {
m3 = Math.max(m3, arr[i][j]);
}
}
dp[i + 1][0] = Math.max(dp[i][1], dp[i][2]) + m1;
dp[i + 1][1] = Math.max(dp[i][0], dp[i][2]) + m2;
dp[i + 1][2] = Math.max(dp[i][1], dp[i][0]) + m3;
}
document.write( parseInt(Math.max(Math.max(dp[n][0], dp[n][1]), dp[n][2])) + "<br>" );
}
arr = [ [ 1, 3, 5, 2, 4, 6 ],
[ 6, 4, 5, 1, 3, 2 ],
[ 1, 3, 5, 2, 4, 6 ],
[ 6, 4, 5, 1, 3, 2 ],
[ 6, 4, 5, 1, 3, 2 ],
[ 1, 3, 5, 2, 4, 6 ] ];
maxSum(arr);
</script>
|
Time Complexity: O(n*m), Where n and m are the numbers of rows and number columns in the matrix.
Auxiliary Space: O(n), as we are using extra space for the dp matrix.
Share your thoughts in the comments
Please Login to comment...