Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups

Given N rows and M columns of a matrix, the task is to fill the matrix cells using first K integers such that:

Examples:

Input: N = 5, M = 5, K = 6
Output:
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
Explanation:
The above matrix follows all the conditions above and dividing the matrix into K different groups.

Input: N = 2, M = 3, K = 3
Output:
1 1 2
3 3 2
Explanation:
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:



Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to fill the matrix with
// the given conditions
void fillMatrix(int** mat, int& row,
                int& col, int sizeOfpart,
                int noOfPart, int& start,
                int m, int& flag)
{
 
    // Count of parts with size sizeOfPart
    for (int i = 0; i < noOfPart; ++i) {
 
        int count = 0;
 
        while (count < sizeOfpart) {
 
            // Assigning the cell
            // with no of groups
            mat[row][col] = start;
 
            // Update row
            if (col == m - 1
                && flag == 1) {
                row++;
                col = m;
                flag = 0;
            }
            else if (col == 0
                     && flag == 0) {
                row++;
                col = -1;
                flag = 1;
            }
 
            // Update col
            if (flag == 1) {
                col++;
            }
            else {
                col--;
            }
 
            // Increment count
            count++;
        }
 
        // For new group increment start
        start++;
    }
}
 
// Function to return the reference of
// the matrix to be filled
int** findMatrix(int N, int M, int k)
{
 
    // Create matrix of size N*M
    int** mat = (int**)malloc(
        N * sizeof(int*));
 
    for (int i = 0; i < N; ++i) {
        mat[i] = (int*)malloc(
            M * sizeof(int));
    }
 
    // Starting index of the matrix
    int row = 0, col = 0;
 
    // Size of one group
    int size = (N * M) / k;
    int rem = (N * M) % k;
 
    // Element to assigned to matrix
    int start = 1, flag = 1;
 
    // Fill the matrix that have rem
    // no of parts with size size + 1
    fillMatrix(mat, row, col, size + 1,
               rem, start, M, flag);
 
    // Fill the remaining number of parts
    // with each part size is 'size'
    fillMatrix(mat, row, col, size,
               k - rem, start, M, flag);
 
    // Return the matrix
    return mat;
}
 
// Function to print the matrix
void printMatrix(int** mat, int N,
                 int M)
{
    // Traverse the rows
    for (int i = 0; i < N; ++i) {
 
        // Traverse the columns
        for (int j = 0; j < M; ++j) {
            cout << mat[i][j] << " ";
        }
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    // Given N, M, K
    int N = 5, M = 5, K = 6;
 
    // Function Call
    int** mat = findMatrix(N, M, K);
 
    // Function Call to print matrix
    printMatrix(mat, N, M);
 
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.util.*;
 
class GFG{
     
static int N, M;
static int [][]mat;
static int row, col, start, flag;
 
// Function to fill the matrix with
// the given conditions
static void fillMatrix(int sizeOfpart,
                       int noOfPart,
                       int m)
{
     
    // Count of parts with size sizeOfPart
    for(int i = 0; i < noOfPart; ++i)
    {
        int count = 0;
 
        while (count < sizeOfpart)
        {
             
            // Assigning the cell
            // with no of groups
            mat[row][col] = start;
 
            // Update row
            if (col == m - 1 && flag == 1)
            {
                row++;
                col = m;
                flag = 0;
            }
            else if (col == 0 && flag == 0)
            {
                row++;
                col = -1;
                flag = 1;
            }
 
            // Update col
            if (flag == 1)
            {
                col++;
            }
            else
            {
                col--;
            }
 
            // Increment count
            count++;
        }
 
        // For new group increment start
        start++;
    }
}
 
// Function to return the reference of
// the matrix to be filled
static void findMatrix(int k)
{
     
    // Create matrix of size N*M
    mat = new int[M][N];
 
    // Starting index of the matrix
    row = 0;
    col = 0;
 
    // Size of one group
    int size = (N * M) / k;
    int rem = (N * M) % k;
 
    // Element to assigned to matrix
    start = 1;
    flag = 1;
 
    // Fill the matrix that have rem
    // no of parts with size size + 1
    fillMatrix(size + 1, rem, M);
 
    // Fill the remaining number of parts
    // with each part size is 'size'
    fillMatrix(size, k - rem, M);
}
 
// Function to print the matrix
static void printMatrix()
{
     
    // Traverse the rows
    for(int i = 0; i < N; ++i)
    {
         
        // Traverse the columns
        for(int j = 0; j < M; ++j)
        {
            System.out.print(mat[i][j] + " ");
        }
        System.out.println();
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given N, M, K
    N = 5;
    M = 5;
    int K = 6;
 
    // Function Call
    findMatrix(K);
 
    // Function Call to print matrix
    printMatrix();
}
}
 
// This code is contributed by Amit Katiyar
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the
// above approach
using System;
class GFG{
     
static int N, M;
static int [,]mat;
static int row, col,
           start, flag;
 
// Function to fill the
// matrix with the given
// conditions
static void fillMatrix(int sizeOfpart,
                       int noOfPart,
                       int m)
{   
  // Count of parts with size
  // sizeOfPart
  for(int i = 0;
          i < noOfPart; ++i)
  {
    int count = 0;
 
    while (count < sizeOfpart)
    {
      // Assigning the cell
      // with no of groups
      mat[row, col] = start;
 
      // Update row
      if (col == m - 1 &&
          flag == 1)
      {
        row++;
        col = m;
        flag = 0;
      }
      else if (col == 0 &&
               flag == 0)
      {
        row++;
        col = -1;
        flag = 1;
      }
 
      // Update col
      if (flag == 1)
      {
        col++;
      }
      else
      {
        col--;
      }
 
      // Increment count
      count++;
    }
 
    // For new group increment
    // start
    start++;
  }
}
 
// Function to return the
// reference of the matrix
// to be filled
static void findMatrix(int k)
{   
  // Create matrix of
  // size N*M
  mat = new int[M, N];
 
  // Starting index of the
  // matrix
  row = 0;
  col = 0;
 
  // Size of one group
  int size = (N * M) / k;
  int rem = (N * M) % k;
 
  // Element to assigned to
  // matrix
  start = 1;
  flag = 1;
 
  // Fill the matrix that have
  // rem no of parts with size
  // size + 1
  fillMatrix(size + 1,
             rem, M);
 
  // Fill the remaining number
  // of parts with each part
  // size is 'size'
  fillMatrix(size, k - rem, M);
}
 
// Function to print the
// matrix
static void printMatrix()
{   
  // Traverse the rows
  for(int i = 0; i < N; ++i)
  {
    // Traverse the columns
    for(int j = 0; j < M; ++j)
    {
      Console.Write(mat[i, j] +
                    " ");
    }
    Console.WriteLine();
  }
}
 
// Driver Code
public static void Main(String[] args)
{   
  // Given N, M, K
  N = 5;
  M = 5;
  int K = 6;
 
  // Function Call
  findMatrix(K);
 
  // Function Call to
  // print matrix
  printMatrix();
}
}
 
// This code is contributed by 29AjayKumar
chevron_right

Output: 
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.




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 : amit143katiyar, 29AjayKumar

Article Tags :