Enlarge a Matrix such that each element occurs in R rows and C columns
Last Updated :
06 Jan, 2023
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++
#include <bits/stdc++.h>
using namespace std;
vector< int > oneD(vector<vector< int >> &matrix){
vector< int > m;
for ( int i = 0; i < matrix.size(); ++i)
for ( int j = 0; j < matrix[0].size(); ++j)
m.push_back(matrix[i][j]);
return m;
}
vector<vector< int >> dimensions( int R, int C, vector< int > matrix) {
vector<vector< int >> ans(R+1);
int ctr = 0;
int r = 0;
while (ctr < matrix.size()) {
r = r % R;
int c = 0;
while (c < C) {
for ( int i = 0; i < R; ++i)
ans[i].push_back(matrix[ctr]);
c += 1;
}
ctr += 1;
r += 1;
}
return ans;
}
int main() {
vector<vector< int >> arr = {{1, 2}, {3, 4}};
int R = 2, C = 3;
vector<vector< int >> ans = dimensions(R, C, oneD(arr));
for ( int i = 0; i < ans.size(); ++i) {
for ( int j = 0; j < ans[i].size(); ++j) {
cout << ans[i][j] << " " ;
}
cout<<endl;
}
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static ArrayList<Integer>oneD(
ArrayList<ArrayList<Integer>> matrix)
{
ArrayList<Integer> m = new ArrayList<Integer>();
for ( int i = 0 ; i < matrix.size(); ++i)
for ( int j = 0 ; j < matrix.get( 0 ).size(); ++j)
m.add(matrix.get(i).get(j));
return m;
}
static ArrayList<ArrayList<Integer>>dimensions(
int R, int C, ArrayList<Integer> matrix)
{
ArrayList<ArrayList<Integer>> ans = new ArrayList<>();
for ( int i = 0 ; i < R; ++i)
{
ans.add( new ArrayList<Integer>());
}
int ctr = 0 ;
int r = 0 ;
while (ctr < matrix.size())
{
r = r % R;
int c = 0 ;
while (c < C)
{
for ( int i = 0 ; i < R; ++i)
{
ans.get(i).add(matrix.get(ctr));
}
c += 1 ;
}
ctr += 1 ;
r += 1 ;
}
return ans;
}
public static void main(String[] args)
{
ArrayList<ArrayList<Integer>> arr = new ArrayList<>();
arr.add( new ArrayList<Integer>(Arrays.asList( 1 , 2 )));
arr.add( new ArrayList<Integer>(Arrays.asList( 3 , 4 )));
int R = 2 , C = 3 ;
ArrayList<ArrayList<Integer>> ans = new ArrayList<>(
dimensions(R, C, oneD(arr)));
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" );
}
}
}
|
Python3
def oneD(matrix):
m = []
for i in range ( len (matrix)):
for j in range ( len (matrix[ 0 ])):
m.append(matrix[i][j])
return m[:]
def dimensions(R, C, matrix):
ans = [[]] * R
ctr = 0
r = 0
while (ctr < len (matrix)):
r = r % R
c = 0
while (c < C):
ans[r].append(matrix[ctr])
c + = 1
ctr + = 1
r + = 1
return ans
if __name__ = = '__main__' :
arr = [[ 1 , 2 ], [ 3 , 4 ]]
R, C = 2 , 3
ans = dimensions(R, C, oneD(arr))
for i in ans:
print (i)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
static List< int > oneD(List<List< int > > matrix)
{
List< int > m = new List< int >();
for ( int i = 0; i < matrix.Count; ++i)
for ( int j = 0; j < matrix[0].Count; ++j)
m.Add(matrix[i][j]);
return m;
}
static List<List< int >> dimensions( int R, int C,
List< int > matrix)
{
List<List< int > > ans = new List<List< int > >();
for ( int i = 0; i < R; ++i)
{
ans.Add( new List< int >());
}
int ctr = 0;
int r = 0;
while (ctr < matrix.Count)
{
r = r % R;
int c = 0;
while (c < C)
{
for ( int i = 0; i < R; ++i)
{
ans[i].Add(matrix[ctr]);
}
c += 1;
}
ctr += 1;
r += 1;
}
return ans;
}
public static void Main()
{
List<List< int >> arr = new List<List< int >>();
arr.Add( new List< int >() { 1, 2 });
arr.Add( new List< int >() { 3, 4 });
int R = 2, C = 3;
List<List< int >> ans = new List<List< int >>(
dimensions(R, C, oneD(arr)));
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" );
}
}
}
|
Javascript
<script>
function oneD(matrix){
let m = new Array();
for (let i = 0; i < matrix.length; ++i)
for (let j = 0; j < matrix[0].length; ++j)
m.push(matrix[i][j]);
return m;
}
function dimensions(R, C, matrix) {
let ans = new Array();
for (let i = 0; i < R + 1; i++){
ans.push([])
}
let ctr = 0;
let r = 0;
while (ctr < matrix.length) {
r = r % R;
let c = 0;
while (c < C) {
for (let i = 0; i < R; ++i)
ans[i].push(matrix[ctr]);
c += 1;
}
ctr += 1;
r += 1;
}
return ans;
}
let arr = [[1, 2], [3, 4]];
let R = 2, C = 3;
let ans = dimensions(R, C, oneD(arr));
for (let i of ans){
if (i.length)
document.write( "[" + i + "]<br>" )
}
</script>
|
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]
Time Complexity: O(M * N * R * C), where M and N are dimensions of the given matrix and R and C are the given integers.
Auxiliary Space: O(M * N * R * C)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...