# Enlarge a Matrix such that each element occurs in R rows and C columns

Given a matrix arr[][] of size N x M, and two numbers R and C, the task is to enlarge this matrix such that each element of the original matrix occurs in R rows and C columns in the enlarged matrix.

Examples:

Input: arr[][] = {{1, 2, 3}, {4, 5, 6}} R = 3, C = 2
Output:
1 1 2 2 3 3 4 4 5 5 6 6
1 1 2 2 3 3 4 4 5 5 6 6
1 1 2 2 3 3 4 4 5 5 6 6
Explanation:
Every element of the original matrix appears in 3 rows and 2 columns in the enlarged matrix.

Input: arr = {{1, 2}}, R = 2, C = 3
Output:
1 1 1 2 2 2
1 1 1 2 2 2
Explanation:
Each element of the original matrix appears in 2 rows and 3 columns in the enlarged matrix.

Approach: This problem can be solved by keeping either the number of rows or columns fixed. In this article, the number of rows is fixed. Therefore, for any matrix of dimension N x M, the number of rows in the final answer would be R.
The idea is to first convert the given matrix into a one dimensional array (i.e) flatten the given matrix. Now, for every element in the flattened matrix, append the values in R rows and C columns of the final enlarged matrix.

Below is the implementation of the above approach:

## C++

 `// C++ program to enlarge a matrix ` `// such that each element occurs in ` `// R rows and C columns` `#include ` `using` `namespace` `std;`   `// Function to convert the matrix` `// into a 1-D array` `vector<``int``> oneD(vector> &matrix){` `     `  `    ``// Variable to store the` `    ``// given matrix ` `    ``vector<``int``> m;` ` `  `    ``// Iterating through all the ` `    ``// elements of the matrix` `    ``for``(``int` `i = 0; i < matrix.size(); ++i)` `        ``for``(``int` `j = 0; j < matrix.size(); ++j)` `            ``// Adding the element into the` `            ``// defined matrix` `            ``m.push_back(matrix[i][j]);` ` `  `    ``return` `m;` `}` ` `  `// Function to enlarge a matrix ` `// such that each element occurs ` `// in R rows and C columns` `vector> dimensions(``int` `R, ``int` `C, vector<``int``> matrix) {` `     `  `    ``// Initializing the enlarged ` `    ``// matrix` `    ``vector> ans(R+1);`   `    ``// Variables to iterate over ` `    ``// the matrix` `    ``int` `ctr = 0;` `    ``int` `r = 0;` `     `  `    ``while``(ctr < matrix.size()) {` ` `  `        ``// To keep the rows in the ` `        ``// range [0, R]` `        ``r = r % R;` `        ``int` `c = 0;` `        ``while``(c < C) {             ` `            ``// In order to fix the number ` `            ``// of columns, the above r ` `            ``// statement can be commented  ` `            ``// and this can be uncommented` `            ``// c = c % C` `               ``for``(``int` `i = 0; i < R; ++i)` `                   ``ans[i].push_back(matrix[ctr]);` `               `  `               ``c += 1;` `          ``}` `        ``ctr += 1;` `        ``r += 1;` `    ``}`   `    ``return` `ans;` `}` `    `  `// Driver code` `int` `main() {`   `    ``vector> arr = {{1, 2}, {3, 4}};` `    ``int` `R = 2, C = 3;` ` `  `    ``vector> ans = dimensions(R, C, oneD(arr));` `     `  `    ``// Print the enlarged matrix` `    ``for``(``int` `i = 0; i < ans.size(); ++i) {` `    `  `        ``for``(``int` `j = 0; j < ans[i].size(); ++j) {` `            ``cout << ans[i][j] << ``" "``;` `        ``}` `    `  `        ``cout<

## Java

 `// Java program to enlarge a matrix` `// such that each element occurs in` `// R rows and C columns` `import` `java.io.*;` `import` `java.util.*;`   `class` `GFG{`   `// Function to convert the matrix` `// into a 1-D array` `static` `ArrayListoneD(` `    ``ArrayList> matrix)` `{` `    `  `    ``// Variable to store the` `    ``// given matrix` `    ``ArrayList m = ``new` `ArrayList();`   `    ``// Iterating through all the` `    ``// elements of the matrix` `    ``for``(``int` `i = ``0``; i < matrix.size(); ++i)` `        ``for``(``int` `j = ``0``; j < matrix.get(``0``).size(); ++j)` `        `  `            ``// Adding the element into the` `            ``// defined matrix` `            ``m.add(matrix.get(i).get(j));`   `    ``return` `m;` `}`   `// Function to enlarge a matrix` `// such that each element occurs` `// in R rows and C columns` `static` `ArrayList>dimensions(` `    ``int` `R, ``int` `C, ArrayList matrix)` `{` `    `  `    ``// Initializing the enlarged` `    ``// matrix` `    ``ArrayList> ans = ``new` `ArrayList<>();` `    ``for``(``int` `i = ``0``; i < R; ++i)` `    ``{` `        ``ans.add(``new` `ArrayList());` `    ``}`   `    ``// Variables to iterate over` `    ``// the matrix` `    ``int` `ctr = ``0``;` `    ``int` `r = ``0``;`   `    ``while` `(ctr < matrix.size())` `    ``{` `        `  `        ``// To keep the rows in the` `        ``// range [0, R]` `        ``r = r % R;` `        ``int` `c = ``0``;` `        `  `        ``while` `(c < C)` `        ``{` `            `  `            ``// In order to fix the number` `            ``// of columns, the above r` `            ``// statement can be commented` `            ``// and this can be uncommented` `            ``// c = c % C` `            ``for``(``int` `i = ``0``; i < R; ++i) ` `            ``{` `                ``ans.get(i).add(matrix.get(ctr));` `            ``}` `            ``c += ``1``;` `        ``}` `        ``ctr += ``1``;` `        ``r += ``1``;` `    ``}` `    ``return` `ans;` `}`   `// Driver code` `public` `static` `void` `main(String[] args)` `{`   `    ``ArrayList> arr = ``new` `ArrayList<>();` `    ``arr.add(``new` `ArrayList(Arrays.asList(``1``, ``2``)));` `    ``arr.add(``new` `ArrayList(Arrays.asList(``3``, ``4``)));` `    ``int` `R = ``2``, C = ``3``;`   `    ``ArrayList> ans = ``new` `ArrayList<>(` `        ``dimensions(R, C, oneD(arr)));`   `    ``// Print the enlarged matrix` `    ``for``(``int` `i = ``0``; i < ans.size(); ++i) ` `    ``{` `        ``System.out.print(``"["``);` `        ``for``(``int` `j = ``0``; j < ans.get(i).size(); ++j)` `        ``{` `            ``System.out.print(ans.get(i).get(j) + ``", "``);` `        ``}` `        ``System.out.print(``"]\n"``);` `    ``}` `}` `}`   `// This code is contributed by akhilsaini`

## Python3

 `# Python program to enlarge a matrix ` `# such that each element occurs in ` `# R rows and C columns`   `# Function to convert the matrix` `# into a 1-D array` `def` `oneD(matrix):` `    `  `    ``# Variable to store the` `    ``# given matrix ` `    ``m ``=` `[]`   `    ``# Iterating through all the ` `    ``# elements of the matrix` `    ``for` `i ``in` `range``(``len``(matrix)):` `        ``for` `j ``in` `range``(``len``(matrix[``0``])):`   `            ``# Adding the element into the` `            ``# defined matrix` `            ``m.append(matrix[i][j])`   `    ``return` `m[:]`   `# Function to enlarge a matrix ` `# such that each element occurs ` `# in R rows and C columns` `def` `dimensions(R, C, matrix):` `    `  `    ``# Initializing the enlarged ` `    ``# matrix` `    ``ans ``=` `[[]] ``*` `R` `    `  `    ``# Variables to iterate over ` `    ``# the matrix` `    ``ctr ``=` `0` `    ``r ``=` `0`   `    `  `    ``while``(ctr < ``len``(matrix)):`   `        ``# To keep the rows in the ` `        ``# range [0, R]` `        ``r ``=` `r ``%` `R` `        ``c ``=` `0` `        ``while``(c < C):` `            `  `            ``# In order to fix the number ` `            ``# of columns, the above r ` `            ``# statement can be commented  ` `            ``# and this can be uncommented` `            ``# c = c % C` `            ``ans[r].append(matrix[ctr])` `            `  `            ``c``+``=` `1` `     `  `        ``ctr ``+``=` `1` `        ``r``+``=` `1` `        `  `        `  `    ``return` `ans`   `# Driver code` `if` `__name__ ``=``=` `'__main__'``: `   `    ``arr ``=` `[[``1``, ``2``], [``3``, ``4``]]` `    ``R, C ``=` `2``, ``3`   `    ``ans ``=` `dimensions(R, C, oneD(arr))` `    `  `    ``# Print the enlarged matrix` `    ``for` `i ``in` `ans:` `        ``print``(i)`

## C#

 `// C# program to enlarge a matrix` `// such that each element occurs in` `// R rows and C columns` `using` `System;` `using` `System.Collections;` `using` `System.Collections.Generic;`   `class` `GFG{`   `// Function to convert the matrix` `// into a 1-D array` `static` `List<``int``> oneD(List > matrix)` `{` `    `  `    ``// Variable to store the` `    ``// given matrix` `    ``List<``int``> m = ``new` `List<``int``>();`   `    ``// Iterating through all the` `    ``// elements of the matrix` `    ``for``(``int` `i = 0; i < matrix.Count; ++i)` `        ``for``(``int` `j = 0; j < matrix.Count; ++j)` `        `  `            ``// Adding the element into the` `            ``// defined matrix` `            ``m.Add(matrix[i][j]);`   `    ``return` `m;` `}`   `// Function to enlarge a matrix` `// such that each element occurs` `// in R rows and C columns` `static` `List> dimensions(``int` `R, ``int` `C,` `                                  ``List<``int``> matrix)` `{` `    `  `    ``// Initializing the enlarged` `    ``// matrix` `    ``List > ans = ``new` `List >();` `    ``for``(``int` `i = 0; i < R; ++i) ` `    ``{` `        ``ans.Add(``new` `List<``int``>());` `    ``}`   `    ``// Variables to iterate over` `    ``// the matrix` `    ``int` `ctr = 0;` `    ``int` `r = 0;`   `    ``while` `(ctr < matrix.Count) ` `    ``{` `        `  `        ``// To keep the rows in the` `        ``// range [0, R]` `        ``r = r % R;` `        ``int` `c = 0;` `        `  `        ``while` `(c < C) ` `        ``{` `            `  `            ``// In order to fix the number` `            ``// of columns, the above r` `            ``// statement can be commented` `            ``// and this can be uncommented` `            ``// c = c % C` `            ``for``(``int` `i = 0; i < R; ++i)` `            ``{` `                ``ans[i].Add(matrix[ctr]);` `            ``}` `            ``c += 1;` `        ``}` `        ``ctr += 1;` `        ``r += 1;` `    ``}` `    ``return` `ans;` `}`   `// Driver code` `public` `static` `void` `Main()` `{`   `    ``List> arr = ``new` `List>();` `    ``arr.Add(``new` `List<``int``>() { 1, 2 });` `    ``arr.Add(``new` `List<``int``>() { 3, 4 });` `    ``int` `R = 2, C = 3;`   `    ``List> ans = ``new` `List>(` `        ``dimensions(R, C, oneD(arr)));`   `    ``// Print the enlarged matrix` `    ``for``(``int` `i = 0; i < ans.Count; ++i)` `    ``{` `        ``Console.Write(``"["``);` `        ``for``(``int` `j = 0; j < ans[i].Count; ++j) ` `        ``{` `            ``Console.Write(ans[i][j] + ``", "``);` `        ``}` `        ``Console.Write(``"]\n"``);` `    ``}` `}` `}`   `// This code is contributed by akhilsaini`

Output:

```[1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]
[1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]

```

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.

My Personal Notes arrow_drop_up 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.