Given four integers N, M, X and Y, the task is to construct a N * M matrix such that each cell consists of a value in the range [0, X] such that sum of any two adjacent cells should be less than or equal to Y and the total sum of the matrix should be maximum.
Example:
Input: N = 3, M = 3, X = 5, Y = 3
Output:
3 0 3
0 3 0
3 0 3
Explanation:
All the values of the matrix are between 0 to 5.
The Sum of any two adjacent cells is equal to 3.
The total sum of the matrix is 15, which is maximum possible.
Input: N = 3, M = 3, X = 3, Y = 5
Output:
3 2 3
2 3 2
3 2 3
Approach:
Follow the steps below to solve the problem:
- In order to satisfy the two necessary conditions, the matrix needs to be filled with only the following two numbers alternately:
First number = minimum(X, Y)
Second number = minimum(2X, Y) – First Number
Illustration:
N = 3, M = 3, X = 5, Y = 3
First Number = Minimum(X, Y) = Minimum(5, 3) = 3
Second Number = Minimum(2X, Y) – 3 = Minimum(10, 3) – 3 = 3 – 3 = 0
Therefore, sum of any two adjacent cells = 3 + 0 = 3(= Y)
- Finally, print the two numbers alternately, first number followed by the second number to maximize the sum of the matrix.
Below is the implementation of the above approach.
C++
#include <iostream>
using namespace std;
void FindMatrix( int n, int m,
int x, int y)
{
int a, b, i, j;
if (n * m == 1) {
if (x > y) {
cout << y << "\n" ;
}
else {
cout << x << "\n" ;
}
return ;
}
a = min(x, y);
b = min(2 * x, y) - a;
bool flag = true ;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (flag)
cout << a << ' ' ;
else
cout << b << ' ' ;
flag = !flag;
}
if (((n % 2 != 0 && m % 2 == 0)
|| (n % 2 == 0 && m % 2 == 0)))
flag = !flag;
cout << "\n" ;
}
}
int main()
{
int N, M, X, Y;
N = 3;
M = 3;
X = 5;
Y = 3;
FindMatrix(N, M, X, Y);
}
|
Java
import java.io.*;
public class GFG{
static void FindMatrix( int n, int m,
int x, int y)
{
int a, b, i, j;
if (n * m == 1 )
{
if (x > y)
{
System.out.print(y + "\n" );
}
else
{
System.out.print(x + "\n" );
}
return ;
}
a = Math.min(x, y);
b = Math.min( 2 * x, y) - a;
boolean flag = true ;
for (i = 0 ; i < n; i++)
{
for (j = 0 ; j < m; j++)
{
if (flag)
System.out.print(a + " " );
else
System.out.print(b + " " );
flag = !flag;
}
if (((n % 2 != 0 && m % 2 == 0 ) ||
(n % 2 == 0 && m % 2 == 0 )))
flag = !flag;
System.out.print( "\n" );
}
}
public static void main(String[] args)
{
int N, M, X, Y;
N = 3 ;
M = 3 ;
X = 5 ;
Y = 3 ;
FindMatrix(N, M, X, Y);
}
}
|
Python3
def FindMatrix(n, m, x, y):
if (n * m = = 1 ):
if (x > y):
print (y)
else :
print (x)
return
a = min (x, y)
b = min ( 2 * x, y) - a
flag = True
for i in range (n):
for j in range (m):
if (flag):
print (a, end = " " )
else :
print (b, end = " " )
flag = not flag
if (((n % 2 ! = 0 and m % 2 = = 0 ) or
(n % 2 = = 0 and m % 2 = = 0 ))):
flag = not flag
print ()
N = 3
M = 3
X = 5
Y = 3
FindMatrix(N, M, X, Y)
|
C#
using System;
class GFG{
static void FindMatrix( int n, int m,
int x, int y)
{
int a, b, i, j;
if (n * m == 1)
{
if (x > y)
{
Console.Write(y + "\n" );
}
else
{
Console.Write(x + "\n" );
}
return ;
}
a = Math.Min(x, y);
b = Math.Min(2 * x, y) - a;
bool flag = true ;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
if (flag)
Console.Write(a + " " );
else
Console.Write(b + " " );
flag = !flag;
}
if (((n % 2 != 0 && m % 2 == 0) ||
(n % 2 == 0 && m % 2 == 0)))
flag = !flag;
Console.Write( "\n" );
}
}
public static void Main(String[] args)
{
int N, M, X, Y;
N = 3;
M = 3;
X = 5;
Y = 3;
FindMatrix(N, M, X, Y);
}
}
|
Javascript
<script>
function FindMatrix(n, m, x, y)
{
var a, b, i, j;
if (n * m == 1)
{
if (x > y)
{
document.write(y);
}
else
{
document.write(x);
}
return ;
}
a = Math.min(x, y);
b = Math.min(2 * x, y) - a;
var flag = true ;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
if (flag)
document.write(a + " " );
else
document.write(b + " " );
flag = !flag;
}
if (((n % 2 != 0 && m % 2 == 0) ||
(n % 2 == 0 && m % 2 == 0)))
flag = !flag;
document.write( "<br>" );
}
}
var N, M, X, Y;
N = 3;
M = 3;
X = 5;
Y = 3;
FindMatrix(N, M, X, Y);
</script>
|
Output: 3 0 3
0 3 0
3 0 3
Time complexity: O(N * M)
Space complexity: O(1)