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.
Input: N = 3, M = 3, X = 5, Y = 3
3 0 3
0 3 0
3 0 3
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
3 2 3
2 3 2
3 2 3
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
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.
3 0 3 0 3 0 3 0 3
Time complexity: O(N * M)
Space complexity: O(1)
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.
- Maximum possible sum of non-adjacent array elements not exceeding K
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K | Set 2
- Minimize count of Subsets with difference between maximum and minimum element not exceeding K
- Count rows with sum exceeding sum of the remaining Matrix
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Construct a matrix with sum equal to the sum of diagonal elements
- Count array elements exceeding sum of preceding K elements
- Construct a square Matrix whose parity of diagonal sum is same as size of matrix
- Maximize subsequences having array elements not exceeding length of the subsequence
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Check if two items can be selected from two different categories without exceeding price
- Maximum students to pass after giving bonus to everybody and not exceeding 100 marks
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
- Construct a matrix such that union of ith row and ith column contains every element from 1 to 2N-1
- Circular Matrix (Construct a matrix with numbers 1 to m*n in spiral way)
- Count of all possible numbers not exceeding M having suffix N
- Count number of triangles possible with length of sides not exceeding N
- Split array into minimum number of subarrays having GCD of its first and last element exceeding 1
- Construct a sequence from given frequencies of N consecutive integers with unit adjacent difference
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.