Given N rows and M columns of a matrix, the task is to fill the matrix cells using first K integers such that:
- Each group of cells is denoted by a single integer.
- The difference between the group containing the maximum and the minimum number of cells should be minimum.
- All the cells of the same group should be contiguous i.e., for any group, two adjacent cells should follow the rule |xi+1 – xi| + |yi+1 – yi| = 1.
Input: N = 5, M = 5, K = 6
1 1 1 1 1
3 2 2 2 2
3 3 3 4 4
5 5 5 4 4
5 6 6 6 6
The above matrix follows all the conditions above and dividing the matrix into K different groups.
Input: N = 2, M = 3, K = 3
1 1 2
3 3 2
For making three group of the matrix each should have the group of size two.
So, to reduce the difference between the group containing maximum and minimum no of cells and all the matrix cells are used to make the K different groups having all the adjacent elements of the same group follow the |xi + 1 – xi| + |yi + 1 – yi| = 1 as well.
Approach: Below are the steps:
- Create the matrix of size N * M.
- To reduce the difference between group containing max and min no of cells, fill all the part with at least (N*M)/ K cells.
- The remaining part will contain (N * M)/ K + 1 no of cells.
- To follow the given rules, traverse the matrix and fill the matrix with the different parts accordingly.
- Print the matrix after the above steps.
Below is the implementation of the above approach:
1 1 1 1 1 3 2 2 2 2 3 3 3 4 4 5 5 5 4 4 5 6 6 6 6
Time Complexity: O(N * M)
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.
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Count of cells in a matrix whose adjacent cells's sum is prime Number
- Minimum Numbers of cells that are connected with the smallest path between 3 given cells
- Construct a Matrix such that each cell consists of sum of adjacent elements of respective cells in given Matrix
- Divide 1 to n into two groups with minimum sum difference
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Maximum given sized rectangles that can be cut out of a sheet of paper
- Minimum number of cuts required to make circle segments equal sized
- Shortest distance between two cells in a matrix or grid
- Find whether there is path between two cells in matrix
- Minimum Distance from a given Cell to all other Cells of a Matrix
- Partition the array into two odd length groups with minimized absolute difference between their median
- Number of cells in the right and left diagonals passing through (x, y) in a matrix
- Source to destination in 2-D path with fixed sized jumps
- Count the number of ways to divide N in k groups incrementally
- Divide a number into two parts such that sum of digits is maximum
- Divide N into K unique parts such that gcd of those parts is maximum
- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Number of ways of distributing N identical objects in R distinct groups with no groups empty
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.