Convert a Matrix into another Matrix of given dimensions
Given a matrix, mat[][] of size N * M and two positive integers A and B, the task is to construct a matrix of size A * B from the given matrix elements. If multiple solutions exist, then print any one of them. Otherwise, print -1.
Input: mat[][] = { { 1, 2, 3, 4, 5, 6 } }, A = 2, B = 3
Output: { { 1, 2, 3 }, { 4, 5, 6 } }
Explanation:
Since the size of the matrix { { 1, 2, 3 }, { 4, 5, 6 } } is A * B(2 * 3).
Therefore, the required output is { { 1, 2, 3 }, { 4, 5, 6 } }.
Input: mat[][] = { {1, 2}, { 3, 4 }, { 5, 6 } }, A = 1, B = 6
Output: { { 1, 2, 3, 4, 5, 6 } }
Approach: Follow the steps below to solve the problem:
- Initialize a matrix of size A * B say, res[][].
- Traverse the matrix, mat[][] and insert each element of the matrix into the matrix, res[][].
- Finally, print the matrix res[][].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void ConstMatrix( int * mat, int N, int M,
int A, int B)
{
if (N * M != A * B)
return ;
int idx = 0;
int res[A][B];
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < M; j++)
{
res[idx / B][idx % B] = *((mat + i * M) + j);
idx++;
}
}
for ( int i = 0; i < A; i++)
{
for ( int j = 0; j < B; j++)
cout << res[i][j] << " " ;
cout << "\n" ;
}
}
int main()
{
int mat[][6] = { { 1, 2, 3, 4, 5, 6 } };
int A = 2;
int B = 3;
int N = sizeof (mat) / sizeof (mat[0]);
int M = sizeof (mat[0]) / sizeof ( int );
ConstMatrix(( int *)mat, N, M, A, B);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void ConstMatrix( int [][] mat, int N, int M,
int A, int B)
{
if (N * M != A * B)
return ;
int idx = 0 ;
int [][]res = new int [A][B];
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < M; j++)
{
res[idx / B][idx % B] = mat[i][j];
idx++;
}
}
for ( int i = 0 ; i < A; i++)
{
for ( int j = 0 ; j < B; j++)
System.out.print(res[i][j] + " " );
System.out.print( "\n" );
}
}
public static void main(String[] args)
{
int mat[][] = { { 1 , 2 , 3 , 4 , 5 , 6 } };
int A = 2 ;
int B = 3 ;
int N = mat.length;
int M = mat[ 0 ].length;
ConstMatrix(mat, N, M, A, B);
}
}
|
Python3
def ConstMatrix(mat, N, M, A, B):
if (N * M ! = A * B):
return - 1
idx = 0 ;
res = [[ 0 for i in range (B)] for i in range (A)]
for i in range (N):
for j in range (M):
res[idx / / B][idx % B] = mat[i][j];
idx + = 1
for i in range (A):
for j in range (B):
print (res[i][j], end = " " )
print ()
if __name__ = = '__main__' :
mat = [ [ 1 , 2 , 3 , 4 , 5 , 6 ] ]
A = 2
B = 3
N = len (mat)
M = len (mat[ 0 ])
ConstMatrix(mat, N, M, A, B)
|
C#
using System;
class GFG
{
static void ConstMatrix( int [,] mat, int N, int M,
int A, int B)
{
if (N * M != A * B)
return ;
int idx = 0;
int [,]res = new int [A,B];
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < M; j++)
{
res[idx / B, idx % B] = mat[i, j];
idx++;
}
}
for ( int i = 0; i < A; i++)
{
for ( int j = 0; j < B; j++)
Console.Write(res[i, j] + " " );
Console.Write( "\n" );
}
}
public static void Main(String[] args)
{
int [,]mat = {{ 1, 2, 3, 4, 5, 6 }};
int A = 2;
int B = 3;
int N = mat.GetLength(0);
int M = mat.GetLength(1);
ConstMatrix(mat, N, M, A, B);
}
}
|
Javascript
<script>
function ConstMatrix(mat, N, M,
A, B)
{
if (N * M != A * B)
return ;
let idx = 0;
let res = new Array(A);
for ( var i = 0; i < res.length; i++) {
res[i] = new Array(2);
}
for (let i = 0; i < N; i++)
{
for (let j = 0; j < M; j++)
{
res[(Math.floor(idx / B))][idx % B] = mat[i][j];
idx++;
}
}
for (let i = 0; i < A; i++)
{
for (let j = 0; j < B; j++)
document.write(res[i][j] + " " );
document.write( "<br/>" );
}
}
let mat = [[ 1, 2, 3, 4, 5, 6 ]];
let A = 2;
let B = 3;
let N = mat.length;
let M = mat[0].length;
ConstMatrix(mat, N, M, A, B);
</script>
|
Time Complexity: O(N * M)
Auxiliary Space: O(N * M)
Last Updated :
24 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...